Improve reify! syntax.

This commit is contained in:
Stephen Chung 2022-12-09 16:41:01 +08:00
parent 3d5908480a
commit 30ff208104
9 changed files with 64 additions and 68 deletions

View File

@ -183,7 +183,7 @@ impl Engine {
.and_then(|result| {
// Bail out early if the return type needs no cast
if TypeId::of::<T>() == TypeId::of::<Dynamic>() {
return Ok(reify!(result => T));
return Ok(reify! { result => T });
}
// Cast return type

View File

@ -130,16 +130,16 @@ impl Expression<'_> {
pub fn get_literal_value<T: Variant>(&self) -> Option<T> {
// Coded this way in order to maximally leverage potentials for dead-code removal.
match self.0 {
Expr::IntegerConstant(x, ..) => reify!(*x => Option<T>),
Expr::IntegerConstant(x, ..) => reify! { *x => Option<T> },
#[cfg(not(feature = "no_float"))]
Expr::FloatConstant(x, ..) => reify!(*x => Option<T>),
Expr::FloatConstant(x, ..) => reify! { *x => Option<T> },
Expr::CharConstant(x, ..) => reify!(*x => Option<T>),
Expr::StringConstant(x, ..) => reify!(x.clone() => Option<T>),
Expr::Variable(x, ..) => reify!(x.3.clone() => Option<T>),
Expr::BoolConstant(x, ..) => reify!(*x => Option<T>),
Expr::Unit(..) => reify!(() => Option<T>),
Expr::CharConstant(x, ..) => reify! { *x => Option<T> },
Expr::StringConstant(x, ..) => reify! { x.clone() => Option<T> },
Expr::Variable(x, ..) => reify! { x.3.clone() => Option<T> },
Expr::BoolConstant(x, ..) => reify! { *x => Option<T> },
Expr::Unit(..) => reify! { () => Option<T> },
_ => None,
}

View File

@ -200,7 +200,7 @@ impl Engine {
// Bail out early if the return type needs no cast
if TypeId::of::<T>() == TypeId::of::<Dynamic>() {
return Ok(reify!(result => T));
return Ok(reify! { result => T });
}
let typ = self.map_type_name(result.type_name());

View File

@ -307,7 +307,7 @@ impl<'a> NativeCallContext<'a> {
.and_then(|result| {
// Bail out early if the return type needs no cast
if TypeId::of::<T>() == TypeId::of::<Dynamic>() {
return Ok(reify!(result => T));
return Ok(reify! { result => T });
}
let typ = self.engine().map_type_name(result.type_name());
@ -338,7 +338,7 @@ impl<'a> NativeCallContext<'a> {
.and_then(|result| {
// Bail out early if the return type needs no cast
if TypeId::of::<T>() == TypeId::of::<Dynamic>() {
return Ok(reify!(result => T));
return Ok(reify! { result => T });
}
let typ = self.engine().map_type_name(result.type_name());

View File

@ -58,7 +58,7 @@ pub fn by_value<T: Variant + Clone>(data: &mut Dynamic) -> T {
}
if TypeId::of::<T>() == TypeId::of::<String>() {
// If T is `String`, data must be `ImmutableString`, so map directly to it
return reify!(mem::take(data).into_string().expect("`ImmutableString`") => T);
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.

View File

@ -75,8 +75,7 @@ pub mod blob_functions {
len: INT,
value: INT,
) -> RhaiResultOf<Blob> {
let len = len.min(MAX_USIZE_INT);
let len = if len < 0 { 0 } else { len as usize };
let len = len.min(MAX_USIZE_INT).max(0) as usize;
let _ctx = ctx;
// Check if blob will be over max size limit

View File

@ -4,12 +4,12 @@
///
/// # Syntax
///
/// * `reify!(`_variable_ or _expression_`,|`_temp-variable_`: `_type_`|` _code_`,` `||` _fallback_ `)`
/// * `reify!(`_variable_ or _expression_`,|`_temp-variable_`: `_type_`|` _code_ `)`
/// * `reify!(`_variable_ or _expression_ `=>` `Option<`_type_`>` `)`
/// * `reify!(`_variable_ or _expression_ `=>` _type_ `)`
/// * `reify! { `_variable_ or _expression_` => |`_temp-variable_`: `_type_`|` _code_`,` `||` _fallback_ `)`
/// * `reify! { `_variable_ or _expression_` => |`_temp-variable_`: `_type_`|` _code_ `)`
/// * `reify! { `_variable_ or _expression_ `=>` `Option<`_type_`>` `)`
/// * `reify! { `_variable_ or _expression_ `=>` _type_ `)`
macro_rules! reify {
($old:ident, |$new:ident : $t:ty| $code:expr, || $fallback:expr) => {{
($old:ident => |$new:ident : $t:ty| $code:expr, || $fallback:expr) => {{
#[allow(clippy::redundant_else)]
if std::any::TypeId::of::<$t>() == std::any::Any::type_id(&$old) {
// SAFETY: This is safe because we already checked to make sure the two types
@ -20,29 +20,29 @@ macro_rules! reify {
$fallback
}
}};
($old:expr, |$new:ident : $t:ty| $code:expr, || $fallback:expr) => {{
($old:expr => |$new:ident : $t:ty| $code:expr, || $fallback:expr) => {{
let old = $old;
reify!(old, |$new: $t| $code, || $fallback)
reify! { old => |$new: $t| $code, || $fallback }
}};
($old:ident, |$new:ident : $t:ty| $code:expr) => {
reify!($old, |$new: $t| $code, || ())
($old:ident => |$new:ident : $t:ty| $code:expr) => {
reify! { $old => |$new: $t| $code, || () }
};
($old:expr, |$new:ident : $t:ty| $code:expr) => {
reify!($old, |$new: $t| $code, || ())
($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)
reify! { $old => |v: $t| Some(v), || None }
};
($old:expr => Option<$t:ty>) => {
reify!($old, |v: $t| Some(v), || None)
reify! { $old => |v: $t| Some(v), || None }
};
($old:ident => $t:ty) => {
reify!($old, |v: $t| v, || unreachable!())
reify! { $old => |v: $t| v, || unreachable!() }
};
($old:expr => $t:ty) => {
reify!($old, |v: $t| v, || unreachable!())
reify! { $old => |v: $t| v, || unreachable!() }
};
}

View File

@ -1083,38 +1083,35 @@ impl Dynamic {
pub fn from<T: Variant + Clone>(value: T) -> Self {
// Coded this way in order to maximally leverage potentials for dead-code removal.
reify!(value, |v: Self| return v);
reify!(value, |v: INT| return v.into());
reify! { value => |v: Self| return v }
reify! { value => |v: INT| return v.into() }
#[cfg(not(feature = "no_float"))]
reify!(value, |v: crate::FLOAT| return v.into());
reify! { value => |v: crate::FLOAT| return v.into() }
#[cfg(feature = "decimal")]
reify!(value, |v: rust_decimal::Decimal| return v.into());
reify! { value => |v: rust_decimal::Decimal| return v.into() }
reify!(value, |v: bool| return v.into());
reify!(value, |v: char| return v.into());
reify!(value, |v: ImmutableString| return v.into());
reify!(value, |v: String| return v.into());
reify!(value, |v: &str| return v.into());
reify!(value, |v: ()| return v.into());
reify! { value => |v: bool| return v.into() }
reify! { value => |v: char| return v.into() }
reify! { value => |v: ImmutableString| return v.into() }
reify! { value => |v: String| return v.into() }
reify! { value => |v: &str| return v.into() }
reify! { value => |v: ()| return v.into() }
#[cfg(not(feature = "no_index"))]
reify!(value, |v: crate::Array| return v.into());
reify! { value => |v: crate::Array| return v.into() }
#[cfg(not(feature = "no_index"))]
reify!(value, |v: crate::Blob| {
// don't use blob.into() because it'll be converted into an Array
return Self::from_blob(v);
});
reify! { value => |v: crate::Blob| return Self::from_blob(v) }
#[cfg(not(feature = "no_object"))]
reify!(value, |v: crate::Map| return v.into());
reify!(value, |v: FnPtr| return v.into());
reify! { value => |v: crate::Map| return v.into() }
reify! { value => |v: FnPtr| return v.into() }
#[cfg(not(feature = "no_time"))]
reify!(value, |v: Instant| return v.into());
reify! { value => |v: Instant| return v.into() }
#[cfg(not(feature = "no_closure"))]
reify!(value, |v: crate::Shared<crate::Locked<Self>>| return v
.into());
reify! { value => |v: crate::Shared<crate::Locked<Self>>| return v.into() }
Self(Union::Variant(
Box::new(Box::new(value)),
@ -1183,89 +1180,89 @@ impl Dynamic {
self.flatten_in_place();
if TypeId::of::<T>() == TypeId::of::<Self>() {
return Some(reify!(self => T));
return Some(reify! { self => T });
}
if TypeId::of::<T>() == TypeId::of::<()>() {
return match self.0 {
Union::Unit(..) => Some(reify!(() => T)),
Union::Unit(..) => Some(reify! { () => T }),
_ => None,
};
}
if TypeId::of::<T>() == TypeId::of::<INT>() {
return match self.0 {
Union::Int(n, ..) => Some(reify!(n => T)),
Union::Int(n, ..) => Some(reify! { n => T }),
_ => None,
};
}
#[cfg(not(feature = "no_float"))]
if TypeId::of::<T>() == TypeId::of::<crate::FLOAT>() {
return match self.0 {
Union::Float(v, ..) => Some(reify!(*v => T)),
Union::Float(v, ..) => Some(reify! { *v => T }),
_ => None,
};
}
#[cfg(feature = "decimal")]
if TypeId::of::<T>() == TypeId::of::<rust_decimal::Decimal>() {
return match self.0 {
Union::Decimal(v, ..) => Some(reify!(*v => T)),
Union::Decimal(v, ..) => Some(reify! { *v => T }),
_ => None,
};
}
if TypeId::of::<T>() == TypeId::of::<bool>() {
return match self.0 {
Union::Bool(b, ..) => Some(reify!(b => T)),
Union::Bool(b, ..) => Some(reify! { b => T }),
_ => None,
};
}
if TypeId::of::<T>() == TypeId::of::<ImmutableString>() {
return match self.0 {
Union::Str(s, ..) => Some(reify!(s => T)),
Union::Str(s, ..) => Some(reify! { s => T }),
_ => None,
};
}
if TypeId::of::<T>() == TypeId::of::<String>() {
return match self.0 {
Union::Str(s, ..) => Some(reify!(s.to_string() => T)),
Union::Str(s, ..) => Some(reify! { s.to_string() => T }),
_ => None,
};
}
if TypeId::of::<T>() == TypeId::of::<char>() {
return match self.0 {
Union::Char(c, ..) => Some(reify!(c => T)),
Union::Char(c, ..) => Some(reify! { c => T }),
_ => None,
};
}
#[cfg(not(feature = "no_index"))]
if TypeId::of::<T>() == TypeId::of::<crate::Array>() {
return match self.0 {
Union::Array(a, ..) => Some(reify!(*a => T)),
Union::Array(a, ..) => Some(reify! { *a => T }),
_ => None,
};
}
#[cfg(not(feature = "no_index"))]
if TypeId::of::<T>() == TypeId::of::<crate::Blob>() {
return match self.0 {
Union::Blob(b, ..) => Some(reify!(*b => T)),
Union::Blob(b, ..) => Some(reify! { *b => T }),
_ => None,
};
}
#[cfg(not(feature = "no_object"))]
if TypeId::of::<T>() == TypeId::of::<crate::Map>() {
return match self.0 {
Union::Map(m, ..) => Some(reify!(*m => T)),
Union::Map(m, ..) => Some(reify! { *m => T }),
_ => None,
};
}
if TypeId::of::<T>() == TypeId::of::<FnPtr>() {
return match self.0 {
Union::FnPtr(f, ..) => Some(reify!(*f => T)),
Union::FnPtr(f, ..) => Some(reify! { *f => T }),
_ => None,
};
}
#[cfg(not(feature = "no_time"))]
if TypeId::of::<T>() == TypeId::of::<Instant>() {
return match self.0 {
Union::TimeStamp(t, ..) => Some(reify!(*t => T)),
Union::TimeStamp(t, ..) => Some(reify! { *t => T }),
_ => None,
};
}
@ -1305,7 +1302,7 @@ impl Dynamic {
pub fn cast<T: Any + Clone>(self) -> T {
// Bail out early if the return type needs no cast
if TypeId::of::<T>() == TypeId::of::<Self>() {
return reify!(self => T);
return reify! { self => T };
}
#[cfg(not(feature = "no_closure"))]
@ -2039,7 +2036,7 @@ impl Dynamic {
})
.collect(),
Union::Blob(b, ..) if TypeId::of::<T>() == TypeId::of::<u8>() => {
Ok(reify!(*b => Vec<T>))
Ok(reify! { *b => Vec<T> })
}
#[cfg(not(feature = "no_closure"))]
Union::Shared(ref cell, ..) => {
@ -2062,7 +2059,7 @@ impl Dynamic {
.collect()
}
Union::Blob(ref b, ..) if TypeId::of::<T>() == TypeId::of::<u8>() => {
Ok(reify!(b.clone() => Vec<T>))
Ok(reify! { b.clone() => Vec<T> })
}
_ => Err(cell.type_name()),
}

View File

@ -161,7 +161,7 @@ impl FnPtr {
self.call_raw(&ctx, None, arg_values).and_then(|result| {
// Bail out early if the return type needs no cast
if TypeId::of::<T>() == TypeId::of::<Dynamic>() {
return Ok(reify!(result => T));
return Ok(reify! { result => T });
}
let typ = engine.map_type_name(result.type_name());
@ -190,7 +190,7 @@ impl FnPtr {
self.call_raw(context, None, arg_values).and_then(|result| {
// Bail out early if the return type needs no cast
if TypeId::of::<T>() == TypeId::of::<Dynamic>() {
return Ok(reify!(result => T));
return Ok(reify! { result => T });
}
let typ = context.engine().map_type_name(result.type_name());