diff options
| author | mat <github@matdoes.dev> | 2022-09-18 23:13:45 -0500 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2022-09-18 23:13:45 -0500 |
| commit | 4247945df13507fb07a4746263bb702d6fbe04cd (patch) | |
| tree | d47d27704359939c7fa389e79ae0cb0aba8a0abd /azalea-block | |
| parent | 79cf19f93e273aa5421e2cc46fee36cc64e7731d (diff) | |
| download | azalea-drasl-4247945df13507fb07a4746263bb702d6fbe04cd.tar.xz | |
start work on optimizing block macros
Diffstat (limited to 'azalea-block')
| -rw-r--r-- | azalea-block/azalea-block-macros/src/lib.rs | 77 | ||||
| -rw-r--r-- | azalea-block/src/lib.rs | 6 |
2 files changed, 48 insertions, 35 deletions
diff --git a/azalea-block/azalea-block-macros/src/lib.rs b/azalea-block/azalea-block-macros/src/lib.rs index ac61912f..85887418 100644 --- a/azalea-block/azalea-block-macros/src/lib.rs +++ b/azalea-block/azalea-block-macros/src/lib.rs @@ -426,45 +426,45 @@ pub fn make_block_states(input: TokenStream) -> TokenStream { quote! { BlockState::#block_name_pascal_case } }; - let block_struct = quote! { - #[derive(Debug)] - pub struct #block_struct_name { - #block_struct_fields - } + let block_struct = quote! { + #[derive(Debug, Copy, Clone)] + pub struct #block_struct_name { + #block_struct_fields + } - impl Block for #block_struct_name { - fn behavior(&self) -> BlockBehavior { - #block_behavior - } - fn id(&self) -> &'static str { - #block_id - } + impl Block for #block_struct_name { + fn behavior(&self) -> BlockBehavior { + #block_behavior + } + fn id(&self) -> &'static str { + #block_id } + } - impl From<#block_struct_name> for BlockState { - fn from(b: #block_struct_name) -> Self { - #from_block_to_state_match - } + impl From<#block_struct_name> for BlockState { + fn from(b: #block_struct_name) -> Self { + #from_block_to_state_match } + } - impl Default for #block_struct_name { - fn default() -> Self { - Self { - #block_default_fields - } + impl Default for #block_struct_name { + fn default() -> Self { + Self { + #block_default_fields } } - }; + } + }; - block_structs.extend(block_struct); - } + block_structs.extend(block_struct); + } let last_state_id = (state_id - 1) as u32; let mut generated = quote! { #property_enums #[repr(u32)] - #[derive(Copy, Clone, PartialEq, Eq, Debug)] + #[derive(Copy, Clone, PartialEq, Eq)] pub enum BlockState { #block_state_enum_variants } @@ -476,21 +476,28 @@ pub fn make_block_states(input: TokenStream) -> TokenStream { #last_state_id } } + + impl std::fmt::Debug for BlockState { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + // having a big match statement here would take up 700kb + f.write_str("BlockState") + } + } }; - generated.extend(quote! { - #block_structs + generated.extend(quote! { + #block_structs - impl From<BlockState> for Box<dyn Block> { - fn from(b: BlockState) -> Self { - let b = b as usize; - match b { - #from_state_to_block_match - _ => panic!("Invalid block state: {}", b), - } + impl From<BlockState> for Box<dyn Block> { + fn from(b: BlockState) -> Self { + let b = b as usize; + match b { + #from_state_to_block_match + _ => panic!("Invalid block state: {}", b), } } - }); + } + }); generated.into() } diff --git a/azalea-block/src/lib.rs b/azalea-block/src/lib.rs index fdcfe631..969288f5 100644 --- a/azalea-block/src/lib.rs +++ b/azalea-block/src/lib.rs @@ -63,4 +63,10 @@ mod tests { assert!(BlockState::try_from(BlockState::max_state()).is_ok()); assert!(BlockState::try_from(BlockState::max_state() + 1).is_err()); } + + #[test] + fn test_from_blockstate() { + let box_block: Box<dyn Block> = Box::<dyn Block>::from(BlockState::Air); + assert_eq!(box_block.id(), "air"); + } } |
