diff --git a/src/any.rs b/src/any.rs index 3b42acf8..47da2e04 100644 --- a/src/any.rs +++ b/src/any.rs @@ -91,13 +91,13 @@ pub trait Variant: Any + Send + Sync { impl Variant for T { fn as_any(&self) -> &dyn Any { - self as &dyn Any + self } fn as_mut_any(&mut self) -> &mut dyn Any { - self as &mut dyn Any + self } fn as_box_any(self: Box) -> Box { - self as Box + self } fn type_name(&self) -> &'static str { type_name::() @@ -311,58 +311,52 @@ impl Dynamic { /// assert_eq!(new_result.to_string(), "hello"); /// ``` pub fn from(value: T) -> Self { - let dyn_value = &value as &dyn Any; - - if let Some(result) = dyn_value.downcast_ref::<()>().cloned().map(Union::Unit) { - return Self(result); - } else if let Some(result) = dyn_value.downcast_ref::().cloned().map(Union::Bool) { - return Self(result); - } else if let Some(result) = dyn_value.downcast_ref::().cloned().map(Union::Int) { - return Self(result); - } else if let Some(result) = dyn_value.downcast_ref::().cloned().map(Union::Char) { - return Self(result); - } else if let Some(result) = dyn_value - .downcast_ref::() - .cloned() - .map(Union::Str) - { - return Self(result); + if let Some(result) = ::downcast_ref::<()>(&value) { + return result.clone().into(); + } else if let Some(result) = ::downcast_ref::(&value) { + return result.clone().into(); + } else if let Some(result) = ::downcast_ref::(&value) { + return result.clone().into(); + } else if let Some(result) = ::downcast_ref::(&value) { + return result.clone().into(); + } else if let Some(result) = ::downcast_ref::(&value) { + return result.clone().into(); } #[cfg(not(feature = "no_float"))] { - if let Some(result) = dyn_value.downcast_ref::().cloned().map(Union::Float) { - return Self(result); + if let Some(result) = ::downcast_ref::(&value) { + return result.clone().into(); } } - let mut var = Box::new(value); + let mut boxed = Box::new(value); - var = match unsafe_cast_box::<_, Dynamic>(var) { + boxed = match unsafe_cast_box::<_, Dynamic>(boxed) { Ok(d) => return *d, - Err(var) => var, + Err(val) => val, }; - var = match unsafe_cast_box::<_, String>(var) { - Ok(s) => return Self(Union::Str(s.into())), - Err(var) => var, + boxed = match unsafe_cast_box::<_, String>(boxed) { + Ok(s) => return (*s).into(), + Err(val) => val, }; #[cfg(not(feature = "no_index"))] { - var = match unsafe_cast_box::<_, Array>(var) { - Ok(array) => return Self(Union::Array(array)), - Err(var) => var, + boxed = match unsafe_cast_box::<_, Array>(boxed) { + Ok(array) => return (*array).into(), + Err(val) => val, }; } #[cfg(not(feature = "no_object"))] { - var = match unsafe_cast_box::<_, Map>(var) { - Ok(map) => return Self(Union::Map(map)), - Err(var) => var, + boxed = match unsafe_cast_box::<_, Map>(boxed) { + Ok(map) => return (*map).into(), + Err(val) => val, } } - Self(Union::Variant(Box::new(var))) + Self(Union::Variant(Box::new(boxed))) } /// Get a copy of the `Dynamic` value as a specific type. @@ -454,24 +448,24 @@ impl Dynamic { /// Returns `None` if the cast fails. pub fn downcast_ref(&self) -> Option<&T> { if TypeId::of::() == TypeId::of::() { - return (self as &dyn Any).downcast_ref::(); + return ::downcast_ref::(self); } match &self.0 { - Union::Unit(value) => (value as &dyn Any).downcast_ref::(), - Union::Bool(value) => (value as &dyn Any).downcast_ref::(), + Union::Unit(value) => ::downcast_ref::(value), + Union::Bool(value) => ::downcast_ref::(value), Union::Str(value) => (value as &dyn Any) .downcast_ref::() - .or_else(|| (value.as_ref() as &dyn Any).downcast_ref::()), - Union::Char(value) => (value as &dyn Any).downcast_ref::(), - Union::Int(value) => (value as &dyn Any).downcast_ref::(), + .or_else(|| ::downcast_ref::(value.as_ref())), + Union::Char(value) => ::downcast_ref::(value), + Union::Int(value) => ::downcast_ref::(value), #[cfg(not(feature = "no_float"))] - Union::Float(value) => (value as &dyn Any).downcast_ref::(), + Union::Float(value) => ::downcast_ref::(value), #[cfg(not(feature = "no_index"))] - Union::Array(value) => (value.as_ref() as &dyn Any).downcast_ref::(), + Union::Array(value) => ::downcast_ref::(value.as_ref()), #[cfg(not(feature = "no_object"))] - Union::Map(value) => (value.as_ref() as &dyn Any).downcast_ref::(), - Union::Module(value) => (value.as_ref() as &dyn Any).downcast_ref::(), + Union::Map(value) => ::downcast_ref::(value.as_ref()), + Union::Module(value) => ::downcast_ref::(value.as_ref()), Union::Variant(value) => value.as_ref().as_ref().as_any().downcast_ref::(), } } @@ -481,22 +475,22 @@ impl Dynamic { /// Returns `None` if the cast fails. pub fn downcast_mut(&mut self) -> Option<&mut T> { if TypeId::of::() == TypeId::of::() { - return (self as &mut dyn Any).downcast_mut::(); + return ::downcast_mut::(self); } match &mut self.0 { - Union::Unit(value) => (value as &mut dyn Any).downcast_mut::(), - Union::Bool(value) => (value as &mut dyn Any).downcast_mut::(), - Union::Str(value) => (value as &mut dyn Any).downcast_mut::(), - Union::Char(value) => (value as &mut dyn Any).downcast_mut::(), - Union::Int(value) => (value as &mut dyn Any).downcast_mut::(), + Union::Unit(value) => ::downcast_mut::(value), + Union::Bool(value) => ::downcast_mut::(value), + Union::Str(value) => ::downcast_mut::(value), + Union::Char(value) => ::downcast_mut::(value), + Union::Int(value) => ::downcast_mut::(value), #[cfg(not(feature = "no_float"))] - Union::Float(value) => (value as &mut dyn Any).downcast_mut::(), + Union::Float(value) => ::downcast_mut::(value), #[cfg(not(feature = "no_index"))] - Union::Array(value) => (value.as_mut() as &mut dyn Any).downcast_mut::(), + Union::Array(value) => ::downcast_mut::(value.as_mut()), #[cfg(not(feature = "no_object"))] - Union::Map(value) => (value.as_mut() as &mut dyn Any).downcast_mut::(), - Union::Module(value) => (value.as_mut() as &mut dyn Any).downcast_mut::(), + Union::Map(value) => ::downcast_mut::(value.as_mut()), + Union::Module(value) => ::downcast_mut::(value.as_mut()), Union::Variant(value) => value.as_mut().as_mut_any().downcast_mut::(), } } @@ -590,16 +584,11 @@ impl From for Dynamic { Self(Union::Char(value)) } } -impl From for Dynamic { - fn from(value: String) -> Self { +impl> From for Dynamic { + fn from(value: S) -> Self { Self(Union::Str(value.into())) } } -impl From for Dynamic { - fn from(value: ImmutableString) -> Self { - Self(Union::Str(value)) - } -} #[cfg(not(feature = "no_index"))] impl From> for Dynamic { fn from(value: Vec) -> Self { @@ -617,8 +606,8 @@ impl From<&[T]> for Dynamic { } } #[cfg(not(feature = "no_object"))] -impl From> for Dynamic { - fn from(value: HashMap) -> Self { +impl, T: Variant + Clone> From> for Dynamic { + fn from(value: HashMap) -> Self { Self(Union::Map(Box::new( value .into_iter()