From 48b2a37aa09f0302b40d0678cdde2703f919ed18 Mon Sep 17 00:00:00 2001 From: Charles Johnson <32775248+ChemicalXandco@users.noreply.github.com> Date: Thu, 9 Feb 2023 17:18:56 +0000 Subject: [PATCH] derive `Debug` for `BlockState` (#64) * derive `Debug` for `BlockState` * change default Debug for BlockState --------- Co-authored-by: Ubuntu --- azalea-block/Cargo.toml | 3 +++ azalea-block/README.md | 2 ++ azalea-block/azalea-block-macros/Cargo.toml | 3 +++ azalea-block/azalea-block-macros/src/lib.rs | 6 ++++-- azalea-block/src/lib.rs | 9 +++++++++ 5 files changed, 21 insertions(+), 2 deletions(-) diff --git a/azalea-block/Cargo.toml b/azalea-block/Cargo.toml index 8a45858c..0ab2edab 100755 --- a/azalea-block/Cargo.toml +++ b/azalea-block/Cargo.toml @@ -6,6 +6,9 @@ name = "azalea-block" repository = "https://github.com/mat-1/azalea/tree/main/azalea-block" version = "0.5.0" +[features] +full-debug = ["azalea-block-macros/full-debug"] + [lib] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/azalea-block/README.md b/azalea-block/README.md index 84da3241..efeda675 100755 --- a/azalea-block/README.md +++ b/azalea-block/README.md @@ -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(). 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). diff --git a/azalea-block/azalea-block-macros/Cargo.toml b/azalea-block/azalea-block-macros/Cargo.toml index 39744dcc..d03dbba7 100755 --- a/azalea-block/azalea-block-macros/Cargo.toml +++ b/azalea-block/azalea-block-macros/Cargo.toml @@ -6,6 +6,9 @@ name = "azalea-block-macros" repository = "https://github.com/mat-1/azalea/tree/main/azalea-block/azalea-block-macros" version = "0.5.0" +[features] +full-debug = [] + [lib] proc-macro = true diff --git a/azalea-block/azalea-block-macros/src/lib.rs b/azalea-block/azalea-block-macros/src/lib.rs index bbd98619..7e304d57 100755 --- a/azalea-block/azalea-block-macros/src/lib.rs +++ b/azalea-block/azalea-block-macros/src/lib.rs @@ -563,6 +563,8 @@ pub fn make_block_states(input: TokenStream) -> TokenStream { #[repr(u32)] #[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 { #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 { 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") + write!(f, "BlockState ({})", Box::::from(*self).id()) } } }; diff --git a/azalea-block/src/lib.rs b/azalea-block/src/lib.rs index 6d45fbf4..4a35be00 100755 --- a/azalea-block/src/lib.rs +++ b/azalea-block/src/lib.rs @@ -74,4 +74,13 @@ mod tests { let block: Box = Box::::from(BlockState::FloweringAzalea); assert_eq!(block.id(), "flowering_azalea"); } + + #[cfg(not(feature = "full-debug"))] + #[test] + fn test_debug_blockstate() { + assert_eq!( + format!("{:?}", BlockState::FloweringAzalea), + "BlockState (flowering_azalea)" + ); + } }