From 74c3ae52f84d988b8bf3f0affe143d2cd2d8143a Mon Sep 17 00:00:00 2001 From: mat Date: Thu, 16 Jun 2022 23:38:51 -0500 Subject: azalea-world uses azalea-block --- azalea-block/src/lib.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'azalea-block/src') diff --git a/azalea-block/src/lib.rs b/azalea-block/src/lib.rs index 01c86b11..95e8ce37 100644 --- a/azalea-block/src/lib.rs +++ b/azalea-block/src/lib.rs @@ -4,3 +4,44 @@ mod blocks; pub use behavior::BlockBehavior; pub use blocks::*; +use std::mem; + +impl BlockState { + /// Transmutes a u32 to a block state. UB if the value is not a valid block + /// state. + #[inline] + pub unsafe fn from_u32_unsafe(state_id: u32) -> Self { + mem::transmute::(state_id) + } + + #[inline] + pub fn is_valid_state(state_id: u32) -> bool { + state_id <= Self::max_state() + } +} + +impl TryFrom for BlockState { + type Error = (); + + /// Safely converts a state id to a block state. + fn try_from(state_id: u32) -> Result { + if Self::is_valid_state(state_id) { + Ok(unsafe { Self::from_u32_unsafe(state_id) }) + } else { + Err(()) + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_from_u32() { + 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()); + } +} -- cgit v1.2.3