Reduce unnecessary generics.

This commit is contained in:
Stephen Chung 2022-10-20 15:31:57 +08:00
parent f8888c83e7
commit c24794187f
8 changed files with 88 additions and 30 deletions

View File

@ -10,9 +10,9 @@ use crate::{
reify, Dynamic, Engine, EvalContext, Identifier, ImmutableString, LexError, Position, reify, Dynamic, Engine, EvalContext, Identifier, ImmutableString, LexError, Position,
RhaiResult, StaticVec, RhaiResult, StaticVec,
}; };
use std::ops::Deref;
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
use std::{borrow::Borrow, ops::Deref};
/// Collection of special markers for custom syntax definition. /// Collection of special markers for custom syntax definition.
pub mod markers { pub mod markers {
@ -145,6 +145,14 @@ impl Expression<'_> {
} }
} }
impl Borrow<Expr> for Expression<'_> {
#[inline(always)]
#[must_use]
fn borrow(&self) -> &Expr {
self.0
}
}
impl AsRef<Expr> for Expression<'_> { impl AsRef<Expr> for Expression<'_> {
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]

View File

@ -5,6 +5,7 @@ use crate::{Dynamic, FnNamespace, Identifier, Position};
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
use std::{ use std::{
borrow::Borrow,
fmt, fmt,
hash::Hash, hash::Hash,
ops::{Add, AddAssign}, ops::{Add, AddAssign},
@ -919,6 +920,14 @@ impl<A: Into<Self>> AddAssign<A> for AST {
} }
} }
impl Borrow<[Stmt]> for AST {
#[inline(always)]
#[must_use]
fn borrow(&self) -> &[Stmt] {
self.statements()
}
}
impl AsRef<[Stmt]> for AST { impl AsRef<[Stmt]> for AST {
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
@ -927,6 +936,15 @@ impl AsRef<[Stmt]> for AST {
} }
} }
#[cfg(not(feature = "no_function"))]
impl Borrow<crate::Module> for AST {
#[inline(always)]
#[must_use]
fn borrow(&self) -> &crate::Module {
&self.shared_lib()
}
}
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
impl AsRef<crate::Module> for AST { impl AsRef<crate::Module> for AST {
#[inline(always)] #[inline(always)]
@ -936,6 +954,15 @@ impl AsRef<crate::Module> for AST {
} }
} }
#[cfg(not(feature = "no_function"))]
impl Borrow<crate::Shared<crate::Module>> for AST {
#[inline(always)]
#[must_use]
fn borrow(&self) -> &crate::Shared<crate::Module> {
self.shared_lib()
}
}
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
impl AsRef<crate::Shared<crate::Module>> for AST { impl AsRef<crate::Shared<crate::Module>> for AST {
#[inline(always)] #[inline(always)]

View File

@ -4,6 +4,7 @@ use crate::{ImmutableString, Position};
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
use std::{ use std::{
borrow::Borrow,
fmt, fmt,
hash::Hash, hash::Hash,
ops::{Deref, DerefMut}, ops::{Deref, DerefMut},
@ -28,6 +29,14 @@ impl fmt::Debug for Ident {
} }
} }
impl Borrow<str> for Ident {
#[inline(always)]
#[must_use]
fn borrow(&self) -> &str {
self.name.as_ref()
}
}
impl AsRef<str> for Ident { impl AsRef<str> for Ident {
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]

View File

@ -7,6 +7,7 @@ use crate::{calc_fn_hash, Position, StaticVec, INT};
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
use std::{ use std::{
borrow::Borrow,
collections::BTreeMap, collections::BTreeMap,
fmt, fmt,
hash::Hash, 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 { impl AsRef<[Stmt]> for StmtBlock {
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]

View File

@ -251,7 +251,7 @@ impl Engine {
get_builtin_binary_op_fn(operator_token.as_ref().unwrap(), operands[0], operands[1]) get_builtin_binary_op_fn(operator_token.as_ref().unwrap(), operands[0], operands[1])
{ {
// Built-in found // 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); return func(context, operands);
} }

View File

@ -2,9 +2,12 @@
use crate::types::dynamic::Variant; use crate::types::dynamic::Variant;
use crate::{Dynamic, Position, RhaiResultOf}; use crate::{Dynamic, Position, RhaiResultOf};
use std::ops::{Deref, DerefMut};
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
use std::{
borrow::Borrow,
ops::{Deref, DerefMut},
};
// Calculate an offset+len pair given an actual length of the underlying array. // Calculate an offset+len pair given an actual length of the underlying array.
// //
@ -422,6 +425,14 @@ impl AsRef<Dynamic> for Target<'_> {
} }
} }
impl Borrow<Dynamic> for Target<'_> {
#[inline(always)]
#[must_use]
fn borrow(&self) -> &Dynamic {
self
}
}
impl DerefMut for Target<'_> { impl DerefMut for Target<'_> {
#[inline] #[inline]
fn deref_mut(&mut self) -> &mut Dynamic { fn deref_mut(&mut self) -> &mut Dynamic {

View File

@ -81,13 +81,13 @@ pub struct NativeCallContext<'a> {
level: usize, level: usize,
} }
impl<'a, M: AsRef<[&'a Module]> + ?Sized, S: AsRef<str> + 'a + ?Sized> impl<'a>
From<( From<(
&'a Engine, &'a Engine,
&'a S, &'a str,
Option<&'a S>, Option<&'a str>,
&'a GlobalRuntimeState<'a>, &'a GlobalRuntimeState<'a>,
&'a M, &'a [&Module],
Position, Position,
usize, usize,
)> for NativeCallContext<'a> )> for NativeCallContext<'a>
@ -96,37 +96,35 @@ impl<'a, M: AsRef<[&'a Module]> + ?Sized, S: AsRef<str> + 'a + ?Sized>
fn from( fn from(
value: ( value: (
&'a Engine, &'a Engine,
&'a S, &'a str,
Option<&'a S>, Option<&'a str>,
&'a GlobalRuntimeState, &'a GlobalRuntimeState,
&'a M, &'a [&Module],
Position, Position,
usize, usize,
), ),
) -> Self { ) -> Self {
Self { Self {
engine: value.0, engine: value.0,
fn_name: value.1.as_ref(), fn_name: value.1,
source: value.2.map(<_>::as_ref), source: value.2,
global: Some(value.3), global: Some(value.3),
lib: value.4.as_ref(), lib: value.4,
pos: value.5, pos: value.5,
level: value.6, level: value.6,
} }
} }
} }
impl<'a, M: AsRef<[&'a Module]> + ?Sized, S: AsRef<str> + 'a + ?Sized> impl<'a> From<(&'a Engine, &'a str, &'a [&'a Module])> for NativeCallContext<'a> {
From<(&'a Engine, &'a S, &'a M)> for NativeCallContext<'a>
{
#[inline(always)] #[inline(always)]
fn from(value: (&'a Engine, &'a S, &'a M)) -> Self { fn from(value: (&'a Engine, &'a str, &'a [&Module])) -> Self {
Self { Self {
engine: value.0, engine: value.0,
fn_name: value.1.as_ref(), fn_name: value.1,
source: None, source: None,
global: None, global: None,
lib: value.2.as_ref(), lib: value.2,
pos: Position::NONE, pos: Position::NONE,
level: 0, level: 0,
} }
@ -142,14 +140,10 @@ impl<'a> NativeCallContext<'a> {
)] )]
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
pub fn new( pub fn new(engine: &'a Engine, fn_name: &'a str, lib: &'a [&Module]) -> Self {
engine: &'a Engine,
fn_name: &'a (impl AsRef<str> + 'a + ?Sized),
lib: &'a [&Module],
) -> Self {
Self { Self {
engine, engine,
fn_name: fn_name.as_ref(), fn_name,
source: None, source: None,
global: None, global: None,
lib, lib,
@ -167,8 +161,8 @@ impl<'a> NativeCallContext<'a> {
#[must_use] #[must_use]
pub fn new_with_all_fields( pub fn new_with_all_fields(
engine: &'a Engine, engine: &'a Engine,
fn_name: &'a (impl AsRef<str> + 'a + ?Sized), fn_name: &'a str,
source: Option<&'a (impl AsRef<str> + 'a + ?Sized)>, source: Option<&'a str>,
global: &'a GlobalRuntimeState, global: &'a GlobalRuntimeState,
lib: &'a [&Module], lib: &'a [&Module],
pos: Position, pos: Position,
@ -176,8 +170,8 @@ impl<'a> NativeCallContext<'a> {
) -> Self { ) -> Self {
Self { Self {
engine, engine,
fn_name: fn_name.as_ref(), fn_name,
source: source.map(<_>::as_ref), source,
global: Some(global), global: Some(global),
lib, lib,
pos, pos,

View File

@ -1191,7 +1191,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, _chaining: bool) {
#[cfg(feature = "no_function")] #[cfg(feature = "no_function")]
let lib = &[]; 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(); let (first, second) = arg_values.split_first_mut().unwrap();
(f)(context, &mut [ first, &mut second[0] ]).ok() (f)(context, &mut [ first, &mut second[0] ]).ok()
}) { }) {