Better panic messages.

This commit is contained in:
Stephen Chung 2020-09-27 11:14:50 +08:00
parent 726b030616
commit a2694114d1

View File

@ -629,7 +629,7 @@ impl Dynamic {
}; };
#[cfg(feature = "no_closure")] #[cfg(feature = "no_closure")]
unimplemented!() panic!("'no_closure' feature does not support converting into a shared value");
} }
/// Convert the `Dynamic` value into specific type. /// Convert the `Dynamic` value into specific type.
@ -781,7 +781,20 @@ impl Dynamic {
/// ``` /// ```
#[inline(always)] #[inline(always)]
pub fn cast<T: Variant + Clone>(self) -> T { pub fn cast<T: Variant + Clone>(self) -> T {
self.try_cast::<T>().unwrap() let self_type_name = if self.is_shared() {
// Avoid panics/deadlocks with shared values
"<shared>"
} else {
self.type_name()
};
self.try_cast::<T>().unwrap_or_else(|| {
panic!(
"value is {} and cannot be cast to {}",
self_type_name,
type_name::<T>()
)
})
} }
/// Flatten the `Dynamic` and clone it. /// Flatten the `Dynamic` and clone it.