From 30ff208104b8855d219d09b22c3f3e4390a8c6c5 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Fri, 9 Dec 2022 16:41:01 +0800 Subject: [PATCH] Improve reify! syntax. --- src/api/call_fn.rs | 2 +- src/api/custom_syntax.rs | 14 ++++---- src/api/eval.rs | 2 +- src/func/native.rs | 4 +-- src/func/register.rs | 2 +- src/packages/blob_basic.rs | 3 +- src/reify.rs | 30 ++++++++-------- src/types/dynamic.rs | 71 ++++++++++++++++++-------------------- src/types/fn_ptr.rs | 4 +-- 9 files changed, 64 insertions(+), 68 deletions(-) diff --git a/src/api/call_fn.rs b/src/api/call_fn.rs index 67861e16..bcbcf608 100644 --- a/src/api/call_fn.rs +++ b/src/api/call_fn.rs @@ -183,7 +183,7 @@ impl Engine { .and_then(|result| { // Bail out early if the return type needs no cast if TypeId::of::() == TypeId::of::() { - return Ok(reify!(result => T)); + return Ok(reify! { result => T }); } // Cast return type diff --git a/src/api/custom_syntax.rs b/src/api/custom_syntax.rs index faf9af17..87c793e6 100644 --- a/src/api/custom_syntax.rs +++ b/src/api/custom_syntax.rs @@ -130,16 +130,16 @@ 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 => Option), + Expr::IntegerConstant(x, ..) => reify! { *x => Option }, #[cfg(not(feature = "no_float"))] - Expr::FloatConstant(x, ..) => reify!(*x => Option), + Expr::FloatConstant(x, ..) => reify! { *x => Option }, - Expr::CharConstant(x, ..) => reify!(*x => Option), - Expr::StringConstant(x, ..) => reify!(x.clone() => Option), - Expr::Variable(x, ..) => reify!(x.3.clone() => Option), - Expr::BoolConstant(x, ..) => reify!(*x => Option), - Expr::Unit(..) => reify!(() => Option), + Expr::CharConstant(x, ..) => reify! { *x => Option }, + Expr::StringConstant(x, ..) => reify! { x.clone() => Option }, + Expr::Variable(x, ..) => reify! { x.3.clone() => Option }, + Expr::BoolConstant(x, ..) => reify! { *x => Option }, + Expr::Unit(..) => reify! { () => Option }, _ => None, } diff --git a/src/api/eval.rs b/src/api/eval.rs index 563be055..30880056 100644 --- a/src/api/eval.rs +++ b/src/api/eval.rs @@ -200,7 +200,7 @@ impl Engine { // Bail out early if the return type needs no cast if TypeId::of::() == TypeId::of::() { - return Ok(reify!(result => T)); + return Ok(reify! { result => T }); } let typ = self.map_type_name(result.type_name()); diff --git a/src/func/native.rs b/src/func/native.rs index e9e3b76d..1e1270aa 100644 --- a/src/func/native.rs +++ b/src/func/native.rs @@ -307,7 +307,7 @@ impl<'a> NativeCallContext<'a> { .and_then(|result| { // Bail out early if the return type needs no cast if TypeId::of::() == TypeId::of::() { - 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::() == TypeId::of::() { - return Ok(reify!(result => T)); + return Ok(reify! { result => T }); } let typ = self.engine().map_type_name(result.type_name()); diff --git a/src/func/register.rs b/src/func/register.rs index e8b99a31..822d8bad 100644 --- a/src/func/register.rs +++ b/src/func/register.rs @@ -58,7 +58,7 @@ pub fn by_value(data: &mut Dynamic) -> T { } 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); + 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. diff --git a/src/packages/blob_basic.rs b/src/packages/blob_basic.rs index 057a00ef..515cef06 100644 --- a/src/packages/blob_basic.rs +++ b/src/packages/blob_basic.rs @@ -75,8 +75,7 @@ pub mod blob_functions { len: INT, value: INT, ) -> RhaiResultOf { - 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 diff --git a/src/reify.rs b/src/reify.rs index 90a21a4c..cc8b6647 100644 --- a/src/reify.rs +++ b/src/reify.rs @@ -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!() } }; } diff --git a/src/types/dynamic.rs b/src/types/dynamic.rs index 7ac4281c..4c660af8 100644 --- a/src/types/dynamic.rs +++ b/src/types/dynamic.rs @@ -1083,38 +1083,35 @@ impl Dynamic { pub fn from(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); - }); + // don't use blob.into() because it'll be converted into an Array + 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>| return v - .into()); + reify! { value => |v: crate::Shared>| return v.into() } Self(Union::Variant( Box::new(Box::new(value)), @@ -1183,89 +1180,89 @@ impl Dynamic { self.flatten_in_place(); if TypeId::of::() == TypeId::of::() { - return Some(reify!(self => T)); + return Some(reify! { self => T }); } if TypeId::of::() == TypeId::of::<()>() { return match self.0 { - Union::Unit(..) => Some(reify!(() => T)), + Union::Unit(..) => Some(reify! { () => T }), _ => None, }; } if TypeId::of::() == TypeId::of::() { 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::() == TypeId::of::() { 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::() == TypeId::of::() { return match self.0 { - Union::Decimal(v, ..) => Some(reify!(*v => T)), + Union::Decimal(v, ..) => Some(reify! { *v => T }), _ => None, }; } if TypeId::of::() == TypeId::of::() { return match self.0 { - Union::Bool(b, ..) => Some(reify!(b => T)), + Union::Bool(b, ..) => Some(reify! { b => T }), _ => None, }; } if TypeId::of::() == TypeId::of::() { return match self.0 { - Union::Str(s, ..) => Some(reify!(s => T)), + Union::Str(s, ..) => Some(reify! { s => T }), _ => None, }; } if TypeId::of::() == TypeId::of::() { 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::() == TypeId::of::() { 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::() == TypeId::of::() { 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::() == TypeId::of::() { 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::() == TypeId::of::() { return match self.0 { - Union::Map(m, ..) => Some(reify!(*m => T)), + Union::Map(m, ..) => Some(reify! { *m => T }), _ => None, }; } if TypeId::of::() == TypeId::of::() { 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::() == TypeId::of::() { 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(self) -> T { // Bail out early if the return type needs no cast if TypeId::of::() == TypeId::of::() { - 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::() == TypeId::of::() => { - Ok(reify!(*b => Vec)) + Ok(reify! { *b => Vec }) } #[cfg(not(feature = "no_closure"))] Union::Shared(ref cell, ..) => { @@ -2062,7 +2059,7 @@ impl Dynamic { .collect() } Union::Blob(ref b, ..) if TypeId::of::() == TypeId::of::() => { - Ok(reify!(b.clone() => Vec)) + Ok(reify! { b.clone() => Vec }) } _ => Err(cell.type_name()), } diff --git a/src/types/fn_ptr.rs b/src/types/fn_ptr.rs index 0b36e2f9..7047da66 100644 --- a/src/types/fn_ptr.rs +++ b/src/types/fn_ptr.rs @@ -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::() == TypeId::of::() { - 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::() == TypeId::of::() { - return Ok(reify!(result => T)); + return Ok(reify! { result => T }); } let typ = context.engine().map_type_name(result.type_name());