1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 23:44:38 +00:00

derive Debug for BlockState (#64)

* derive `Debug` for `BlockState`

* change default Debug for BlockState

---------

Co-authored-by: Ubuntu <github@matdoes.dev>
This commit is contained in:
Charles Johnson 2023-02-09 17:18:56 +00:00 committed by GitHub
commit 48b2a37aa0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 21 additions and 2 deletions

View file

@ -6,6 +6,9 @@ name = "azalea-block"
repository = "https://github.com/mat-1/azalea/tree/main/azalea-block" repository = "https://github.com/mat-1/azalea/tree/main/azalea-block"
version = "0.5.0" version = "0.5.0"
[features]
full-debug = ["azalea-block-macros/full-debug"]
[lib] [lib]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View file

@ -8,3 +8,5 @@ There's two main things here, the `BlockState` enum and the `Block` trait.
Every block is a struct that implements `Block`. You can freely convert between `BlockState` and `Block` with .into(). 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. If you don't want the `Block` trait, set default-features to false.
Also, by default the `Debug` implementation for `BlockState` only logs the name of the block and not the name of the enum variant. If you want that, enable the `full-debug` feature (though it's not recommended).

View file

@ -6,6 +6,9 @@ name = "azalea-block-macros"
repository = "https://github.com/mat-1/azalea/tree/main/azalea-block/azalea-block-macros" repository = "https://github.com/mat-1/azalea/tree/main/azalea-block/azalea-block-macros"
version = "0.5.0" version = "0.5.0"
[features]
full-debug = []
[lib] [lib]
proc-macro = true proc-macro = true

View file

@ -563,6 +563,8 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
#[repr(u32)] #[repr(u32)]
#[derive(Copy, Clone, PartialEq, Eq)] #[derive(Copy, Clone, PartialEq, Eq)]
// the Debug impl is very large and slows down compilation
#[cfg_attr(feature = "full-debug", derive(Debug))]
pub enum BlockState { pub enum BlockState {
#block_state_enum_variants #block_state_enum_variants
} }
@ -575,10 +577,10 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
} }
} }
#[cfg(not(feature = "full-debug"))]
impl std::fmt::Debug for BlockState { impl std::fmt::Debug for BlockState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// having a big match statement here would take up 700kb write!(f, "BlockState ({})", Box::<dyn Block>::from(*self).id())
f.write_str("BlockState")
} }
} }
}; };

View file

@ -74,4 +74,13 @@ mod tests {
let block: Box<dyn Block> = Box::<dyn Block>::from(BlockState::FloweringAzalea); let block: Box<dyn Block> = Box::<dyn Block>::from(BlockState::FloweringAzalea);
assert_eq!(block.id(), "flowering_azalea"); assert_eq!(block.id(), "flowering_azalea");
} }
#[cfg(not(feature = "full-debug"))]
#[test]
fn test_debug_blockstate() {
assert_eq!(
format!("{:?}", BlockState::FloweringAzalea),
"BlockState (flowering_azalea)"
);
}
} }