Improve reify! syntax.
This commit is contained in:
parent
3d5908480a
commit
30ff208104
@ -183,7 +183,7 @@ impl Engine {
|
|||||||
.and_then(|result| {
|
.and_then(|result| {
|
||||||
// Bail out early if the return type needs no cast
|
// Bail out early if the return type needs no cast
|
||||||
if TypeId::of::<T>() == TypeId::of::<Dynamic>() {
|
if TypeId::of::<T>() == TypeId::of::<Dynamic>() {
|
||||||
return Ok(reify!(result => T));
|
return Ok(reify! { result => T });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cast return type
|
// Cast return type
|
||||||
|
@ -130,16 +130,16 @@ 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 => Option<T>),
|
Expr::IntegerConstant(x, ..) => reify! { *x => Option<T> },
|
||||||
|
|
||||||
#[cfg(not(feature = "no_float"))]
|
#[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::CharConstant(x, ..) => reify! { *x => Option<T> },
|
||||||
Expr::StringConstant(x, ..) => reify!(x.clone() => Option<T>),
|
Expr::StringConstant(x, ..) => reify! { x.clone() => Option<T> },
|
||||||
Expr::Variable(x, ..) => reify!(x.3.clone() => Option<T>),
|
Expr::Variable(x, ..) => reify! { x.3.clone() => Option<T> },
|
||||||
Expr::BoolConstant(x, ..) => reify!(*x => Option<T>),
|
Expr::BoolConstant(x, ..) => reify! { *x => Option<T> },
|
||||||
Expr::Unit(..) => reify!(() => Option<T>),
|
Expr::Unit(..) => reify! { () => Option<T> },
|
||||||
|
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
|
@ -200,7 +200,7 @@ impl Engine {
|
|||||||
|
|
||||||
// Bail out early if the return type needs no cast
|
// Bail out early if the return type needs no cast
|
||||||
if TypeId::of::<T>() == TypeId::of::<Dynamic>() {
|
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());
|
let typ = self.map_type_name(result.type_name());
|
||||||
|
@ -307,7 +307,7 @@ impl<'a> NativeCallContext<'a> {
|
|||||||
.and_then(|result| {
|
.and_then(|result| {
|
||||||
// Bail out early if the return type needs no cast
|
// Bail out early if the return type needs no cast
|
||||||
if TypeId::of::<T>() == TypeId::of::<Dynamic>() {
|
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());
|
let typ = self.engine().map_type_name(result.type_name());
|
||||||
@ -338,7 +338,7 @@ impl<'a> NativeCallContext<'a> {
|
|||||||
.and_then(|result| {
|
.and_then(|result| {
|
||||||
// Bail out early if the return type needs no cast
|
// Bail out early if the return type needs no cast
|
||||||
if TypeId::of::<T>() == TypeId::of::<Dynamic>() {
|
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());
|
let typ = self.engine().map_type_name(result.type_name());
|
||||||
|
@ -58,7 +58,7 @@ pub fn by_value<T: Variant + Clone>(data: &mut Dynamic) -> T {
|
|||||||
}
|
}
|
||||||
if TypeId::of::<T>() == TypeId::of::<String>() {
|
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
|
||||||
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.
|
// We consume the argument and then replace it with () - the argument is not supposed to be used again.
|
||||||
|
@ -75,8 +75,7 @@ pub mod blob_functions {
|
|||||||
len: INT,
|
len: INT,
|
||||||
value: INT,
|
value: INT,
|
||||||
) -> RhaiResultOf<Blob> {
|
) -> RhaiResultOf<Blob> {
|
||||||
let len = len.min(MAX_USIZE_INT);
|
let len = len.min(MAX_USIZE_INT).max(0) as usize;
|
||||||
let len = if len < 0 { 0 } else { len as usize };
|
|
||||||
let _ctx = ctx;
|
let _ctx = ctx;
|
||||||
|
|
||||||
// Check if blob will be over max size limit
|
// Check if blob will be over max size limit
|
||||||
|
30
src/reify.rs
30
src/reify.rs
@ -4,12 +4,12 @@
|
|||||||
///
|
///
|
||||||
/// # Syntax
|
/// # Syntax
|
||||||
///
|
///
|
||||||
/// * `reify!(`_variable_ or _expression_`,|`_temp-variable_`: `_type_`|` _code_`,` `||` _fallback_ `)`
|
/// * `reify! { `_variable_ or _expression_` => |`_temp-variable_`: `_type_`|` _code_`,` `||` _fallback_ `)`
|
||||||
/// * `reify!(`_variable_ or _expression_`,|`_temp-variable_`: `_type_`|` _code_ `)`
|
/// * `reify! { `_variable_ or _expression_` => |`_temp-variable_`: `_type_`|` _code_ `)`
|
||||||
/// * `reify!(`_variable_ or _expression_ `=>` `Option<`_type_`>` `)`
|
/// * `reify! { `_variable_ or _expression_ `=>` `Option<`_type_`>` `)`
|
||||||
/// * `reify!(`_variable_ or _expression_ `=>` _type_ `)`
|
/// * `reify! { `_variable_ or _expression_ `=>` _type_ `)`
|
||||||
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(clippy::redundant_else)]
|
#[allow(clippy::redundant_else)]
|
||||||
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 already checked to make sure the two types
|
// SAFETY: This is safe because we already checked to make sure the two types
|
||||||
@ -20,29 +20,29 @@ macro_rules! reify {
|
|||||||
$fallback
|
$fallback
|
||||||
}
|
}
|
||||||
}};
|
}};
|
||||||
($old:expr, |$new:ident : $t:ty| $code:expr, || $fallback:expr) => {{
|
($old:expr => |$new:ident : $t:ty| $code:expr, || $fallback:expr) => {{
|
||||||
let old = $old;
|
let old = $old;
|
||||||
reify!(old, |$new: $t| $code, || $fallback)
|
reify! { old => |$new: $t| $code, || $fallback }
|
||||||
}};
|
}};
|
||||||
|
|
||||||
($old:ident, |$new:ident : $t:ty| $code:expr) => {
|
($old:ident => |$new:ident : $t:ty| $code:expr) => {
|
||||||
reify!($old, |$new: $t| $code, || ())
|
reify! { $old => |$new: $t| $code, || () }
|
||||||
};
|
};
|
||||||
($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>) => {
|
($old:ident => Option<$t:ty>) => {
|
||||||
reify!($old, |v: $t| Some(v), || None)
|
reify! { $old => |v: $t| Some(v), || None }
|
||||||
};
|
};
|
||||||
($old:expr => Option<$t:ty>) => {
|
($old:expr => Option<$t:ty>) => {
|
||||||
reify!($old, |v: $t| Some(v), || None)
|
reify! { $old => |v: $t| Some(v), || None }
|
||||||
};
|
};
|
||||||
|
|
||||||
($old:ident => $t:ty) => {
|
($old:ident => $t:ty) => {
|
||||||
reify!($old, |v: $t| v, || unreachable!())
|
reify! { $old => |v: $t| v, || unreachable!() }
|
||||||
};
|
};
|
||||||
($old:expr => $t:ty) => {
|
($old:expr => $t:ty) => {
|
||||||
reify!($old, |v: $t| v, || unreachable!())
|
reify! { $old => |v: $t| v, || unreachable!() }
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -1083,38 +1083,35 @@ impl Dynamic {
|
|||||||
pub fn from<T: Variant + Clone>(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.
|
||||||
|
|
||||||
reify!(value, |v: Self| return v);
|
reify! { value => |v: Self| return v }
|
||||||
reify!(value, |v: INT| return v.into());
|
reify! { value => |v: INT| return v.into() }
|
||||||
|
|
||||||
#[cfg(not(feature = "no_float"))]
|
#[cfg(not(feature = "no_float"))]
|
||||||
reify!(value, |v: crate::FLOAT| return v.into());
|
reify! { value => |v: crate::FLOAT| return v.into() }
|
||||||
|
|
||||||
#[cfg(feature = "decimal")]
|
#[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: bool| return v.into() }
|
||||||
reify!(value, |v: char| return v.into());
|
reify! { value => |v: char| return v.into() }
|
||||||
reify!(value, |v: ImmutableString| return v.into());
|
reify! { value => |v: ImmutableString| return v.into() }
|
||||||
reify!(value, |v: String| return v.into());
|
reify! { value => |v: String| return v.into() }
|
||||||
reify!(value, |v: &str| return v.into());
|
reify! { value => |v: &str| return v.into() }
|
||||||
reify!(value, |v: ()| return v.into());
|
reify! { value => |v: ()| return v.into() }
|
||||||
|
|
||||||
#[cfg(not(feature = "no_index"))]
|
#[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"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
reify!(value, |v: crate::Blob| {
|
|
||||||
// don't use blob.into() because it'll be converted into an Array
|
// 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"))]
|
#[cfg(not(feature = "no_object"))]
|
||||||
reify!(value, |v: crate::Map| return v.into());
|
reify! { value => |v: crate::Map| return v.into() }
|
||||||
reify!(value, |v: FnPtr| return v.into());
|
reify! { value => |v: FnPtr| return v.into() }
|
||||||
|
|
||||||
#[cfg(not(feature = "no_time"))]
|
#[cfg(not(feature = "no_time"))]
|
||||||
reify!(value, |v: Instant| return v.into());
|
reify! { value => |v: Instant| return v.into() }
|
||||||
#[cfg(not(feature = "no_closure"))]
|
#[cfg(not(feature = "no_closure"))]
|
||||||
reify!(value, |v: crate::Shared<crate::Locked<Self>>| return v
|
reify! { value => |v: crate::Shared<crate::Locked<Self>>| return v.into() }
|
||||||
.into());
|
|
||||||
|
|
||||||
Self(Union::Variant(
|
Self(Union::Variant(
|
||||||
Box::new(Box::new(value)),
|
Box::new(Box::new(value)),
|
||||||
@ -1183,89 +1180,89 @@ impl Dynamic {
|
|||||||
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 Some(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(..) => Some(reify! { () => T }),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
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, ..) => Some(reify! { n => 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(v, ..) => Some(reify!(*v => T)),
|
Union::Float(v, ..) => Some(reify! { *v => 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(v, ..) => Some(reify!(*v => T)),
|
Union::Decimal(v, ..) => Some(reify! { *v => 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(b, ..) => Some(reify!(b => T)),
|
Union::Bool(b, ..) => Some(reify! { b => 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(s, ..) => Some(reify!(s => T)),
|
Union::Str(s, ..) => Some(reify! { s => T }),
|
||||||
_ => 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(s, ..) => Some(reify!(s.to_string() => T)),
|
Union::Str(s, ..) => Some(reify! { s.to_string() => 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(c, ..) => Some(reify!(c => T)),
|
Union::Char(c, ..) => Some(reify! { c => 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(a, ..) => Some(reify!(*a => T)),
|
Union::Array(a, ..) => Some(reify! { *a => 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(b, ..) => Some(reify!(*b => T)),
|
Union::Blob(b, ..) => Some(reify! { *b => 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(m, ..) => Some(reify!(*m => T)),
|
Union::Map(m, ..) => Some(reify! { *m => 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(f, ..) => Some(reify!(*f => T)),
|
Union::FnPtr(f, ..) => Some(reify! { *f => T }),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
#[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, ..) => Some(reify! { *t => T }),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -1305,7 +1302,7 @@ impl Dynamic {
|
|||||||
pub fn cast<T: Any + Clone>(self) -> T {
|
pub fn cast<T: Any + Clone>(self) -> T {
|
||||||
// Bail out early if the return type needs no cast
|
// Bail out early if the return type needs no cast
|
||||||
if TypeId::of::<T>() == TypeId::of::<Self>() {
|
if TypeId::of::<T>() == TypeId::of::<Self>() {
|
||||||
return reify!(self => T);
|
return reify! { self => T };
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "no_closure"))]
|
#[cfg(not(feature = "no_closure"))]
|
||||||
@ -2039,7 +2036,7 @@ impl Dynamic {
|
|||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
Union::Blob(b, ..) if TypeId::of::<T>() == TypeId::of::<u8>() => {
|
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"))]
|
#[cfg(not(feature = "no_closure"))]
|
||||||
Union::Shared(ref cell, ..) => {
|
Union::Shared(ref cell, ..) => {
|
||||||
@ -2062,7 +2059,7 @@ impl Dynamic {
|
|||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
Union::Blob(ref b, ..) if TypeId::of::<T>() == TypeId::of::<u8>() => {
|
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()),
|
_ => Err(cell.type_name()),
|
||||||
}
|
}
|
||||||
|
@ -161,7 +161,7 @@ impl FnPtr {
|
|||||||
self.call_raw(&ctx, None, arg_values).and_then(|result| {
|
self.call_raw(&ctx, None, arg_values).and_then(|result| {
|
||||||
// Bail out early if the return type needs no cast
|
// Bail out early if the return type needs no cast
|
||||||
if TypeId::of::<T>() == TypeId::of::<Dynamic>() {
|
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());
|
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| {
|
self.call_raw(context, None, arg_values).and_then(|result| {
|
||||||
// Bail out early if the return type needs no cast
|
// Bail out early if the return type needs no cast
|
||||||
if TypeId::of::<T>() == TypeId::of::<Dynamic>() {
|
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());
|
let typ = context.engine().map_type_name(result.type_name());
|
||||||
|
Loading…
Reference in New Issue
Block a user