Simplify Dynamic::from for better inlining.

This commit is contained in:
Stephen Chung 2022-01-02 20:47:03 +08:00
parent 809feaf58d
commit 29f1328087
4 changed files with 77 additions and 83 deletions

View File

@ -95,41 +95,39 @@ impl Expression<'_> {
if TypeId::of::<T>() == TypeId::of::<INT>() { if TypeId::of::<T>() == TypeId::of::<INT>() {
return match self.0 { return match self.0 {
Expr::IntegerConstant(x, _) => unsafe_try_cast(*x).ok(), Expr::IntegerConstant(x, _) => unsafe_try_cast(*x),
_ => 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 {
Expr::FloatConstant(x, _) => unsafe_try_cast(*x).ok(), Expr::FloatConstant(x, _) => unsafe_try_cast(*x),
_ => None, _ => None,
}; };
} }
if TypeId::of::<T>() == TypeId::of::<char>() { if TypeId::of::<T>() == TypeId::of::<char>() {
return match self.0 { return match self.0 {
Expr::CharConstant(x, _) => unsafe_try_cast(*x).ok(), Expr::CharConstant(x, _) => unsafe_try_cast(*x),
_ => None, _ => None,
}; };
} }
if TypeId::of::<T>() == TypeId::of::<ImmutableString>() { if TypeId::of::<T>() == TypeId::of::<ImmutableString>() {
return match self.0 { return match self.0 {
Expr::StringConstant(x, _) => unsafe_try_cast(x.clone()).ok(), Expr::StringConstant(x, _) => unsafe_try_cast(x.clone()),
Expr::Variable(_, _, x) => { Expr::Variable(_, _, x) => unsafe_try_cast(Into::<ImmutableString>::into(&x.2)),
unsafe_try_cast(Into::<ImmutableString>::into(&x.2)).ok()
}
_ => None, _ => None,
}; };
} }
if TypeId::of::<T>() == TypeId::of::<bool>() { if TypeId::of::<T>() == TypeId::of::<bool>() {
return match self.0 { return match self.0 {
Expr::BoolConstant(x, _) => unsafe_try_cast(*x).ok(), Expr::BoolConstant(x, _) => unsafe_try_cast(*x),
_ => None, _ => None,
}; };
} }
if TypeId::of::<T>() == TypeId::of::<()>() { if TypeId::of::<T>() == TypeId::of::<()>() {
return match self.0 { return match self.0 {
Expr::Unit(_) => unsafe_try_cast(()).ok(), Expr::Unit(_) => unsafe_try_cast(()),
_ => None, _ => None,
}; };
} }

View File

@ -5,7 +5,7 @@
use super::call::FnCallArgs; use super::call::FnCallArgs;
use super::callable_function::CallableFunction; use super::callable_function::CallableFunction;
use super::native::{FnAny, SendSync}; use super::native::{FnAny, SendSync};
use crate::r#unsafe::unsafe_try_cast; use crate::r#unsafe::unsafe_cast;
use crate::tokenizer::Position; use crate::tokenizer::Position;
use crate::types::dynamic::{DynamicWriteLock, Variant}; use crate::types::dynamic::{DynamicWriteLock, Variant};
use crate::{Dynamic, NativeCallContext, RhaiResultOf, ERR}; use crate::{Dynamic, NativeCallContext, RhaiResultOf, ERR};
@ -50,8 +50,7 @@ pub fn by_value<T: Variant + Clone>(data: &mut Dynamic) -> T {
ref_t.clone() ref_t.clone()
} else if TypeId::of::<T>() == TypeId::of::<String>() { } else 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 value = mem::take(data).into_string().expect("`ImmutableString`"); unsafe_cast(mem::take(data).into_string().expect("`ImmutableString`"))
unsafe_try_cast(value).expect("checked")
} else { } 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.

View File

@ -1,7 +1,7 @@
//! Helper module which defines the [`Any`] trait to to allow dynamic value handling. //! Helper module which defines the [`Any`] trait to to allow dynamic value handling.
use crate::func::native::SendSync; use crate::func::native::SendSync;
use crate::r#unsafe::{unsafe_cast_box, unsafe_try_cast}; use crate::r#unsafe::{unsafe_cast, unsafe_cast_box, unsafe_try_cast};
use crate::{ExclusiveRange, FnPtr, ImmutableString, InclusiveRange, INT}; use crate::{ExclusiveRange, FnPtr, ImmutableString, InclusiveRange, INT};
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
@ -1209,11 +1209,11 @@ impl Dynamic {
/// ``` /// ```
#[inline] #[inline]
#[must_use] #[must_use]
pub fn from<T: Variant + Clone>(mut value: T) -> Self { pub fn from<T: Variant + Clone>(value: 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.
if TypeId::of::<T>() == TypeId::of::<Dynamic>() { if TypeId::of::<T>() == TypeId::of::<Dynamic>() {
return unsafe_try_cast::<_, Dynamic>(value).ok().expect(CHECKED); return unsafe_cast::<_, Dynamic>(value);
} }
let val = value.as_any(); let val = value.as_any();
@ -1249,52 +1249,36 @@ impl Dynamic {
return ().into(); return ().into();
} }
value = match unsafe_try_cast::<_, String>(value) { if TypeId::of::<T>() == TypeId::of::<String>() {
Ok(s) => return s.into(), return unsafe_cast::<_, String>(value).into();
Err(value) => value, }
}; #[cfg(not(feature = "no_float"))]
#[cfg(not(feature = "no_index"))] if TypeId::of::<T>() == TypeId::of::<crate::FLOAT>() {
{ return unsafe_cast::<_, crate::FLOAT>(value).into();
value = match unsafe_try_cast::<_, crate::Array>(value) {
Ok(array) => return array.into(),
Err(value) => value,
};
} }
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
{ if TypeId::of::<T>() == TypeId::of::<crate::Array>() {
value = match unsafe_try_cast::<_, crate::Blob>(value) { return unsafe_cast::<_, crate::Array>(value).into();
Ok(blob) => return Dynamic::from_blob(blob), // don't use blob.into() because it'll be converted into an Array }
Err(value) => value, #[cfg(not(feature = "no_index"))]
}; if TypeId::of::<T>() == TypeId::of::<crate::Blob>() {
return Dynamic::from_blob(unsafe_cast::<_, crate::Blob>(value)); // don't use blob.into() because it'll be converted into an Array
} }
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
{ if TypeId::of::<T>() == TypeId::of::<crate::Map>() {
value = match unsafe_try_cast::<_, crate::Map>(value) { return unsafe_cast::<_, crate::Map>(value).into();
Ok(map) => return map.into(), }
Err(value) => value, if TypeId::of::<T>() == TypeId::of::<FnPtr>() {
}; return unsafe_cast::<_, FnPtr>(value).into();
} }
value = match unsafe_try_cast::<_, FnPtr>(value) {
Ok(fn_ptr) => return fn_ptr.into(),
Err(value) => value,
};
#[cfg(not(feature = "no_std"))] #[cfg(not(feature = "no_std"))]
{ if TypeId::of::<T>() == TypeId::of::<Instant>() {
value = match unsafe_try_cast::<_, Instant>(value) { return unsafe_cast::<_, Instant>(value).into();
Ok(timestamp) => return timestamp.into(),
Err(value) => value,
};
} }
#[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "no_closure"))]
{ if TypeId::of::<T>() == TypeId::of::<crate::Shared<crate::Locked<Dynamic>>>() {
value = match unsafe_try_cast::<_, crate::Shared<crate::Locked<Dynamic>>>(value) { return unsafe_cast::<_, crate::Shared<crate::Locked<Dynamic>>>(value).into();
Ok(value) => return value.into(),
Err(value) => value,
};
} }
Self(Union::Variant( Self(Union::Variant(
@ -1365,12 +1349,12 @@ impl Dynamic {
} }
if TypeId::of::<T>() == TypeId::of::<Dynamic>() { if TypeId::of::<T>() == TypeId::of::<Dynamic>() {
return unsafe_try_cast::<_, T>(self).ok(); return unsafe_try_cast::<_, T>(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(value, _, _) => unsafe_try_cast(value).ok(), Union::Int(value, _, _) => unsafe_try_cast(value),
_ => None, _ => None,
}; };
} }
@ -1378,7 +1362,7 @@ impl Dynamic {
#[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(value, _, _) => unsafe_try_cast(*value).ok(), Union::Float(value, _, _) => unsafe_try_cast(*value),
_ => None, _ => None,
}; };
} }
@ -1386,35 +1370,35 @@ impl Dynamic {
#[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(value, _, _) => unsafe_try_cast(*value).ok(), Union::Decimal(value, _, _) => unsafe_try_cast(*value),
_ => 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(value, _, _) => unsafe_try_cast(value).ok(), Union::Bool(value, _, _) => unsafe_try_cast(value),
_ => 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(value, _, _) => unsafe_try_cast(value).ok(), Union::Str(value, _, _) => unsafe_try_cast(value),
_ => None, _ => None,
}; };
} }
if TypeId::of::<T>() == TypeId::of::<String>() { if TypeId::of::<T>() == TypeId::of::<String>() {
return match self.0 { return match self.0 {
Union::Str(value, _, _) => unsafe_try_cast(value.into_owned()).ok(), Union::Str(value, _, _) => unsafe_try_cast(value.into_owned()),
_ => 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(value, _, _) => unsafe_try_cast(value).ok(), Union::Char(value, _, _) => unsafe_try_cast(value),
_ => None, _ => None,
}; };
} }
@ -1422,7 +1406,7 @@ impl Dynamic {
#[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(value, _, _) => unsafe_cast_box::<_, T>(value).ok().map(|v| *v), Union::Array(value, _, _) => unsafe_cast_box::<_, T>(value).map(|v| *v),
_ => None, _ => None,
}; };
} }
@ -1430,7 +1414,7 @@ impl Dynamic {
#[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(value, _, _) => unsafe_cast_box::<_, T>(value).ok().map(|v| *v), Union::Blob(value, _, _) => unsafe_cast_box::<_, T>(value).map(|v| *v),
_ => None, _ => None,
}; };
} }
@ -1438,14 +1422,14 @@ impl Dynamic {
#[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(value, _, _) => unsafe_cast_box::<_, T>(value).ok().map(|v| *v), Union::Map(value, _, _) => unsafe_cast_box::<_, T>(value).map(|v| *v),
_ => 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(value, _, _) => unsafe_cast_box::<_, T>(value).ok().map(|v| *v), Union::FnPtr(value, _, _) => unsafe_cast_box::<_, T>(value).map(|v| *v),
_ => None, _ => None,
}; };
} }
@ -1453,20 +1437,20 @@ impl Dynamic {
#[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(value, _, _) => unsafe_cast_box::<_, T>(value).ok().map(|v| *v), Union::TimeStamp(value, _, _) => unsafe_cast_box::<_, T>(value).map(|v| *v),
_ => None, _ => None,
}; };
} }
if TypeId::of::<T>() == TypeId::of::<()>() { if TypeId::of::<T>() == TypeId::of::<()>() {
return match self.0 { return match self.0 {
Union::Unit(value, _, _) => unsafe_try_cast(value).ok(), Union::Unit(value, _, _) => unsafe_try_cast(value),
_ => None, _ => None,
}; };
} }
match self.0 { match self.0 {
Union::Variant(value, _, _) => (*value).as_box_any().downcast().map(|x| *x).ok(), Union::Variant(value, _, _) => (*value).as_box_any().downcast().ok().map(|x| *x),
#[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, _ => None,

View File

@ -8,35 +8,48 @@ use std::{
}; };
/// Cast a type into another type. /// Cast a type into another type.
#[inline] ///
pub fn unsafe_try_cast<A: Any, B: Any>(a: A) -> Result<B, A> { /// # Undefined Behavior
if TypeId::of::<B>() == a.type_id() { ///
// SAFETY: Just checked we have the right type. We explicitly forget the /// It is UB if the types are not compatible.
// value immediately after moving out, removing any chance of a destructor #[inline(always)]
// running or value otherwise being used again. #[must_use]
pub fn unsafe_cast<A: Any, B: Any>(a: A) -> B {
unsafe { unsafe {
let ret: B = ptr::read(&a as *const _ as *const B); let ret: B = ptr::read(&a as *const _ as *const B);
// We explicitly forget the value immediately after moving out,
// removing any chance of a destructor running or value otherwise
// being used again.
mem::forget(a); mem::forget(a);
Ok(ret) ret
} }
}
/// Cast a type into another type.
#[inline(always)]
#[must_use]
pub fn unsafe_try_cast<A: Any, B: Any>(a: A) -> Option<B> {
if TypeId::of::<B>() == a.type_id() {
// SAFETY: Just checked we have the right type.
Some(unsafe_cast(a))
} else { } else {
Err(a) None
} }
} }
/// Cast a Boxed type into another type. /// Cast a Boxed type into another type.
#[inline] #[inline(always)]
pub fn unsafe_cast_box<X: Any, T: Any>(item: Box<X>) -> Result<Box<T>, Box<X>> { #[must_use]
pub fn unsafe_cast_box<X: Any, T: Any>(item: Box<X>) -> Option<Box<T>> {
// Only allow casting to the exact same type // Only allow casting to the exact same type
if TypeId::of::<X>() == TypeId::of::<T>() { if TypeId::of::<X>() == TypeId::of::<T>() {
// SAFETY: just checked whether we are pointing to the correct type // SAFETY: just checked whether we are pointing to the correct type
unsafe { unsafe {
let raw: *mut dyn Any = Box::into_raw(item as Box<dyn Any>); let raw: *mut dyn Any = Box::into_raw(item as Box<dyn Any>);
Ok(Box::from_raw(raw as *mut T)) Some(Box::from_raw(raw as *mut T))
} }
} else { } else {
// Return the consumed item for chaining. None
Err(item)
} }
} }