diff --git a/src/api/custom_syntax.rs b/src/api/custom_syntax.rs index 7ee568c0..868b9931 100644 --- a/src/api/custom_syntax.rs +++ b/src/api/custom_syntax.rs @@ -10,9 +10,9 @@ use crate::{ reify, Dynamic, Engine, EvalContext, Identifier, ImmutableString, LexError, Position, RhaiResult, StaticVec, }; -use std::ops::Deref; #[cfg(feature = "no_std")] use std::prelude::v1::*; +use std::{borrow::Borrow, ops::Deref}; /// Collection of special markers for custom syntax definition. pub mod markers { @@ -145,6 +145,14 @@ impl Expression<'_> { } } +impl Borrow for Expression<'_> { + #[inline(always)] + #[must_use] + fn borrow(&self) -> &Expr { + self.0 + } +} + impl AsRef for Expression<'_> { #[inline(always)] #[must_use] diff --git a/src/ast/ast.rs b/src/ast/ast.rs index d0b88e26..98ed86b3 100644 --- a/src/ast/ast.rs +++ b/src/ast/ast.rs @@ -5,6 +5,7 @@ use crate::{Dynamic, FnNamespace, Identifier, Position}; #[cfg(feature = "no_std")] use std::prelude::v1::*; use std::{ + borrow::Borrow, fmt, hash::Hash, ops::{Add, AddAssign}, @@ -919,6 +920,14 @@ impl> AddAssign for AST { } } +impl Borrow<[Stmt]> for AST { + #[inline(always)] + #[must_use] + fn borrow(&self) -> &[Stmt] { + self.statements() + } +} + impl AsRef<[Stmt]> for AST { #[inline(always)] #[must_use] @@ -927,6 +936,15 @@ impl AsRef<[Stmt]> for AST { } } +#[cfg(not(feature = "no_function"))] +impl Borrow for AST { + #[inline(always)] + #[must_use] + fn borrow(&self) -> &crate::Module { + &self.shared_lib() + } +} + #[cfg(not(feature = "no_function"))] impl AsRef for AST { #[inline(always)] @@ -936,6 +954,15 @@ impl AsRef for AST { } } +#[cfg(not(feature = "no_function"))] +impl Borrow> for AST { + #[inline(always)] + #[must_use] + fn borrow(&self) -> &crate::Shared { + self.shared_lib() + } +} + #[cfg(not(feature = "no_function"))] impl AsRef> for AST { #[inline(always)] diff --git a/src/ast/ident.rs b/src/ast/ident.rs index e8eb6626..ffc5f968 100644 --- a/src/ast/ident.rs +++ b/src/ast/ident.rs @@ -4,6 +4,7 @@ use crate::{ImmutableString, Position}; #[cfg(feature = "no_std")] use std::prelude::v1::*; use std::{ + borrow::Borrow, fmt, hash::Hash, ops::{Deref, DerefMut}, @@ -28,6 +29,14 @@ impl fmt::Debug for Ident { } } +impl Borrow for Ident { + #[inline(always)] + #[must_use] + fn borrow(&self) -> &str { + self.name.as_ref() + } +} + impl AsRef for Ident { #[inline(always)] #[must_use] diff --git a/src/ast/stmt.rs b/src/ast/stmt.rs index a72e2f01..6b15061c 100644 --- a/src/ast/stmt.rs +++ b/src/ast/stmt.rs @@ -7,6 +7,7 @@ use crate::{calc_fn_hash, Position, StaticVec, INT}; #[cfg(feature = "no_std")] use std::prelude::v1::*; use std::{ + borrow::Borrow, collections::BTreeMap, fmt, hash::Hash, @@ -443,6 +444,14 @@ impl DerefMut for StmtBlock { } } +impl Borrow<[Stmt]> for StmtBlock { + #[inline(always)] + #[must_use] + fn borrow(&self) -> &[Stmt] { + &self.block + } +} + impl AsRef<[Stmt]> for StmtBlock { #[inline(always)] #[must_use] diff --git a/src/eval/expr.rs b/src/eval/expr.rs index 285f5e23..c8d17896 100644 --- a/src/eval/expr.rs +++ b/src/eval/expr.rs @@ -251,7 +251,7 @@ impl Engine { get_builtin_binary_op_fn(operator_token.as_ref().unwrap(), operands[0], operands[1]) { // Built-in found - let context = (self, name, None, &*global, lib, pos, level + 1).into(); + let context = (self, name.as_str(), None, &*global, lib, pos, level + 1).into(); return func(context, operands); } diff --git a/src/eval/target.rs b/src/eval/target.rs index 5a441520..c48aa62a 100644 --- a/src/eval/target.rs +++ b/src/eval/target.rs @@ -2,9 +2,12 @@ use crate::types::dynamic::Variant; use crate::{Dynamic, Position, RhaiResultOf}; -use std::ops::{Deref, DerefMut}; #[cfg(feature = "no_std")] use std::prelude::v1::*; +use std::{ + borrow::Borrow, + ops::{Deref, DerefMut}, +}; // Calculate an offset+len pair given an actual length of the underlying array. // @@ -422,6 +425,14 @@ impl AsRef for Target<'_> { } } +impl Borrow for Target<'_> { + #[inline(always)] + #[must_use] + fn borrow(&self) -> &Dynamic { + self + } +} + impl DerefMut for Target<'_> { #[inline] fn deref_mut(&mut self) -> &mut Dynamic { diff --git a/src/func/native.rs b/src/func/native.rs index b3681e74..707f8f4c 100644 --- a/src/func/native.rs +++ b/src/func/native.rs @@ -81,13 +81,13 @@ pub struct NativeCallContext<'a> { level: usize, } -impl<'a, M: AsRef<[&'a Module]> + ?Sized, S: AsRef + 'a + ?Sized> +impl<'a> From<( &'a Engine, - &'a S, - Option<&'a S>, + &'a str, + Option<&'a str>, &'a GlobalRuntimeState<'a>, - &'a M, + &'a [&Module], Position, usize, )> for NativeCallContext<'a> @@ -96,37 +96,35 @@ impl<'a, M: AsRef<[&'a Module]> + ?Sized, S: AsRef + 'a + ?Sized> fn from( value: ( &'a Engine, - &'a S, - Option<&'a S>, + &'a str, + Option<&'a str>, &'a GlobalRuntimeState, - &'a M, + &'a [&Module], Position, usize, ), ) -> Self { Self { engine: value.0, - fn_name: value.1.as_ref(), - source: value.2.map(<_>::as_ref), + fn_name: value.1, + source: value.2, global: Some(value.3), - lib: value.4.as_ref(), + lib: value.4, pos: value.5, level: value.6, } } } -impl<'a, M: AsRef<[&'a Module]> + ?Sized, S: AsRef + 'a + ?Sized> - From<(&'a Engine, &'a S, &'a M)> for NativeCallContext<'a> -{ +impl<'a> From<(&'a Engine, &'a str, &'a [&'a Module])> for NativeCallContext<'a> { #[inline(always)] - fn from(value: (&'a Engine, &'a S, &'a M)) -> Self { + fn from(value: (&'a Engine, &'a str, &'a [&Module])) -> Self { Self { engine: value.0, - fn_name: value.1.as_ref(), + fn_name: value.1, source: None, global: None, - lib: value.2.as_ref(), + lib: value.2, pos: Position::NONE, level: 0, } @@ -142,14 +140,10 @@ impl<'a> NativeCallContext<'a> { )] #[inline(always)] #[must_use] - pub fn new( - engine: &'a Engine, - fn_name: &'a (impl AsRef + 'a + ?Sized), - lib: &'a [&Module], - ) -> Self { + pub fn new(engine: &'a Engine, fn_name: &'a str, lib: &'a [&Module]) -> Self { Self { engine, - fn_name: fn_name.as_ref(), + fn_name, source: None, global: None, lib, @@ -167,8 +161,8 @@ impl<'a> NativeCallContext<'a> { #[must_use] pub fn new_with_all_fields( engine: &'a Engine, - fn_name: &'a (impl AsRef + 'a + ?Sized), - source: Option<&'a (impl AsRef + 'a + ?Sized)>, + fn_name: &'a str, + source: Option<&'a str>, global: &'a GlobalRuntimeState, lib: &'a [&Module], pos: Position, @@ -176,8 +170,8 @@ impl<'a> NativeCallContext<'a> { ) -> Self { Self { engine, - fn_name: fn_name.as_ref(), - source: source.map(<_>::as_ref), + fn_name, + source, global: Some(global), lib, pos, diff --git a/src/optimizer.rs b/src/optimizer.rs index 20145659..42e9dd90 100644 --- a/src/optimizer.rs +++ b/src/optimizer.rs @@ -1191,7 +1191,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, _chaining: bool) { #[cfg(feature = "no_function")] let lib = &[]; - let context = (state.engine, &x.name, lib).into(); + let context = (state.engine, x.name.as_str(), lib).into(); let (first, second) = arg_values.split_first_mut().unwrap(); (f)(context, &mut [ first, &mut second[0] ]).ok() }) {