diff options
Diffstat (limited to 'azalea-block/src')
| -rw-r--r-- | azalea-block/src/block_state.rs | 22 | ||||
| -rw-r--r-- | azalea-block/src/generated.rs | 1 | ||||
| -rw-r--r-- | azalea-block/src/lib.rs | 7 |
3 files changed, 21 insertions, 9 deletions
diff --git a/azalea-block/src/block_state.rs b/azalea-block/src/block_state.rs index 9a3eece8..3806c915 100644 --- a/azalea-block/src/block_state.rs +++ b/azalea-block/src/block_state.rs @@ -1,5 +1,6 @@ use std::{ fmt::{self, Debug}, + hint::assert_unchecked, io::{self, Cursor, Write}, }; @@ -68,6 +69,12 @@ impl BlockState { /// hard-code them or store them in databases. #[inline] pub const fn id(&self) -> BlockStateIntegerRepr { + // this unsafe assert allows us to skip bounds checks if the array has at least + // MAX_STATE items. + // SAFETY: BlockState::id is private and is always checked with is_valid_state + // before being constructed. + unsafe { assert_unchecked(Self::is_valid_state(self.id)) }; + self.id } } @@ -97,13 +104,12 @@ impl TryFrom<u16> for BlockState { type Error = (); /// Safely converts a u16 state ID to a block state. - fn try_from(state_id: u16) -> Result<Self, Self::Error> { - let state_id = state_id as BlockStateIntegerRepr; - if Self::is_valid_state(state_id) { - Ok(BlockState { id: state_id }) - } else { - Err(()) + fn try_from(id: u16) -> Result<Self, Self::Error> { + let id = id as BlockStateIntegerRepr; + if !Self::is_valid_state(id) { + return Err(()); } + Ok(BlockState { id }) } } impl From<BlockState> for u32 { @@ -137,8 +143,8 @@ impl Debug for BlockState { } impl From<BlockState> for BlockKind { - fn from(value: BlockState) -> Self { - Box::<dyn BlockTrait>::from(value).as_registry_block() + fn from(block_state: BlockState) -> Self { + block_state.as_block_kind() } } diff --git a/azalea-block/src/generated.rs b/azalea-block/src/generated.rs index 85a911c1..62b580d7 100644 --- a/azalea-block/src/generated.rs +++ b/azalea-block/src/generated.rs @@ -3,6 +3,7 @@ use std::{fmt::Debug, str::FromStr}; use azalea_block_macros::make_block_states; +use azalea_registry::{Registry, builtin::BlockKind}; use crate::{ BlockBehavior, BlockState, BlockStates, BlockTrait, InvalidPropertyError, Property, diff --git a/azalea-block/src/lib.rs b/azalea-block/src/lib.rs index 60849b83..9e2a3c5c 100644 --- a/azalea-block/src/lib.rs +++ b/azalea-block/src/lib.rs @@ -31,7 +31,12 @@ pub trait BlockTrait: Debug + Any { /// /// This is a lossy conversion, as [`BlockKind`] doesn't contain any state /// data. - fn as_registry_block(&self) -> BlockKind; + fn as_block_kind(&self) -> BlockKind; + #[deprecated = "renamed to as_block_kind"] + #[doc(hidden)] + fn as_registry_block(&self) -> BlockKind { + self.as_block_kind() + } /// Returns a map of property names on this block to their values as /// strings. |
