From 3d6c83c6d81e62bd746af1d979fc594a4fc1cacf Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 4 Aug 2020 09:47:48 +0800 Subject: [PATCH] Fix serde builds. --- src/any.rs | 53 +++++++++++++++++++++++++++++++------------------ src/serde/de.rs | 3 +++ 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/src/any.rs b/src/any.rs index f9c8a093..bdde78f3 100644 --- a/src/any.rs +++ b/src/any.rs @@ -337,7 +337,10 @@ impl Dynamic { Union::Variant(value) => (***value).type_name(), #[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "sync"))] - Union::Shared(cell) => (*cell.borrow()).type_name(), + Union::Shared(cell) => cell + .try_borrow() + .map(|v| (*v).type_name()) + .unwrap_or(""), #[cfg(not(feature = "no_closure"))] #[cfg(feature = "sync")] Union::Shared(cell) => (*cell.read().unwrap()).type_name(), @@ -394,10 +397,20 @@ impl fmt::Display for Dynamic { Union::FnPtr(value) => fmt::Display::fmt(value, f), #[cfg(not(feature = "no_std"))] - Union::Variant(value) if value.is::() => write!(f, ""), - Union::Variant(value) => write!(f, "{}", (*value).type_name()), + Union::Variant(value) if value.is::() => f.write_str(""), + Union::Variant(value) => f.write_str((*value).type_name()), #[cfg(not(feature = "no_closure"))] - Union::Shared(_) => f.write_str(""), + #[cfg(not(feature = "sync"))] + Union::Shared(cell) => { + if let Ok(v) = cell.try_borrow() { + fmt::Display::fmt(&*v, f) + } else { + f.write_str("") + } + } + #[cfg(not(feature = "no_closure"))] + #[cfg(feature = "sync")] + Union::Shared(cell) => fmt::Display::fmt(*cell.read_lock().unwrap(), f), } } } @@ -425,7 +438,17 @@ impl fmt::Debug for Dynamic { Union::Variant(value) if value.is::() => write!(f, ""), Union::Variant(value) => write!(f, "{}", (*value).type_name()), #[cfg(not(feature = "no_closure"))] - Union::Shared(_) => f.write_str(""), + #[cfg(not(feature = "sync"))] + Union::Shared(cell) => { + if let Ok(v) = cell.try_borrow() { + write!(f, "{:?} (shared)", *v) + } else { + f.write_str("") + } + } + #[cfg(not(feature = "no_closure"))] + #[cfg(feature = "sync")] + Union::Shared(cell) => fmt::Display::fmt(*cell.read_lock().unwrap(), f), } } } @@ -861,13 +884,9 @@ impl Dynamic { /// Get a reference of a specific type to the `Dynamic`. /// Casting to `Dynamic` just returns a reference to it. /// - /// Returns `None` if the cast fails. - /// - /// # Panics - /// - /// Panics if the value is shared. + /// Returns `None` if the cast fails, or if the value is shared. #[inline(always)] - fn downcast_ref(&self) -> Option<&T> { + pub(crate) fn downcast_ref(&self) -> Option<&T> { let type_id = TypeId::of::(); if type_id == TypeId::of::() { @@ -940,7 +959,7 @@ impl Dynamic { match &self.0 { Union::Variant(value) => value.as_ref().as_ref().as_any().downcast_ref::(), #[cfg(not(feature = "no_closure"))] - Union::Shared(_) => unreachable!(), + Union::Shared(_) => None, _ => None, } } @@ -948,13 +967,9 @@ impl Dynamic { /// Get a mutable reference of a specific type to the `Dynamic`. /// Casting to `Dynamic` just returns a mutable reference to it. /// - /// Returns `None` if the cast fails. - /// - /// # Panics - /// - /// Panics if the value is shared. + /// Returns `None` if the cast fails, or if the value is shared. #[inline(always)] - fn downcast_mut(&mut self) -> Option<&mut T> { + pub(crate) fn downcast_mut(&mut self) -> Option<&mut T> { let type_id = TypeId::of::(); if type_id == TypeId::of::() { @@ -1021,7 +1036,7 @@ impl Dynamic { match &mut self.0 { Union::Variant(value) => value.as_mut().as_mut_any().downcast_mut::(), #[cfg(not(feature = "no_closure"))] - Union::Shared(_) => unreachable!(), + Union::Shared(_) => None, _ => None, } } diff --git a/src/serde/de.rs b/src/serde/de.rs index 8a60604c..f6d328db 100644 --- a/src/serde/de.rs +++ b/src/serde/de.rs @@ -177,6 +177,9 @@ impl<'de> Deserializer<'de> for &mut DynamicDeserializer<'de> { Union::Variant(value) if value.is::() => self.deserialize_u128(visitor), Union::Variant(_) => self.type_error(), + + #[cfg(not(feature = "no_closure"))] + Union::Shared(_) => self.type_error(), } }