Use deref for Expression.

This commit is contained in:
Stephen Chung 2021-12-06 18:50:37 +08:00
parent cc98e82ea1
commit 5b64e0b383
2 changed files with 19 additions and 8 deletions

View File

@ -32,6 +32,7 @@ Enhancements
* Added `FnPtr::call` and `FnPtr::call_within_context` to simplify calling a function pointer. * Added `FnPtr::call` and `FnPtr::call_within_context` to simplify calling a function pointer.
* BLob's can now be deserialized (using `from_dynamic`) into `Vec<u8>` via [`serde_bytes`](https://crates.io/crates/serde_bytes). * BLob's can now be deserialized (using `from_dynamic`) into `Vec<u8>` via [`serde_bytes`](https://crates.io/crates/serde_bytes).
* A function's hashes are included in its JSON metadata to assist in debugging. Each function's `hashBase` field in the JSON object should map directly to the pre-calculated hash in the function call. * A function's hashes are included in its JSON metadata to assist in debugging. Each function's `hashBase` field in the JSON object should map directly to the pre-calculated hash in the function call.
* `Expression` now derefs to `Expr`.
Deprecated and Gated API's Deprecated and Gated API's
-------------------------- --------------------------

View File

@ -10,9 +10,9 @@ use crate::{
Engine, Identifier, ImmutableString, LexError, ParseError, Position, RhaiResult, Shared, Engine, Identifier, ImmutableString, LexError, ParseError, Position, RhaiResult, Shared,
StaticVec, INT, StaticVec, INT,
}; };
use std::any::TypeId;
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
use std::{any::TypeId, ops::Deref};
/// Collection of special markers for custom syntax definition. /// Collection of special markers for custom syntax definition.
pub mod markers { pub mod markers {
@ -71,12 +71,6 @@ impl Expression<'_> {
pub fn get_variable_name(&self) -> Option<&str> { pub fn get_variable_name(&self) -> Option<&str> {
self.0.get_variable_name(true) self.0.get_variable_name(true)
} }
/// Get the expression.
#[inline(always)]
#[must_use]
pub(crate) const fn expr(&self) -> &Expr {
&self.0
}
/// Get the position of this expression. /// Get the position of this expression.
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
@ -134,6 +128,22 @@ impl Expression<'_> {
} }
} }
impl AsRef<Expr> for Expression<'_> {
#[inline(always)]
fn as_ref(&self) -> &Expr {
&self.0
}
}
impl Deref for Expression<'_> {
type Target = Expr;
#[inline(always)]
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl EvalContext<'_, '_, '_, '_, '_, '_, '_, '_> { impl EvalContext<'_, '_, '_, '_, '_, '_, '_, '_> {
/// Evaluate an [expression tree][Expression]. /// Evaluate an [expression tree][Expression].
/// ///
@ -148,7 +158,7 @@ impl EvalContext<'_, '_, '_, '_, '_, '_, '_, '_> {
self.state, self.state,
self.lib, self.lib,
self.this_ptr, self.this_ptr,
expr.expr(), expr,
self.level, self.level,
) )
} }