diff options
| author | Ubuntu <github@matdoes.dev> | 2023-02-10 01:56:45 +0000 |
|---|---|---|
| committer | Ubuntu <github@matdoes.dev> | 2023-02-10 01:56:45 +0000 |
| commit | 9d4f738d4e66adf0796e163d1c9368aaba906bba (patch) | |
| tree | ae7b3c7fca0ff054eb67c1311955e9321a37e559 /azalea-block/src/lib.rs | |
| parent | 48b2a37aa09f0302b40d0678cdde2703f919ed18 (diff) | |
| download | azalea-drasl-9d4f738d4e66adf0796e163d1c9368aaba906bba.tar.xz | |
make blockstate good
Diffstat (limited to 'azalea-block/src/lib.rs')
| -rwxr-xr-x | azalea-block/src/lib.rs | 43 |
1 files changed, 28 insertions, 15 deletions
diff --git a/azalea-block/src/lib.rs b/azalea-block/src/lib.rs index 4a35be00..7a62e588 100755 --- a/azalea-block/src/lib.rs +++ b/azalea-block/src/lib.rs @@ -1,4 +1,5 @@ #![doc = include_str!("../README.md")] +#![feature(trait_upcasting)] mod behavior; mod blocks; @@ -6,10 +7,7 @@ mod blocks; use azalea_buf::{BufReadError, McBufReadable, McBufVarReadable, McBufVarWritable, McBufWritable}; pub use behavior::BlockBehavior; pub use blocks::*; -use std::{ - io::{Cursor, Write}, - mem, -}; +use std::io::{Cursor, Write}; impl BlockState { /// Transmutes a u32 to a block state. @@ -17,8 +15,8 @@ impl BlockState { /// # Safety /// The `state_id` should be a valid block state. #[inline] - pub unsafe fn from_u32_unsafe(state_id: u32) -> Self { - mem::transmute::<u32, BlockState>(state_id) + pub unsafe fn from_u32_unchecked(state_id: u32) -> Self { + BlockState { id: state_id } } #[inline] @@ -33,7 +31,7 @@ impl TryFrom<u32> for BlockState { /// Safely converts a state id to a block state. fn try_from(state_id: u32) -> Result<Self, Self::Error> { if Self::is_valid_state(state_id) { - Ok(unsafe { Self::from_u32_unsafe(state_id) }) + Ok(unsafe { Self::from_u32_unchecked(state_id) }) } else { Err(()) } @@ -50,7 +48,7 @@ impl McBufReadable for BlockState { } impl McBufWritable for BlockState { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - u32::var_write_into(&(*self as u32), buf) + u32::var_write_into(&self.id, buf) } } @@ -60,7 +58,7 @@ mod tests { #[test] fn test_from_u32() { - assert_eq!(BlockState::try_from(0).unwrap(), BlockState::Air); + assert_eq!(BlockState::try_from(0).unwrap(), BlockState::AIR); assert!(BlockState::try_from(BlockState::max_state()).is_ok()); assert!(BlockState::try_from(BlockState::max_state() + 1).is_err()); @@ -68,19 +66,34 @@ mod tests { #[test] fn test_from_blockstate() { - let block: Box<dyn Block> = Box::<dyn Block>::from(BlockState::Air); + let block: Box<dyn Block> = Box::<dyn Block>::from(BlockState::AIR); assert_eq!(block.id(), "air"); - let block: Box<dyn Block> = Box::<dyn Block>::from(BlockState::FloweringAzalea); + let block: Box<dyn Block> = + Box::<dyn Block>::from(BlockState::from(azalea_registry::Block::FloweringAzalea)); assert_eq!(block.id(), "flowering_azalea"); } - #[cfg(not(feature = "full-debug"))] #[test] fn test_debug_blockstate() { - assert_eq!( - format!("{:?}", BlockState::FloweringAzalea), - "BlockState (flowering_azalea)" + let formatted = format!( + "{:?}", + BlockState::from(azalea_registry::Block::FloweringAzalea) + ); + assert!( + formatted.ends_with(", FloweringAzaleaBlock)"), + "{}", + formatted + ); + + let formatted = format!( + "{:?}", + BlockState::from(azalea_registry::Block::BigDripleafStem) + ); + assert!( + formatted.ends_with(", BigDripleafStemBlock { facing: North, waterlogged: false })"), + "{}", + formatted ); } } |
