From e1289aaf7717e6de0f685079e3f36430cf27a90a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Obr=C3=A9jan?= Date: Sat, 2 Jul 2022 18:14:59 +0200 Subject: [PATCH] Store type names if `debug_assertions` is enabled This helps provide better `Debug` formating by printing actual type names instead of type ids. --- src/lib.rs | 56 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2622978..84022bb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -81,7 +81,13 @@ macro_rules! impl_erased_set { #[doc(hidden)] ::alloc::collections::BTreeMap< ::core::any::TypeId, - ::alloc::boxed::Box + ::alloc::boxed::Box, + >, + #[doc(hidden)] + #[cfg(debug_assertions)] + ::alloc::collections::BTreeMap< + ::core::any::TypeId, + &'static str >, ); @@ -101,7 +107,11 @@ macro_rules! impl_erased_set { /// ``` #[must_use] pub fn new() -> Self { - Self(::alloc::collections::BTreeMap::new()) + Self( + ::alloc::collections::BTreeMap::new(), + #[cfg(debug_assertions)] + ::alloc::collections::BTreeMap::new(), + ) } /// Returns `true` if the set contains no instances of any type. @@ -224,6 +234,9 @@ macro_rules! impl_erased_set { use ::core::any::{Any, TypeId}; use ::alloc::boxed::Box; + #[cfg(debug_assertions)] + self.1.insert(TypeId::of::(), core::any::type_name::()); + let boxed_any: &Box = self .0 .entry(TypeId::of::()) @@ -256,6 +269,9 @@ macro_rules! impl_erased_set { use ::core::any::{Any, TypeId}; use ::alloc::boxed::Box; + #[cfg(debug_assertions)] + self.1.insert(TypeId::of::(), core::any::type_name::()); + let boxed_any: &Box = self .0 .entry(TypeId::of::()) @@ -325,6 +341,9 @@ macro_rules! impl_erased_set { use ::core::any::{Any, TypeId}; use ::alloc::boxed::Box; + #[cfg(debug_assertions)] + self.1.insert(TypeId::of::(), core::any::type_name::()); + self.0 .insert(TypeId::of::(), Box::new(value)) .map(|boxed_any: Box| { @@ -357,6 +376,9 @@ macro_rules! impl_erased_set { use ::core::any::{Any, TypeId}; use ::alloc::boxed::Box; + #[cfg(debug_assertions)] + self.1.remove(&TypeId::of::()); + self.0 .remove(&TypeId::of::()) .map(|boxed_any: Box| { @@ -372,6 +394,22 @@ macro_rules! impl_erased_set { } } +macro_rules! impl_debug { + ($ident:ident) => { + impl ::core::fmt::Debug for $ident { + fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { + // Print types ids. + #[cfg(not(debug_assertions))] + return f.debug_set().entries(self.0.keys()).finish(); + + // Print type names. + #[cfg(debug_assertions)] + return f.debug_set().entries(self.1.values()).finish(); + } + } + }; +} + impl_erased_set! { /// A set of erased types. /// @@ -399,7 +437,7 @@ impl_erased_set! { /// /// assert_eq!(set.len(), 1); /// ``` - #[derive(Debug, Default)] + #[derive(Default)] pub struct ErasedSet: Any; } @@ -429,7 +467,7 @@ impl_erased_set! { /// /// assert_eq!(set.len(), 1); /// ``` - #[derive(Debug, Default)] + #[derive(Default)] pub struct ErasedSendSet: Any + Send; } @@ -459,6 +497,14 @@ impl_erased_set! { /// /// assert_eq!(set.len(), 1); /// ``` - #[derive(Debug, Default)] + #[derive(Default)] pub struct ErasedSyncSet: Any + Send + Sync; } + +impl_debug!(ErasedSet); + +#[cfg(feature = "send")] +impl_debug!(ErasedSendSet); + +#[cfg(feature = "sync")] +impl_debug!(ErasedSyncSet);