From 97a8fd3d5b1143ec9334e740ec740b9987597220 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 8 Feb 2022 09:25:53 +0800 Subject: [PATCH] Improve reify syntax. --- src/api/custom_syntax.rs | 20 +-- src/func/call.rs | 14 +- src/func/register.rs | 25 ++-- src/reify.rs | 17 ++- src/types/dynamic.rs | 282 +++++++++++++++++++-------------------- 5 files changed, 184 insertions(+), 174 deletions(-) diff --git a/src/api/custom_syntax.rs b/src/api/custom_syntax.rs index c0f15ff4..01e0d523 100644 --- a/src/api/custom_syntax.rs +++ b/src/api/custom_syntax.rs @@ -72,8 +72,8 @@ impl Expression<'_> { pub fn get_string_value(&self) -> Option<&str> { match self.0 { #[cfg(not(feature = "no_module"))] - Expr::Variable(.., _, x) if x.1.is_some() => None, - Expr::Variable(.., _, x) => Some(x.2.as_str()), + Expr::Variable(.., x) if x.1.is_some() => None, + Expr::Variable(.., x) => Some(x.2.as_str()), Expr::StringConstant(x, ..) => Some(x.as_str()), _ => None, } @@ -94,19 +94,19 @@ impl Expression<'_> { pub fn get_literal_value(&self) -> Option { // Coded this way in order to maximally leverage potentials for dead-code removal. match self.0 { - Expr::IntegerConstant(x, ..) => reify!(*x, |x: T| Some(x), || None), + Expr::IntegerConstant(x, ..) => reify!(*x => Option), #[cfg(not(feature = "no_float"))] - Expr::FloatConstant(x, ..) => reify!(*x, |x: T| Some(x), || None), + Expr::FloatConstant(x, ..) => reify!(*x => Option), - Expr::CharConstant(x, ..) => reify!(*x, |x: T| Some(x), || None), - Expr::StringConstant(x, ..) => reify!(x.clone(), |x: T| Some(x), || None), - Expr::Variable(.., _, x) => { + Expr::CharConstant(x, ..) => reify!(*x => Option), + Expr::StringConstant(x, ..) => reify!(x.clone() => Option), + Expr::Variable(.., x) => { let x: ImmutableString = x.2.clone().into(); - reify!(x, |x: T| Some(x), || None) + reify!(x => Option) } - Expr::BoolConstant(x, ..) => reify!(*x, |x: T| Some(x), || None), - Expr::Unit(_) => reify!((), |x: T| Some(x), || None), + Expr::BoolConstant(x, ..) => reify!(*x => Option), + Expr::Unit(..) => reify!(() => Option), _ => None, } diff --git a/src/func/call.rs b/src/func/call.rs index 56311890..bb19eda7 100644 --- a/src/func/call.rs +++ b/src/func/call.rs @@ -46,11 +46,13 @@ impl<'a> ArgBackup<'a> { /// This function replaces the first argument of a method call with a clone copy. /// This is to prevent a pure function unintentionally consuming the first argument. /// - /// `restore_first_arg` must be called before the end of the scope to prevent the shorter lifetime from leaking. + /// `restore_first_arg` must be called before the end of the scope to prevent the shorter + /// lifetime from leaking. /// /// # Safety /// - /// This method blindly casts a reference to another lifetime, which saves allocation and string cloning. + /// This method blindly casts a reference to another lifetime, which saves allocation and + /// string cloning. /// /// As long as `restore_first_arg` is called before the end of the scope, the shorter lifetime /// will not leak. @@ -71,8 +73,8 @@ impl<'a> ArgBackup<'a> { // Blindly casting a reference to another lifetime saves allocation and string cloning, // but must be used with the utmost care. // - // We can do this here because, before the end of this scope, we'd restore the original reference - // via `restore_first_arg`. Therefore this shorter lifetime does not leak. + // We can do this here because, before the end of this scope, we'd restore the original + // reference via `restore_first_arg`. Therefore this shorter lifetime does not leak. self.orig_mut = Some(mem::replace(&mut args[0], unsafe { mem::transmute(&mut self.value_copy) })); @@ -81,8 +83,8 @@ impl<'a> ArgBackup<'a> { /// /// # Safety /// - /// If `change_first_arg_to_copy` has been called, this function **MUST** be called _BEFORE_ exiting - /// the current scope. Otherwise it is undefined behavior as the shorter lifetime will leak. + /// If `change_first_arg_to_copy` has been called, this function **MUST** be called _BEFORE_ + /// exiting the current scope. Otherwise it is undefined behavior as the shorter lifetime will leak. #[inline(always)] fn restore_first_arg(mut self, args: &mut FnCallArgs<'a>) { if let Some(p) = self.orig_mut.take() { diff --git a/src/func/register.rs b/src/func/register.rs index b87e0bec..eab4000d 100644 --- a/src/func/register.rs +++ b/src/func/register.rs @@ -38,25 +38,26 @@ pub fn by_ref(data: &mut Dynamic) -> DynamicWriteLock { } /// Dereference into value. -#[inline] +#[inline(always)] #[must_use] pub fn by_value(data: &mut Dynamic) -> T { if TypeId::of::() == TypeId::of::<&str>() { // If T is `&str`, data must be `ImmutableString`, so map directly to it data.flatten_in_place(); let ref_str = data.as_str_ref().expect("&str"); - //let ref_t = reify!(&ref_str, |ref_t: &T| ref_t, || unreachable!()); - let ref_t = unsafe { mem::transmute::<_, &T>(&ref_str) }; - ref_t.clone() - } else if TypeId::of::() == TypeId::of::() { - // If T is `String`, data must be `ImmutableString`, so map directly to it - let t = mem::take(data).into_string().expect("`ImmutableString`"); - reify!(t, |t: T| t, || unreachable!()) - } else { - // We consume the argument and then replace it with () - the argument is not supposed to be used again. - // This way, we avoid having to clone the argument again, because it is already a clone when passed here. - mem::take(data).cast::() + // # Safety + // + // We already checked that `T` is `&str`, so it is safe to cast here. + return unsafe { std::mem::transmute_copy::<_, T>(&ref_str) }; } + if TypeId::of::() == TypeId::of::() { + // If T is `String`, data must be `ImmutableString`, so map directly to it + return reify!(mem::take(data).into_string().expect("`ImmutableString`") => T); + } + + // We consume the argument and then replace it with () - the argument is not supposed to be used again. + // This way, we avoid having to clone the argument again, because it is already a clone when passed here. + return mem::take(data).cast::(); } /// Trait to register custom Rust functions. diff --git a/src/reify.rs b/src/reify.rs index e0ac8481..e582b5cb 100644 --- a/src/reify.rs +++ b/src/reify.rs @@ -4,9 +4,6 @@ #[macro_export] macro_rules! reify { ($old:ident, |$new:ident : $t:ty| $code:expr, || $fallback:expr) => {{ - #[allow(unused_imports)] - use std::any::Any; - if std::any::TypeId::of::<$t>() == std::any::Any::type_id(&$old) { // SAFETY: This is safe because we check to make sure the two types are // actually the same type. @@ -26,4 +23,18 @@ macro_rules! reify { ($old:expr, |$new:ident : $t:ty| $code:expr) => { reify!($old, |$new: $t| $code, || ()) }; + + ($old:ident => Option<$t:ty>) => { + reify!($old, |v: $t| Some(v), || None) + }; + ($old:expr => Option<$t:ty>) => { + reify!($old, |v: $t| Some(v), || None) + }; + + ($old:ident => $t:ty) => { + reify!($old, |v: $t| v, || unreachable!()) + }; + ($old:expr => $t:ty) => { + reify!($old, |v: $t| v, || unreachable!()) + }; } diff --git a/src/types/dynamic.rs b/src/types/dynamic.rs index d0a4d56a..08a89729 100644 --- a/src/types/dynamic.rs +++ b/src/types/dynamic.rs @@ -399,15 +399,15 @@ impl Dynamic { #[cfg(not(feature = "no_std"))] Union::TimeStamp(..) => TypeId::of::(), - Union::Variant(ref v, _, ..) => (***v).type_id(), + Union::Variant(ref v, ..) => (***v).type_id(), #[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "sync"))] - Union::Shared(ref cell, _, ..) => (*cell.borrow()).type_id(), + Union::Shared(ref cell, ..) => (*cell.borrow()).type_id(), #[cfg(not(feature = "no_closure"))] #[cfg(feature = "sync")] - Union::Shared(ref cell, _, ..) => (*cell.read().unwrap()).type_id(), + Union::Shared(ref cell, ..) => (*cell.read().unwrap()).type_id(), } } /// Get the name of the type of the value held by this [`Dynamic`]. @@ -438,17 +438,17 @@ impl Dynamic { #[cfg(not(feature = "no_std"))] Union::TimeStamp(..) => "timestamp", - Union::Variant(ref v, _, ..) => (***v).type_name(), + Union::Variant(ref v, ..) => (***v).type_name(), #[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "sync"))] - Union::Shared(ref cell, _, ..) => cell + Union::Shared(ref cell, ..) => cell .try_borrow() .map(|v| (*v).type_name()) .unwrap_or(""), #[cfg(not(feature = "no_closure"))] #[cfg(feature = "sync")] - Union::Shared(ref cell, _, ..) => (*cell.read().unwrap()).type_name(), + Union::Shared(ref cell, ..) => (*cell.read().unwrap()).type_name(), } } } @@ -464,31 +464,31 @@ impl Hash for Dynamic { match self.0 { Union::Unit(..) => ().hash(state), - Union::Bool(ref b, _, ..) => b.hash(state), - Union::Str(ref s, _, ..) => s.hash(state), - Union::Char(ref c, _, ..) => c.hash(state), - Union::Int(ref i, _, ..) => i.hash(state), + Union::Bool(ref b, ..) => b.hash(state), + Union::Str(ref s, ..) => s.hash(state), + Union::Char(ref c, ..) => c.hash(state), + Union::Int(ref i, ..) => i.hash(state), #[cfg(not(feature = "no_float"))] - Union::Float(ref f, _, ..) => f.hash(state), + Union::Float(ref f, ..) => f.hash(state), #[cfg(feature = "decimal")] - Union::Decimal(ref d, _, ..) => d.hash(state), + 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.as_ref().hash(state), #[cfg(not(feature = "no_index"))] - Union::Blob(ref a, _, ..) => a.as_ref().hash(state), + Union::Blob(ref a, ..) => a.as_ref().hash(state), #[cfg(not(feature = "no_object"))] - Union::Map(ref m, _, ..) => m.as_ref().hash(state), - Union::FnPtr(ref f, _, ..) => f.hash(state), + Union::Map(ref m, ..) => m.as_ref().hash(state), + Union::FnPtr(ref f, ..) => f.hash(state), #[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "sync"))] - Union::Shared(ref cell, _, ..) => (*cell.borrow()).hash(state), + Union::Shared(ref cell, ..) => (*cell.borrow()).hash(state), #[cfg(not(feature = "no_closure"))] #[cfg(feature = "sync")] - Union::Shared(ref cell, _, ..) => (*cell.read().unwrap()).hash(state), + Union::Shared(ref cell, ..) => (*cell.read().unwrap()).hash(state), - Union::Variant(ref v, _, ..) => { + Union::Variant(ref v, ..) => { let _v = v; #[cfg(not(feature = "only_i32"))] @@ -546,25 +546,25 @@ impl fmt::Display for Dynamic { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self.0 { Union::Unit(..) => write!(f, ""), - Union::Bool(ref v, _, ..) => fmt::Display::fmt(v, f), - Union::Str(ref v, _, ..) => fmt::Display::fmt(v, f), - Union::Char(ref v, _, ..) => fmt::Display::fmt(v, f), - Union::Int(ref v, _, ..) => fmt::Display::fmt(v, f), + Union::Bool(ref v, ..) => fmt::Display::fmt(v, f), + Union::Str(ref v, ..) => fmt::Display::fmt(v, f), + Union::Char(ref v, ..) => fmt::Display::fmt(v, f), + Union::Int(ref v, ..) => fmt::Display::fmt(v, f), #[cfg(not(feature = "no_float"))] - Union::Float(ref v, _, ..) => fmt::Display::fmt(v, f), + Union::Float(ref v, ..) => fmt::Display::fmt(v, f), #[cfg(feature = "decimal")] - Union::Decimal(ref v, _, ..) => fmt::Display::fmt(v, f), + Union::Decimal(ref v, ..) => fmt::Display::fmt(v, f), #[cfg(not(feature = "no_index"))] Union::Array(..) => fmt::Debug::fmt(self, f), #[cfg(not(feature = "no_index"))] Union::Blob(..) => fmt::Debug::fmt(self, f), #[cfg(not(feature = "no_object"))] Union::Map(..) => fmt::Debug::fmt(self, f), - Union::FnPtr(ref v, _, ..) => fmt::Display::fmt(v, f), + Union::FnPtr(ref v, ..) => fmt::Display::fmt(v, f), #[cfg(not(feature = "no_std"))] Union::TimeStamp(..) => f.write_str(""), - Union::Variant(ref v, _, ..) => { + Union::Variant(ref v, ..) => { let _value_any = (***v).as_any(); let _type_id = _value_any.type_id(); @@ -621,7 +621,7 @@ impl fmt::Display for Dynamic { #[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "sync"))] - Union::Shared(ref cell, _, ..) => { + Union::Shared(ref cell, ..) => { if let Ok(v) = cell.try_borrow() { fmt::Display::fmt(&*v, f) } else { @@ -630,7 +630,7 @@ impl fmt::Display for Dynamic { } #[cfg(not(feature = "no_closure"))] #[cfg(feature = "sync")] - Union::Shared(ref cell, _, ..) => fmt::Display::fmt(&*cell.read().unwrap(), f), + Union::Shared(ref cell, ..) => fmt::Display::fmt(&*cell.read().unwrap(), f), } } } @@ -638,19 +638,19 @@ impl fmt::Display for Dynamic { impl fmt::Debug for Dynamic { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self.0 { - Union::Unit(ref v, _, ..) => fmt::Debug::fmt(v, f), - Union::Bool(ref v, _, ..) => fmt::Debug::fmt(v, f), - Union::Str(ref v, _, ..) => fmt::Debug::fmt(v, f), - Union::Char(ref v, _, ..) => fmt::Debug::fmt(v, f), - Union::Int(ref v, _, ..) => fmt::Debug::fmt(v, f), + Union::Unit(ref v, ..) => fmt::Debug::fmt(v, f), + Union::Bool(ref v, ..) => fmt::Debug::fmt(v, f), + Union::Str(ref v, ..) => fmt::Debug::fmt(v, f), + Union::Char(ref v, ..) => fmt::Debug::fmt(v, f), + Union::Int(ref v, ..) => fmt::Debug::fmt(v, f), #[cfg(not(feature = "no_float"))] - Union::Float(ref v, _, ..) => fmt::Debug::fmt(v, f), + Union::Float(ref v, ..) => fmt::Debug::fmt(v, f), #[cfg(feature = "decimal")] - Union::Decimal(ref v, _, ..) => fmt::Debug::fmt(v, f), + Union::Decimal(ref v, ..) => fmt::Debug::fmt(v, f), #[cfg(not(feature = "no_index"))] - Union::Array(ref v, _, ..) => fmt::Debug::fmt(v, f), + Union::Array(ref v, ..) => fmt::Debug::fmt(v, f), #[cfg(not(feature = "no_index"))] - Union::Blob(ref v, _, ..) => { + Union::Blob(ref v, ..) => { f.write_str("[")?; v.iter().enumerate().try_for_each(|(i, v)| { if i > 0 && i % 8 == 0 { @@ -661,15 +661,15 @@ impl fmt::Debug for Dynamic { f.write_str("]") } #[cfg(not(feature = "no_object"))] - Union::Map(ref v, _, ..) => { + Union::Map(ref v, ..) => { f.write_str("#")?; fmt::Debug::fmt(v, f) } - Union::FnPtr(ref v, _, ..) => fmt::Debug::fmt(v, f), + Union::FnPtr(ref v, ..) => fmt::Debug::fmt(v, f), #[cfg(not(feature = "no_std"))] Union::TimeStamp(..) => write!(f, ""), - Union::Variant(ref v, _, ..) => { + Union::Variant(ref v, ..) => { let _value_any = (***v).as_any(); let _type_id = _value_any.type_id(); @@ -726,7 +726,7 @@ impl fmt::Debug for Dynamic { #[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "sync"))] - Union::Shared(ref cell, _, ..) => { + Union::Shared(ref cell, ..) => { if let Ok(v) = cell.try_borrow() { write!(f, "{:?} (shared)", *v) } else { @@ -735,7 +735,7 @@ impl fmt::Debug for Dynamic { } #[cfg(not(feature = "no_closure"))] #[cfg(feature = "sync")] - Union::Shared(ref cell, _, ..) => fmt::Debug::fmt(&*cell.read().unwrap(), f), + Union::Shared(ref cell, ..) => fmt::Debug::fmt(&*cell.read().unwrap(), f), } } } @@ -1010,43 +1010,43 @@ impl Dynamic { #[must_use] pub(crate) const fn access_mode(&self) -> AccessMode { match self.0 { - Union::Unit(.., _, access) - | Union::Bool(.., _, access) - | Union::Str(.., _, access) - | Union::Char(.., _, access) - | Union::Int(.., _, access) - | Union::FnPtr(.., _, access) - | Union::Variant(.., _, access) => access, + Union::Unit(.., access) + | Union::Bool(.., access) + | Union::Str(.., access) + | Union::Char(.., access) + | Union::Int(.., access) + | Union::FnPtr(.., access) + | Union::Variant(.., access) => access, #[cfg(not(feature = "no_float"))] - Union::Float(.., _, access) => access, + Union::Float(.., access) => access, #[cfg(feature = "decimal")] - Union::Decimal(.., _, access) => access, + Union::Decimal(.., access) => access, #[cfg(not(feature = "no_index"))] - Union::Array(.., _, access) | Union::Blob(.., _, access) => access, + Union::Array(.., access) | Union::Blob(.., access) => access, #[cfg(not(feature = "no_object"))] - Union::Map(.., _, access) => access, + Union::Map(.., access) => access, #[cfg(not(feature = "no_std"))] - Union::TimeStamp(.., _, access) => access, + Union::TimeStamp(.., access) => access, #[cfg(not(feature = "no_closure"))] - Union::Shared(.., _, access) => access, + Union::Shared(.., access) => access, } } /// Set the [`AccessMode`] for this [`Dynamic`]. pub(crate) fn set_access_mode(&mut self, typ: AccessMode) -> &mut Self { match self.0 { - Union::Unit(.., _, ref mut access) - | Union::Bool(.., _, ref mut access) - | Union::Str(.., _, ref mut access) - | Union::Char(.., _, ref mut access) - | Union::Int(.., _, ref mut access) - | Union::FnPtr(.., _, ref mut access) - | Union::Variant(.., _, ref mut access) => *access = typ, + Union::Unit(.., ref mut access) + | Union::Bool(.., ref mut access) + | Union::Str(.., ref mut access) + | Union::Char(.., ref mut access) + | Union::Int(.., ref mut access) + | Union::FnPtr(.., ref mut access) + | Union::Variant(.., ref mut access) => *access = typ, #[cfg(not(feature = "no_float"))] - Union::Float(.., _, ref mut access) => *access = typ, + Union::Float(.., ref mut access) => *access = typ, #[cfg(feature = "decimal")] - Union::Decimal(.., _, ref mut access) => *access = typ, + Union::Decimal(.., ref mut access) => *access = typ, #[cfg(not(feature = "no_index"))] Union::Array(ref mut a, _, ref mut access) => { *access = typ; @@ -1055,7 +1055,7 @@ impl Dynamic { } } #[cfg(not(feature = "no_index"))] - Union::Blob(.., _, ref mut access) => *access = typ, + Union::Blob(.., ref mut access) => *access = typ, #[cfg(not(feature = "no_object"))] Union::Map(ref mut m, _, ref mut access) => { *access = typ; @@ -1064,9 +1064,9 @@ impl Dynamic { } } #[cfg(not(feature = "no_std"))] - Union::TimeStamp(.., _, ref mut access) => *access = typ, + Union::TimeStamp(.., ref mut access) => *access = typ, #[cfg(not(feature = "no_closure"))] - Union::Shared(.., _, ref mut access) => *access = typ, + Union::Shared(.., ref mut access) => *access = typ, } self } @@ -1081,17 +1081,17 @@ impl Dynamic { pub fn is_read_only(&self) -> bool { #[cfg(not(feature = "no_closure"))] match self.0 { - Union::Shared(.., _, ReadOnly) => return true, + Union::Shared(.., ReadOnly) => return true, #[cfg(not(feature = "sync"))] - Union::Shared(ref cell, _, ..) => { + Union::Shared(ref cell, ..) => { return match cell.borrow().access_mode() { ReadWrite => false, ReadOnly => true, } } #[cfg(feature = "sync")] - Union::Shared(ref cell, _, ..) => { + Union::Shared(ref cell, ..) => { return match cell.read().unwrap().access_mode() { ReadWrite => false, ReadOnly => true, @@ -1125,11 +1125,11 @@ impl Dynamic { #[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "sync"))] - Union::Shared(ref cell, _, ..) => cell.borrow().is_hashable(), + Union::Shared(ref cell, ..) => cell.borrow().is_hashable(), #[cfg(not(feature = "no_closure"))] #[cfg(feature = "sync")] - Union::Shared(ref cell, _, ..) => cell.read().unwrap().is_hashable(), + Union::Shared(ref cell, ..) => cell.read().unwrap().is_hashable(), _ => false, } @@ -1273,33 +1273,29 @@ impl Dynamic { reify!(self, |v: T| return Some(v)); match self.0 { - Union::Int(v, ..) => reify!(v, |v: T| Some(v), || None), + Union::Int(v, ..) => reify!(v => Option), #[cfg(not(feature = "no_float"))] - Union::Float(v, ..) => reify!(*v, |v: T| Some(v), || None), + Union::Float(v, ..) => reify!(*v => Option), #[cfg(feature = "decimal")] - Union::Decimal(v, ..) => reify!(*v, |v: T| Some(v), || None), - Union::Bool(v, ..) => reify!(v, |v: T| Some(v), || None), + Union::Decimal(v, ..) => reify!(*v => Option), + Union::Bool(v, ..) => reify!(v => Option), Union::Str(v, ..) => { - reify!(v, |v: T| Some(v), || { - reify!(v.to_string(), |v: T| Some(v), || None) - }) + reify!(v, |v: T| Some(v), || reify!(v.to_string() => Option)) } - Union::Char(v, ..) => reify!(v, |v: T| Some(v), || None), + Union::Char(v, ..) => reify!(v => Option), #[cfg(not(feature = "no_index"))] - Union::Array(v, ..) => reify!(v, |v: Box| Some(*v), || None), + Union::Array(v, ..) => reify!(*v => Option), #[cfg(not(feature = "no_index"))] - Union::Blob(v, ..) => reify!(v, |v: Box| Some(*v), || None), + Union::Blob(v, ..) => reify!(*v => Option), #[cfg(not(feature = "no_object"))] - Union::Map(v, ..) => reify!(v, |v: Box| Some(*v), || None), - Union::FnPtr(v, ..) => reify!(v, |v: Box| Some(*v), || None), + Union::Map(v, ..) => reify!(*v => Option), + Union::FnPtr(v, ..) => reify!(*v => Option), #[cfg(not(feature = "no_std"))] - Union::TimeStamp(v, ..) => reify!(v, |v: Box| Some(*v), || None), - Union::Unit(..) => reify!((), |v: T| Some(v), || None), - Union::Variant(v, _, ..) => (*v).as_boxed_any().downcast().ok().map(|x| *x), + Union::TimeStamp(v, ..) => reify!(*v => Option), + Union::Unit(v, ..) => reify!(v => Option), + Union::Variant(v, ..) => (*v).as_boxed_any().downcast().ok().map(|x| *x), #[cfg(not(feature = "no_closure"))] - Union::Shared(..) => { - unreachable!("Union::Shared case should be already handled") - } + Union::Shared(..) => unreachable!("Union::Shared case should be already handled"), } } /// Convert the [`Dynamic`] value into a specific type. @@ -1384,10 +1380,10 @@ impl Dynamic { match self.0 { #[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "sync"))] - Union::Shared(ref cell, _, ..) => cell.borrow().clone(), + Union::Shared(ref cell, ..) => cell.borrow().clone(), #[cfg(not(feature = "no_closure"))] #[cfg(feature = "sync")] - Union::Shared(ref cell, _, ..) => cell.read().unwrap().clone(), + Union::Shared(ref cell, ..) => cell.read().unwrap().clone(), _ => self.clone(), } } @@ -1402,7 +1398,7 @@ impl Dynamic { pub fn flatten(self) -> Self { match self.0 { #[cfg(not(feature = "no_closure"))] - Union::Shared(cell, _, ..) => crate::func::native::shared_try_take(cell).map_or_else( + Union::Shared(cell, ..) => crate::func::native::shared_try_take(cell).map_or_else( #[cfg(not(feature = "sync"))] |cell| cell.borrow().clone(), #[cfg(feature = "sync")] @@ -1425,7 +1421,7 @@ impl Dynamic { pub(crate) fn flatten_in_place(&mut self) -> &mut Self { match self.0 { #[cfg(not(feature = "no_closure"))] - Union::Shared(ref mut cell, _, ..) => { + Union::Shared(ref mut cell, ..) => { let cell = mem::take(cell); *self = crate::func::native::shared_try_take(cell).map_or_else( #[cfg(not(feature = "sync"))] @@ -1457,7 +1453,7 @@ impl Dynamic { pub fn is_locked(&self) -> bool { #[cfg(not(feature = "no_closure"))] match self.0 { - Union::Shared(ref _cell, _, ..) => { + Union::Shared(ref _cell, ..) => { #[cfg(not(feature = "sync"))] return _cell.try_borrow().is_err(); #[cfg(feature = "sync")] @@ -1482,7 +1478,7 @@ impl Dynamic { pub fn read_lock(&self) -> Option> { match self.0 { #[cfg(not(feature = "no_closure"))] - Union::Shared(ref cell, _, ..) => { + Union::Shared(ref cell, ..) => { #[cfg(not(feature = "sync"))] let value = cell.borrow(); #[cfg(feature = "sync")] @@ -1517,7 +1513,7 @@ impl Dynamic { pub fn write_lock(&mut self) -> Option> { match self.0 { #[cfg(not(feature = "no_closure"))] - Union::Shared(ref cell, _, ..) => { + Union::Shared(ref cell, ..) => { let guard = crate::func::native::locked_write(cell); if (*guard).type_id() != TypeId::of::() @@ -1546,79 +1542,79 @@ impl Dynamic { if TypeId::of::() == TypeId::of::() { return match self.0 { - Union::Int(ref v, _, ..) => v.as_any().downcast_ref::(), + Union::Int(ref v, ..) => v.as_any().downcast_ref::(), _ => None, }; } #[cfg(not(feature = "no_float"))] if TypeId::of::() == TypeId::of::() { return match self.0 { - Union::Float(ref v, _, ..) => v.as_ref().as_any().downcast_ref::(), + Union::Float(ref v, ..) => v.as_ref().as_any().downcast_ref::(), _ => None, }; } #[cfg(feature = "decimal")] if TypeId::of::() == TypeId::of::() { return match self.0 { - Union::Decimal(ref v, _, ..) => v.as_ref().as_any().downcast_ref::(), + Union::Decimal(ref v, ..) => v.as_ref().as_any().downcast_ref::(), _ => None, }; } if TypeId::of::() == TypeId::of::() { return match self.0 { - Union::Bool(ref v, _, ..) => v.as_any().downcast_ref::(), + Union::Bool(ref v, ..) => v.as_any().downcast_ref::(), _ => None, }; } if TypeId::of::() == TypeId::of::() { return match self.0 { - Union::Str(ref v, _, ..) => v.as_any().downcast_ref::(), + Union::Str(ref v, ..) => v.as_any().downcast_ref::(), _ => None, }; } if TypeId::of::() == TypeId::of::() { return match self.0 { - Union::Char(ref v, _, ..) => v.as_any().downcast_ref::(), + Union::Char(ref v, ..) => v.as_any().downcast_ref::(), _ => None, }; } #[cfg(not(feature = "no_index"))] if TypeId::of::() == TypeId::of::() { return match self.0 { - Union::Array(ref v, _, ..) => v.as_ref().as_any().downcast_ref::(), + Union::Array(ref v, ..) => v.as_ref().as_any().downcast_ref::(), _ => None, }; } #[cfg(not(feature = "no_index"))] if TypeId::of::() == TypeId::of::() { return match self.0 { - Union::Blob(ref v, _, ..) => v.as_ref().as_any().downcast_ref::(), + Union::Blob(ref v, ..) => v.as_ref().as_any().downcast_ref::(), _ => None, }; } #[cfg(not(feature = "no_object"))] if TypeId::of::() == TypeId::of::() { return match self.0 { - Union::Map(ref v, _, ..) => v.as_ref().as_any().downcast_ref::(), + Union::Map(ref v, ..) => v.as_ref().as_any().downcast_ref::(), _ => None, }; } if TypeId::of::() == TypeId::of::() { return match self.0 { - Union::FnPtr(ref v, _, ..) => v.as_ref().as_any().downcast_ref::(), + Union::FnPtr(ref v, ..) => v.as_ref().as_any().downcast_ref::(), _ => None, }; } #[cfg(not(feature = "no_std"))] if TypeId::of::() == TypeId::of::() { return match self.0 { - Union::TimeStamp(ref v, _, ..) => v.as_ref().as_any().downcast_ref::(), + Union::TimeStamp(ref v, ..) => v.as_ref().as_any().downcast_ref::(), _ => None, }; } if TypeId::of::() == TypeId::of::<()>() { return match self.0 { - Union::Unit(ref v, _, ..) => v.as_any().downcast_ref::(), + Union::Unit(ref v, ..) => v.as_any().downcast_ref::(), _ => None, }; } @@ -1627,7 +1623,7 @@ impl Dynamic { } match self.0 { - Union::Variant(ref v, _, ..) => (***v).as_any().downcast_ref::(), + Union::Variant(ref v, ..) => (***v).as_any().downcast_ref::(), #[cfg(not(feature = "no_closure"))] Union::Shared(..) => None, _ => None, @@ -1644,79 +1640,79 @@ impl Dynamic { if TypeId::of::() == TypeId::of::() { return match self.0 { - Union::Int(ref mut v, _, ..) => v.as_any_mut().downcast_mut::(), + Union::Int(ref mut v, ..) => v.as_any_mut().downcast_mut::(), _ => None, }; } #[cfg(not(feature = "no_float"))] if TypeId::of::() == TypeId::of::() { return match self.0 { - Union::Float(ref mut v, _, ..) => v.as_mut().as_any_mut().downcast_mut::(), + Union::Float(ref mut v, ..) => v.as_mut().as_any_mut().downcast_mut::(), _ => None, }; } #[cfg(feature = "decimal")] if TypeId::of::() == TypeId::of::() { return match self.0 { - Union::Decimal(ref mut v, _, ..) => v.as_mut().as_any_mut().downcast_mut::(), + Union::Decimal(ref mut v, ..) => v.as_mut().as_any_mut().downcast_mut::(), _ => None, }; } if TypeId::of::() == TypeId::of::() { return match self.0 { - Union::Bool(ref mut v, _, ..) => v.as_any_mut().downcast_mut::(), + Union::Bool(ref mut v, ..) => v.as_any_mut().downcast_mut::(), _ => None, }; } if TypeId::of::() == TypeId::of::() { return match self.0 { - Union::Str(ref mut v, _, ..) => v.as_any_mut().downcast_mut::(), + Union::Str(ref mut v, ..) => v.as_any_mut().downcast_mut::(), _ => None, }; } if TypeId::of::() == TypeId::of::() { return match self.0 { - Union::Char(ref mut v, _, ..) => v.as_any_mut().downcast_mut::(), + Union::Char(ref mut v, ..) => v.as_any_mut().downcast_mut::(), _ => None, }; } #[cfg(not(feature = "no_index"))] if TypeId::of::() == TypeId::of::() { return match self.0 { - Union::Array(ref mut v, _, ..) => v.as_mut().as_any_mut().downcast_mut::(), + Union::Array(ref mut v, ..) => v.as_mut().as_any_mut().downcast_mut::(), _ => None, }; } #[cfg(not(feature = "no_index"))] if TypeId::of::() == TypeId::of::() { return match self.0 { - Union::Blob(ref mut v, _, ..) => v.as_mut().as_any_mut().downcast_mut::(), + Union::Blob(ref mut v, ..) => v.as_mut().as_any_mut().downcast_mut::(), _ => None, }; } #[cfg(not(feature = "no_object"))] if TypeId::of::() == TypeId::of::() { return match self.0 { - Union::Map(ref mut v, _, ..) => v.as_mut().as_any_mut().downcast_mut::(), + Union::Map(ref mut v, ..) => v.as_mut().as_any_mut().downcast_mut::(), _ => None, }; } if TypeId::of::() == TypeId::of::() { return match self.0 { - Union::FnPtr(ref mut v, _, ..) => v.as_mut().as_any_mut().downcast_mut::(), + Union::FnPtr(ref mut v, ..) => v.as_mut().as_any_mut().downcast_mut::(), _ => None, }; } #[cfg(not(feature = "no_std"))] if TypeId::of::() == TypeId::of::() { return match self.0 { - Union::TimeStamp(ref mut v, _, ..) => v.as_mut().as_any_mut().downcast_mut::(), + Union::TimeStamp(ref mut v, ..) => v.as_mut().as_any_mut().downcast_mut::(), _ => None, }; } if TypeId::of::() == TypeId::of::<()>() { return match self.0 { - Union::Unit(ref mut v, _, ..) => v.as_any_mut().downcast_mut::(), + Union::Unit(ref mut v, ..) => v.as_any_mut().downcast_mut::(), _ => None, }; } @@ -1725,7 +1721,7 @@ impl Dynamic { } match self.0 { - Union::Variant(ref mut v, _, ..) => (***v).as_any_mut().downcast_mut::(), + Union::Variant(ref mut v, ..) => (***v).as_any_mut().downcast_mut::(), #[cfg(not(feature = "no_closure"))] Union::Shared(..) => None, _ => None, @@ -1736,7 +1732,7 @@ impl Dynamic { #[inline] pub fn as_unit(&self) -> Result<(), &'static str> { match self.0 { - Union::Unit(v, _, ..) => Ok(v), + Union::Unit(v, ..) => Ok(v), #[cfg(not(feature = "no_closure"))] Union::Shared(..) => self.read_lock().map(|v| *v).ok_or_else(|| self.type_name()), _ => Err(self.type_name()), @@ -1747,7 +1743,7 @@ impl Dynamic { #[inline] pub fn as_int(&self) -> Result { match self.0 { - Union::Int(n, _, ..) => Ok(n), + Union::Int(n, ..) => Ok(n), #[cfg(not(feature = "no_closure"))] Union::Shared(..) => self.read_lock().map(|v| *v).ok_or_else(|| self.type_name()), _ => Err(self.type_name()), @@ -1761,7 +1757,7 @@ impl Dynamic { #[inline] pub fn as_float(&self) -> Result { match self.0 { - Union::Float(n, _, ..) => Ok(*n), + Union::Float(n, ..) => Ok(*n), #[cfg(not(feature = "no_closure"))] Union::Shared(..) => self.read_lock().map(|v| *v).ok_or_else(|| self.type_name()), _ => Err(self.type_name()), @@ -1775,7 +1771,7 @@ impl Dynamic { #[inline] pub fn as_decimal(&self) -> Result { match self.0 { - Union::Decimal(ref n, _, ..) => Ok(**n), + Union::Decimal(ref n, ..) => Ok(**n), #[cfg(not(feature = "no_closure"))] Union::Shared(..) => self.read_lock().map(|v| *v).ok_or_else(|| self.type_name()), _ => Err(self.type_name()), @@ -1786,7 +1782,7 @@ impl Dynamic { #[inline] pub fn as_bool(&self) -> Result { match self.0 { - Union::Bool(b, _, ..) => Ok(b), + Union::Bool(b, ..) => Ok(b), #[cfg(not(feature = "no_closure"))] Union::Shared(..) => self.read_lock().map(|v| *v).ok_or_else(|| self.type_name()), _ => Err(self.type_name()), @@ -1797,7 +1793,7 @@ impl Dynamic { #[inline] pub fn as_char(&self) -> Result { match self.0 { - Union::Char(n, _, ..) => Ok(n), + Union::Char(n, ..) => Ok(n), #[cfg(not(feature = "no_closure"))] Union::Shared(..) => self.read_lock().map(|v| *v).ok_or_else(|| self.type_name()), _ => Err(self.type_name()), @@ -1812,7 +1808,7 @@ impl Dynamic { #[inline] pub(crate) fn as_str_ref(&self) -> Result<&str, &'static str> { match self.0 { - Union::Str(ref s, _, ..) => Ok(s), + Union::Str(ref s, ..) => Ok(s), #[cfg(not(feature = "no_closure"))] Union::Shared(..) => panic!("as_str_ref() cannot be called on shared values"), _ => Err(self.type_name()), @@ -1831,16 +1827,16 @@ impl Dynamic { #[inline] pub fn into_immutable_string(self) -> Result { match self.0 { - Union::Str(s, _, ..) => Ok(s), + Union::Str(s, ..) => Ok(s), #[cfg(not(feature = "no_closure"))] - Union::Shared(cell, _, ..) => { + Union::Shared(cell, ..) => { #[cfg(not(feature = "sync"))] let value = cell.borrow(); #[cfg(feature = "sync")] let value = cell.read().unwrap(); match value.0 { - Union::Str(ref s, _, ..) => Ok(s.clone()), + Union::Str(ref s, ..) => Ok(s.clone()), _ => Err((*value).type_name()), } } @@ -1853,16 +1849,16 @@ impl Dynamic { #[inline(always)] pub fn into_array(self) -> Result { match self.0 { - Union::Array(a, _, ..) => Ok(*a), + Union::Array(a, ..) => Ok(*a), #[cfg(not(feature = "no_closure"))] - Union::Shared(cell, _, ..) => { + Union::Shared(cell, ..) => { #[cfg(not(feature = "sync"))] let value = cell.borrow(); #[cfg(feature = "sync")] let value = cell.read().unwrap(); match value.0 { - Union::Array(ref a, _, ..) => Ok(a.as_ref().clone()), + Union::Array(ref a, ..) => Ok(a.as_ref().clone()), _ => Err((*value).type_name()), } } @@ -1875,7 +1871,7 @@ impl Dynamic { #[inline(always)] pub fn into_typed_array(self) -> Result, &'static str> { match self.0 { - Union::Array(a, _, ..) => a + Union::Array(a, ..) => a .into_iter() .map(|v| { #[cfg(not(feature = "no_closure"))] @@ -1893,14 +1889,14 @@ impl Dynamic { .collect(), Union::Blob(..) if TypeId::of::() == TypeId::of::() => Ok(self.cast::>()), #[cfg(not(feature = "no_closure"))] - Union::Shared(cell, _, ..) => { + Union::Shared(cell, ..) => { #[cfg(not(feature = "sync"))] let value = cell.borrow(); #[cfg(feature = "sync")] let value = cell.read().unwrap(); match value.0 { - Union::Array(ref a, _, ..) => { + Union::Array(ref a, ..) => { a.iter() .map(|v| { #[cfg(not(feature = "no_closure"))] @@ -1932,16 +1928,16 @@ impl Dynamic { #[inline(always)] pub fn into_blob(self) -> Result { match self.0 { - Union::Blob(a, _, ..) => Ok(*a), + Union::Blob(a, ..) => Ok(*a), #[cfg(not(feature = "no_closure"))] - Union::Shared(cell, _, ..) => { + Union::Shared(cell, ..) => { #[cfg(not(feature = "sync"))] let value = cell.borrow(); #[cfg(feature = "sync")] let value = cell.read().unwrap(); match value.0 { - Union::Blob(ref a, _, ..) => Ok(a.as_ref().clone()), + Union::Blob(ref a, ..) => Ok(a.as_ref().clone()), _ => Err((*value).type_name()), } }