From 881d3cd2288b73cb45dc8db099076ceaf7a04ecb Mon Sep 17 00:00:00 2001 From: mat Date: Wed, 24 Aug 2022 22:42:54 -0500 Subject: [PATCH] add trait feature to az-block --- azalea-block/Cargo.toml | 4 ++ azalea-block/README.md | 2 + azalea-block/block-macros/src/lib.rs | 86 +++++++++++++++------------- azalea-block/src/lib.rs | 2 + azalea-world/Cargo.toml | 2 +- 5 files changed, 55 insertions(+), 41 deletions(-) diff --git a/azalea-block/Cargo.toml b/azalea-block/Cargo.toml index edeba385..71d7149f 100644 --- a/azalea-block/Cargo.toml +++ b/azalea-block/Cargo.toml @@ -9,3 +9,7 @@ version = "0.1.0" [dependencies] block-macros = {path = "./block-macros"} + +[features] +default = ["trait"] +trait = [] diff --git a/azalea-block/README.md b/azalea-block/README.md index 39843ef4..84da3241 100644 --- a/azalea-block/README.md +++ b/azalea-block/README.md @@ -6,3 +6,5 @@ There's two main things here, the `BlockState` enum and the `Block` trait. `BlockState` is a simple enum with every possible block state as variant, and `Block` is a heavier trait which lets you access information about a block more easily. Every block is a struct that implements `Block`. You can freely convert between `BlockState` and `Block` with .into(). + +If you don't want the `Block` trait, set default-features to false. diff --git a/azalea-block/block-macros/src/lib.rs b/azalea-block/block-macros/src/lib.rs index 907fd241..e6585600 100644 --- a/azalea-block/block-macros/src/lib.rs +++ b/azalea-block/block-macros/src/lib.rs @@ -426,41 +426,43 @@ 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 - } - - impl Block for #block_struct_name { - fn behavior(&self) -> BlockBehavior { - #block_behavior + if cfg!(feature = "trait") { + let block_struct = quote! { + #[derive(Debug)] + pub struct #block_struct_name { + #block_struct_fields } - 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 Default for #block_struct_name { - fn default() -> Self { - Self { - #block_default_fields + impl Block for #block_struct_name { + fn behavior(&self) -> BlockBehavior { + #block_behavior + } + fn id(&self) -> &'static str { + #block_id } } - } - }; - block_structs.extend(block_struct); + 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 + } + } + } + }; + + block_structs.extend(block_struct); + } } let last_state_id = (state_id - 1) as u32; - quote! { + let mut generated = quote! { #property_enums #[repr(u32)] @@ -469,18 +471,6 @@ pub fn make_block_states(input: TokenStream) -> TokenStream { #block_state_enum_variants } - #block_structs - - impl From for Box { - fn from(b: BlockState) -> Self { - let b = b as usize; - match b { - #from_state_to_block_match - _ => panic!("Invalid block state: {}", b), - } - } - } - impl BlockState { /// Returns the highest possible state #[inline] @@ -488,7 +478,23 @@ pub fn make_block_states(input: TokenStream) -> TokenStream { #last_state_id } } + }; + if cfg!(feature = "trait") { + generated.extend(quote! { + #block_structs + + impl From for Box { + fn from(b: BlockState) -> Self { + let b = b as usize; + match b { + #from_state_to_block_match + _ => panic!("Invalid block state: {}", b), + } + } + } + }); } - .into() + + generated.into() } diff --git a/azalea-block/src/lib.rs b/azalea-block/src/lib.rs index 3eb86a90..9320a2a5 100644 --- a/azalea-block/src/lib.rs +++ b/azalea-block/src/lib.rs @@ -1,6 +1,8 @@ +#[cfg(feature = "trait")] mod behavior; mod blocks; +#[cfg(feature = "trait")] pub use behavior::BlockBehavior; pub use blocks::*; diff --git a/azalea-world/Cargo.toml b/azalea-world/Cargo.toml index dca0d362..250a7bd9 100644 --- a/azalea-world/Cargo.toml +++ b/azalea-world/Cargo.toml @@ -6,7 +6,7 @@ version = "0.1.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -azalea-block = {path = "../azalea-block"} +azalea-block = {path = "../azalea-block", default-features = false} azalea-buf = {path = "../azalea-buf"} azalea-core = {path = "../azalea-core"} azalea-entity = {path = "../azalea-entity"}