Add Dynamic::try_cast_raw

This commit is contained in:
Stephen Chung 2023-05-06 02:08:32 +08:00
parent dbe85bf33e
commit 0a7cca9910
4 changed files with 69 additions and 47 deletions

View File

@ -212,11 +212,13 @@ impl Engine {
return Ok(reify! { result => T }); return Ok(reify! { result => T });
} }
let typ = self.map_type_name(result.type_name()); result.try_cast_raw::<T>().map_err(|v| {
ERR::ErrorMismatchOutputType(
result.try_cast::<T>().ok_or_else(|| { self.map_type_name(type_name::<T>()).into(),
let t = self.map_type_name(type_name::<T>()).into(); self.map_type_name(v.type_name()).into(),
ERR::ErrorMismatchOutputType(t, typ.into(), Position::NONE).into() Position::NONE,
)
.into()
}) })
} }
/// Evaluate an [`AST`] with own scope, returning the result value or an error. /// Evaluate an [`AST`] with own scope, returning the result value or an error.

View File

@ -861,9 +861,12 @@ impl Engine {
} }
let v = self.eval_expr(global, caches, scope, this_ptr, expr)?; let v = self.eval_expr(global, caches, scope, this_ptr, expr)?;
let typ = v.type_name();
let path = v.try_cast::<crate::ImmutableString>().ok_or_else(|| { let path = v.try_cast_raw::<crate::ImmutableString>().map_err(|v| {
self.make_type_mismatch_err::<crate::ImmutableString>(typ, expr.position()) self.make_type_mismatch_err::<crate::ImmutableString>(
v.type_name(),
expr.position(),
)
})?; })?;
let path_pos = expr.start_position(); let path_pos = expr.start_position();

View File

@ -796,9 +796,11 @@ impl Engine {
debug_assert!(!call_args.is_empty()); debug_assert!(!call_args.is_empty());
// FnPtr call on object // FnPtr call on object
let typ = call_args[0].type_name(); let fn_ptr = call_args[0].take().try_cast_raw::<FnPtr>().map_err(|v| {
let fn_ptr = call_args[0].take().try_cast::<FnPtr>().ok_or_else(|| { self.make_type_mismatch_err::<FnPtr>(
self.make_type_mismatch_err::<FnPtr>(self.map_type_name(typ), first_arg_pos) self.map_type_name(v.type_name()),
first_arg_pos,
)
})?; })?;
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
@ -1025,9 +1027,11 @@ impl Engine {
let (first_arg_value, first_arg_pos) = let (first_arg_value, first_arg_pos) =
self.get_arg_value(global, caches, scope, this_ptr.as_deref_mut(), arg)?; self.get_arg_value(global, caches, scope, this_ptr.as_deref_mut(), arg)?;
let typ = first_arg_value.type_name(); let fn_ptr = first_arg_value.try_cast_raw::<FnPtr>().map_err(|v| {
let fn_ptr = first_arg_value.try_cast::<FnPtr>().ok_or_else(|| { self.make_type_mismatch_err::<FnPtr>(
self.make_type_mismatch_err::<FnPtr>(self.map_type_name(typ), first_arg_pos) self.map_type_name(v.type_name()),
first_arg_pos,
)
})?; })?;
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
@ -1104,9 +1108,11 @@ impl Engine {
let (first_arg_value, first_arg_pos) = let (first_arg_value, first_arg_pos) =
self.get_arg_value(global, caches, scope, this_ptr.as_deref_mut(), first)?; self.get_arg_value(global, caches, scope, this_ptr.as_deref_mut(), first)?;
let typ = first_arg_value.type_name(); let mut fn_ptr = first_arg_value.try_cast_raw::<FnPtr>().map_err(|v| {
let mut fn_ptr = first_arg_value.try_cast::<FnPtr>().ok_or_else(|| { self.make_type_mismatch_err::<FnPtr>(
self.make_type_mismatch_err::<FnPtr>(self.map_type_name(typ), first_arg_pos) self.map_type_name(v.type_name()),
first_arg_pos,
)
})?; })?;
// Append the new curried arguments to the existing list. // Append the new curried arguments to the existing list.

View File

@ -1174,108 +1174,119 @@ impl Dynamic {
/// ///
/// assert_eq!(x.try_cast::<u32>().expect("x should be u32"), 42); /// assert_eq!(x.try_cast::<u32>().expect("x should be u32"), 42);
/// ``` /// ```
#[inline] #[inline(always)]
#[must_use] #[must_use]
#[allow(unused_mut)] #[allow(unused_mut)]
pub fn try_cast<T: Any>(mut self) -> Option<T> { pub fn try_cast<T: Any>(mut self) -> Option<T> {
self.try_cast_raw().ok()
}
/// Convert the [`Dynamic`] value into specific type.
///
/// Casting to a [`Dynamic`] just returns as is, but if it contains a shared value,
/// it is cloned into a [`Dynamic`] with a normal value.
///
/// Returns itself if types mismatched.
pub(crate) fn try_cast_raw<T: Any>(mut self) -> Result<T, Self> {
// 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.
#[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "no_closure"))]
self.flatten_in_place(); self.flatten_in_place();
if TypeId::of::<T>() == TypeId::of::<Self>() { if TypeId::of::<T>() == TypeId::of::<Self>() {
return Some(reify! { self => !!! T }); return Ok(reify! { self => !!! T });
} }
if TypeId::of::<T>() == TypeId::of::<()>() { if TypeId::of::<T>() == TypeId::of::<()>() {
return match self.0 { return match self.0 {
Union::Unit(..) => Some(reify! { () => !!! T }), Union::Unit(..) => Ok(reify! { () => !!! T }),
_ => None, _ => Err(self),
}; };
} }
if TypeId::of::<T>() == TypeId::of::<INT>() { if TypeId::of::<T>() == TypeId::of::<INT>() {
return match self.0 { return match self.0 {
Union::Int(n, ..) => Some(reify! { n => !!! T }), Union::Int(n, ..) => Ok(reify! { n => !!! T }),
_ => None, _ => Err(self),
}; };
} }
#[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(v, ..) => Some(reify! { *v => !!! T }), Union::Float(v, ..) => Ok(reify! { *v => !!! T }),
_ => None, _ => Err(self),
}; };
} }
#[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(v, ..) => Some(reify! { *v => !!! T }), Union::Decimal(v, ..) => Ok(reify! { *v => !!! T }),
_ => None, _ => Err(self),
}; };
} }
if TypeId::of::<T>() == TypeId::of::<bool>() { if TypeId::of::<T>() == TypeId::of::<bool>() {
return match self.0 { return match self.0 {
Union::Bool(b, ..) => Some(reify! { b => !!! T }), Union::Bool(b, ..) => Ok(reify! { b => !!! T }),
_ => None, _ => Err(self),
}; };
} }
if TypeId::of::<T>() == TypeId::of::<ImmutableString>() { if TypeId::of::<T>() == TypeId::of::<ImmutableString>() {
return match self.0 { return match self.0 {
Union::Str(s, ..) => Some(reify! { s => !!! T }), Union::Str(s, ..) => Ok(reify! { s => !!! T }),
_ => None, _ => Err(self),
}; };
} }
if TypeId::of::<T>() == TypeId::of::<String>() { if TypeId::of::<T>() == TypeId::of::<String>() {
return match self.0 { return match self.0 {
Union::Str(s, ..) => Some(reify! { s.to_string() => !!! T }), Union::Str(s, ..) => Ok(reify! { s.to_string() => !!! T }),
_ => None, _ => Err(self),
}; };
} }
if TypeId::of::<T>() == TypeId::of::<char>() { if TypeId::of::<T>() == TypeId::of::<char>() {
return match self.0 { return match self.0 {
Union::Char(c, ..) => Some(reify! { c => !!! T }), Union::Char(c, ..) => Ok(reify! { c => !!! T }),
_ => None, _ => Err(self),
}; };
} }
#[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(a, ..) => Some(reify! { *a => !!! T }), Union::Array(a, ..) => Ok(reify! { *a => !!! T }),
_ => None, _ => Err(self),
}; };
} }
#[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(b, ..) => Some(reify! { *b => !!! T }), Union::Blob(b, ..) => Ok(reify! { *b => !!! T }),
_ => None, _ => Err(self),
}; };
} }
#[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(m, ..) => Some(reify! { *m => !!! T }), Union::Map(m, ..) => Ok(reify! { *m => !!! T }),
_ => None, _ => Err(self),
}; };
} }
if TypeId::of::<T>() == TypeId::of::<FnPtr>() { if TypeId::of::<T>() == TypeId::of::<FnPtr>() {
return match self.0 { return match self.0 {
Union::FnPtr(f, ..) => Some(reify! { *f => !!! T }), Union::FnPtr(f, ..) => Ok(reify! { *f => !!! T }),
_ => None, _ => Err(self),
}; };
} }
#[cfg(not(feature = "no_time"))] #[cfg(not(feature = "no_time"))]
if TypeId::of::<T>() == TypeId::of::<Instant>() { if TypeId::of::<T>() == TypeId::of::<Instant>() {
return match self.0 { return match self.0 {
Union::TimeStamp(t, ..) => Some(reify! { *t => !!! T }), Union::TimeStamp(t, ..) => Ok(reify! { *t => !!! T }),
_ => None, _ => Err(self),
}; };
} }
match self.0 { match self.0 {
Union::Variant(v, ..) => (*v).as_boxed_any().downcast().ok().map(|x| *x), Union::Variant(v, ..) if TypeId::of::<T>() == (**v).type_id() => {
Ok((*v).as_boxed_any().downcast().map(|x| *x).unwrap())
}
#[cfg(not(feature = "no_closure"))] #[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"),
_ => None, _ => Err(self),
} }
} }
/// Convert the [`Dynamic`] value into a specific type. /// Convert the [`Dynamic`] value into a specific type.