From a2694114d12881120f1a799f93b2c428ef7fee4d Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 27 Sep 2020 11:14:50 +0800 Subject: [PATCH] Better panic messages. --- src/any.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/any.rs b/src/any.rs index 12836aff..75c88683 100644 --- a/src/any.rs +++ b/src/any.rs @@ -629,7 +629,7 @@ impl Dynamic { }; #[cfg(feature = "no_closure")] - unimplemented!() + panic!("'no_closure' feature does not support converting into a shared value"); } /// Convert the `Dynamic` value into specific type. @@ -781,7 +781,20 @@ impl Dynamic { /// ``` #[inline(always)] pub fn cast(self) -> T { - self.try_cast::().unwrap() + let self_type_name = if self.is_shared() { + // Avoid panics/deadlocks with shared values + "" + } else { + self.type_name() + }; + + self.try_cast::().unwrap_or_else(|| { + panic!( + "value is {} and cannot be cast to {}", + self_type_name, + type_name::() + ) + }) } /// Flatten the `Dynamic` and clone it.