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