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,
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<Expr> for Expression<'_> {
#[inline(always)]
#[must_use]
fn borrow(&self) -> &Expr {
self.0
}
}
impl AsRef<Expr> for Expression<'_> {
#[inline(always)]
#[must_use]

View File

@ -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<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 {
#[inline(always)]
#[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"))]
impl AsRef<crate::Module> for AST {
#[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"))]
impl AsRef<crate::Shared<crate::Module>> for AST {
#[inline(always)]

View File

@ -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<str> for Ident {
#[inline(always)]
#[must_use]
fn borrow(&self) -> &str {
self.name.as_ref()
}
}
impl AsRef<str> for Ident {
#[inline(always)]
#[must_use]

View File

@ -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]

View File

@ -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);
}

View File

@ -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<Dynamic> for Target<'_> {
}
}
impl Borrow<Dynamic> for Target<'_> {
#[inline(always)]
#[must_use]
fn borrow(&self) -> &Dynamic {
self
}
}
impl DerefMut for Target<'_> {
#[inline]
fn deref_mut(&mut self) -> &mut Dynamic {

View File

@ -81,13 +81,13 @@ pub struct NativeCallContext<'a> {
level: usize,
}
impl<'a, M: AsRef<[&'a Module]> + ?Sized, S: AsRef<str> + '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<str> + '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<str> + '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<str> + '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<str> + 'a + ?Sized),
source: Option<&'a (impl AsRef<str> + '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,

View File

@ -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()
}) {