diff --git a/src/types/dynamic.rs b/src/types/dynamic.rs index 8c71de89..ff85dafa 100644 --- a/src/types/dynamic.rs +++ b/src/types/dynamic.rs @@ -455,11 +455,11 @@ impl Hash for Dynamic { #[cfg(feature = "decimal")] Union::Decimal(ref d, ..) => d.hash(state), #[cfg(not(feature = "no_index"))] - Union::Array(ref a, ..) => a.as_ref().hash(state), + Union::Array(ref a, ..) => a.hash(state), #[cfg(not(feature = "no_index"))] - Union::Blob(ref a, ..) => a.as_ref().hash(state), + Union::Blob(ref a, ..) => a.hash(state), #[cfg(not(feature = "no_object"))] - Union::Map(ref m, ..) => m.as_ref().hash(state), + Union::Map(ref m, ..) => m.hash(state), Union::FnPtr(ref f, ..) => f.hash(state), #[cfg(not(feature = "no_closure"))] @@ -470,51 +470,7 @@ impl Hash for Dynamic { #[cfg(feature = "sync")] Union::Shared(ref cell, ..) => (*cell.read().unwrap()).hash(state), - Union::Variant(ref _v, ..) => { - #[cfg(not(feature = "only_i32"))] - #[cfg(not(feature = "only_i64"))] - { - let value_any = (***_v).as_any(); - let type_id = value_any.type_id(); - - if type_id == TypeId::of::() { - TypeId::of::().hash(state); - value_any.downcast_ref::().expect(CHECKED).hash(state); - } else if type_id == TypeId::of::() { - TypeId::of::().hash(state); - value_any.downcast_ref::().expect(CHECKED).hash(state); - } else if type_id == TypeId::of::() { - TypeId::of::().hash(state); - value_any.downcast_ref::().expect(CHECKED).hash(state); - } else if type_id == TypeId::of::() { - TypeId::of::().hash(state); - value_any.downcast_ref::().expect(CHECKED).hash(state); - } else if type_id == TypeId::of::() { - TypeId::of::().hash(state); - value_any.downcast_ref::().expect(CHECKED).hash(state); - } else if type_id == TypeId::of::() { - TypeId::of::().hash(state); - value_any.downcast_ref::().expect(CHECKED).hash(state); - } else if type_id == TypeId::of::() { - TypeId::of::().hash(state); - value_any.downcast_ref::().expect(CHECKED).hash(state); - } else if type_id == TypeId::of::() { - TypeId::of::().hash(state); - value_any.downcast_ref::().expect(CHECKED).hash(state); - } - - #[cfg(not(target_family = "wasm"))] - if type_id == TypeId::of::() { - TypeId::of::().hash(state); - value_any.downcast_ref::().expect(CHECKED).hash(state); - } else if type_id == TypeId::of::() { - TypeId::of::().hash(state); - value_any.downcast_ref::().expect(CHECKED).hash(state); - } - } - - unimplemented!("a custom type cannot be hashed") - } + Union::Variant(..) => unimplemented!("{} cannot be hashed", self.type_name()), #[cfg(not(feature = "no_std"))] Union::TimeStamp(..) => unimplemented!("{} cannot be hashed", self.type_name()), @@ -550,49 +506,47 @@ impl fmt::Display for Dynamic { #[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i64"))] - if _type_id == TypeId::of::() { - return fmt::Display::fmt(_value_any.downcast_ref::().expect(CHECKED), f); - } else if _type_id == TypeId::of::() { - return fmt::Display::fmt(_value_any.downcast_ref::().expect(CHECKED), f); - } else if _type_id == TypeId::of::() { - return fmt::Display::fmt(_value_any.downcast_ref::().expect(CHECKED), f); - } else if _type_id == TypeId::of::() { - return fmt::Display::fmt(_value_any.downcast_ref::().expect(CHECKED), f); - } else if _type_id == TypeId::of::() { - return fmt::Display::fmt(_value_any.downcast_ref::().expect(CHECKED), f); - } else if _type_id == TypeId::of::() { - return fmt::Display::fmt(_value_any.downcast_ref::().expect(CHECKED), f); - } else if _type_id == TypeId::of::() { - return fmt::Display::fmt(_value_any.downcast_ref::().expect(CHECKED), f); - } else if _type_id == TypeId::of::() { - return fmt::Display::fmt(_value_any.downcast_ref::().expect(CHECKED), f); + if let Some(value) = _value_any.downcast_ref::() { + return fmt::Display::fmt(value, f); + } else if let Some(value) = _value_any.downcast_ref::() { + return fmt::Display::fmt(value, f); + } else if let Some(value) = _value_any.downcast_ref::() { + return fmt::Display::fmt(value, f); + } else if let Some(value) = _value_any.downcast_ref::() { + return fmt::Display::fmt(value, f); + } else if let Some(value) = _value_any.downcast_ref::() { + return fmt::Display::fmt(value, f); + } else if let Some(value) = _value_any.downcast_ref::() { + return fmt::Display::fmt(value, f); + } else if let Some(value) = _value_any.downcast_ref::() { + return fmt::Display::fmt(value, f); + } else if let Some(value) = _value_any.downcast_ref::() { + return fmt::Display::fmt(value, f); } #[cfg(not(feature = "no_float"))] #[cfg(not(feature = "f32_float"))] - if _type_id == TypeId::of::() { - return fmt::Display::fmt(_value_any.downcast_ref::().expect(CHECKED), f); + if let Some(value) = _value_any.downcast_ref::() { + return fmt::Display::fmt(value, f); } #[cfg(not(feature = "no_float"))] #[cfg(feature = "f32_float")] - if _type_id == TypeId::of::() { - return fmt::Display::fmt(_value_any.downcast_ref::().expect(CHECKED), f); + if let Some(value) = _value_any.downcast_ref::() { + return fmt::Display::fmt(value, f); } #[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i64"))] #[cfg(not(target_family = "wasm"))] - if _type_id == TypeId::of::() { - return fmt::Display::fmt(_value_any.downcast_ref::().expect(CHECKED), f); - } else if _type_id == TypeId::of::() { - return fmt::Display::fmt(_value_any.downcast_ref::().expect(CHECKED), f); + if let Some(value) = _value_any.downcast_ref::() { + return fmt::Display::fmt(value, f); + } else if let Some(value) = _value_any.downcast_ref::() { + return fmt::Display::fmt(value, f); } - if _type_id == TypeId::of::() { - let range = _value_any.downcast_ref::().expect(CHECKED); + if let Some(range) = _value_any.downcast_ref::() { return write!(f, "{}..{}", range.start, range.end); - } else if _type_id == TypeId::of::() { - let range = _value_any.downcast_ref::().expect(CHECKED); + } else if let Some(range) = _value_any.downcast_ref::() { return write!(f, "{}..={}", range.start(), range.end()); } @@ -655,49 +609,47 @@ impl fmt::Debug for Dynamic { #[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i64"))] - if _type_id == TypeId::of::() { - return fmt::Debug::fmt(_value_any.downcast_ref::().expect(CHECKED), f); - } else if _type_id == TypeId::of::() { - return fmt::Debug::fmt(_value_any.downcast_ref::().expect(CHECKED), f); - } else if _type_id == TypeId::of::() { - return fmt::Debug::fmt(_value_any.downcast_ref::().expect(CHECKED), f); - } else if _type_id == TypeId::of::() { - return fmt::Debug::fmt(_value_any.downcast_ref::().expect(CHECKED), f); - } else if _type_id == TypeId::of::() { - return fmt::Debug::fmt(_value_any.downcast_ref::().expect(CHECKED), f); - } else if _type_id == TypeId::of::() { - return fmt::Debug::fmt(_value_any.downcast_ref::().expect(CHECKED), f); - } else if _type_id == TypeId::of::() { - return fmt::Debug::fmt(_value_any.downcast_ref::().expect(CHECKED), f); - } else if _type_id == TypeId::of::() { - return fmt::Debug::fmt(_value_any.downcast_ref::().expect(CHECKED), f); + if let Some(value) = _value_any.downcast_ref::() { + return fmt::Debug::fmt(value, f); + } else if let Some(value) = _value_any.downcast_ref::() { + return fmt::Debug::fmt(value, f); + } else if let Some(value) = _value_any.downcast_ref::() { + return fmt::Debug::fmt(value, f); + } else if let Some(value) = _value_any.downcast_ref::() { + return fmt::Debug::fmt(value, f); + } else if let Some(value) = _value_any.downcast_ref::() { + return fmt::Debug::fmt(value, f); + } else if let Some(value) = _value_any.downcast_ref::() { + return fmt::Debug::fmt(value, f); + } else if let Some(value) = _value_any.downcast_ref::() { + return fmt::Debug::fmt(value, f); + } else if let Some(value) = _value_any.downcast_ref::() { + return fmt::Debug::fmt(value, f); } #[cfg(not(feature = "no_float"))] #[cfg(not(feature = "f32_float"))] - if _type_id == TypeId::of::() { - return fmt::Debug::fmt(_value_any.downcast_ref::().expect(CHECKED), f); + if let Some(value) = _value_any.downcast_ref::() { + return fmt::Debug::fmt(value, f); } #[cfg(not(feature = "no_float"))] #[cfg(feature = "f32_float")] - if _type_id == TypeId::of::() { - return fmt::Debug::fmt(_value_any.downcast_ref::().expect(CHECKED), f); + if let Some(value) = _value_any.downcast_ref::() { + return fmt::Debug::fmt(value, f); } #[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i64"))] #[cfg(not(target_family = "wasm"))] - if _type_id == TypeId::of::() { - return fmt::Debug::fmt(_value_any.downcast_ref::().expect(CHECKED), f); - } else if _type_id == TypeId::of::() { - return fmt::Debug::fmt(_value_any.downcast_ref::().expect(CHECKED), f); + if let Some(value) = _value_any.downcast_ref::() { + return fmt::Debug::fmt(value, f); + } else if let Some(value) = _value_any.downcast_ref::() { + return fmt::Debug::fmt(value, f); } - if _type_id == TypeId::of::() { - let range = _value_any.downcast_ref::().expect(CHECKED); + if let Some(range) = _value_any.downcast_ref::() { return write!(f, "{}..{}", range.start, range.end); - } else if _type_id == TypeId::of::() { - let range = _value_any.downcast_ref::().expect(CHECKED); + } else if let Some(range) = _value_any.downcast_ref::() { return write!(f, "{}..={}", range.start(), range.end()); }