Fix bug with casting from float.

This commit is contained in:
Stephen Chung 2020-04-13 23:31:05 +08:00
parent 20bc66c5f6
commit f600e59401

View File

@ -279,39 +279,21 @@ impl Dynamic {
/// assert_eq!(new_result.to_string(), "hello"); /// assert_eq!(new_result.to_string(), "hello");
/// ``` /// ```
pub fn from<T: Variant + Clone>(value: T) -> Self { pub fn from<T: Variant + Clone>(value: T) -> Self {
if let Some(result) = (&value as &dyn Variant) let dyn_value = &value as &dyn Variant;
.downcast_ref::<()>()
.cloned() if let Some(result) = dyn_value.downcast_ref::<()>().cloned().map(Union::Unit) {
.map(Union::Unit) return Self(result);
.or_else(|| { } else if let Some(result) = dyn_value.downcast_ref::<bool>().cloned().map(Union::Bool) {
(&value as &dyn Variant) return Self(result);
.downcast_ref::<bool>() } else if let Some(result) = dyn_value.downcast_ref::<INT>().cloned().map(Union::Int) {
.cloned() return Self(result);
.map(Union::Bool) } else if let Some(result) = dyn_value.downcast_ref::<char>().cloned().map(Union::Char) {
.or_else(|| {
(&value as &dyn Variant)
.downcast_ref::<INT>()
.cloned()
.map(Union::Int)
.or_else(|| {
(&value as &dyn Variant)
.downcast_ref::<char>()
.cloned()
.map(Union::Char)
})
})
})
{
return Self(result); return Self(result);
} }
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
{ {
if let Some(result) = (&value as &dyn Variant) if let Some(result) = dyn_value.downcast_ref::<FLOAT>().cloned().map(Union::Float) {
.downcast_ref::<char>()
.cloned()
.map(Union::Char)
{
return Self(result); return Self(result);
} }
} }
@ -372,6 +354,7 @@ impl Dynamic {
} }
/// Get a copy of the `Dynamic` value as a specific type. /// Get a copy of the `Dynamic` value as a specific type.
/// Casting to a `Dynamic` just returns as is.
/// ///
/// # Panics /// # Panics
/// ///
@ -391,8 +374,13 @@ impl Dynamic {
} }
/// Get a reference of a specific type to the `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. /// Returns `None` if the cast fails.
pub fn downcast_ref<T: Variant + Clone>(&self) -> Option<&T> { pub fn downcast_ref<T: Variant + Clone>(&self) -> Option<&T> {
if TypeId::of::<T>() == TypeId::of::<Dynamic>() {
return (self as &dyn Variant).downcast_ref::<T>();
}
match &self.0 { match &self.0 {
Union::Unit(value) => (value as &dyn Variant).downcast_ref::<T>(), Union::Unit(value) => (value as &dyn Variant).downcast_ref::<T>(),
Union::Bool(value) => (value as &dyn Variant).downcast_ref::<T>(), Union::Bool(value) => (value as &dyn Variant).downcast_ref::<T>(),
@ -408,8 +396,13 @@ impl Dynamic {
} }
/// Get a mutable reference of a specific type to the `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. /// Returns `None` if the cast fails.
pub fn downcast_mut<T: Variant + Clone>(&mut self) -> Option<&mut T> { pub fn downcast_mut<T: Variant + Clone>(&mut self) -> Option<&mut T> {
if TypeId::of::<T>() == TypeId::of::<Dynamic>() {
return (self as &mut dyn Variant).downcast_mut::<T>();
}
match &mut self.0 { match &mut self.0 {
Union::Unit(value) => (value as &mut dyn Variant).downcast_mut::<T>(), Union::Unit(value) => (value as &mut dyn Variant).downcast_mut::<T>(),
Union::Bool(value) => (value as &mut dyn Variant).downcast_mut::<T>(), Union::Bool(value) => (value as &mut dyn Variant).downcast_mut::<T>(),