Merge use.

This commit is contained in:
Stephen Chung 2022-04-13 10:35:10 +08:00
parent c5015d5e86
commit 7788e1058a
10 changed files with 32 additions and 43 deletions

View File

@ -4,6 +4,7 @@ use crate::{
Dynamic, Engine, EvalAltResult, Expression, FnPtr, ImmutableString, NativeCallContext, Dynamic, Engine, EvalAltResult, Expression, FnPtr, ImmutableString, NativeCallContext,
Position, RhaiResult, RhaiResultOf, Scope, AST, Position, RhaiResult, RhaiResultOf, Scope, AST,
}; };
use std::path::PathBuf;
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
@ -22,7 +23,7 @@ impl Engine {
#[cfg(not(feature = "no_std"))] #[cfg(not(feature = "no_std"))]
#[cfg(not(target_family = "wasm"))] #[cfg(not(target_family = "wasm"))]
#[inline(always)] #[inline(always)]
pub fn consume_file(&self, path: std::path::PathBuf) -> RhaiResultOf<()> { pub fn consume_file(&self, path: PathBuf) -> RhaiResultOf<()> {
self.run_file(path) self.run_file(path)
} }
@ -40,11 +41,7 @@ impl Engine {
#[cfg(not(feature = "no_std"))] #[cfg(not(feature = "no_std"))]
#[cfg(not(target_family = "wasm"))] #[cfg(not(target_family = "wasm"))]
#[inline(always)] #[inline(always)]
pub fn consume_file_with_scope( pub fn consume_file_with_scope(&self, scope: &mut Scope, path: PathBuf) -> RhaiResultOf<()> {
&self,
scope: &mut Scope,
path: std::path::PathBuf,
) -> RhaiResultOf<()> {
self.run_file_with_scope(scope, path) self.run_file_with_scope(scope, path)
} }

View File

@ -6,13 +6,12 @@ use crate::types::dynamic::Variant;
use crate::{Engine, RhaiResultOf, Scope, AST, ERR}; use crate::{Engine, RhaiResultOf, Scope, AST, ERR};
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
use std::{fs::File, io::Read, path::PathBuf};
impl Engine { impl Engine {
/// Read the contents of a file into a string. /// Read the contents of a file into a string.
fn read_file(path: std::path::PathBuf) -> RhaiResultOf<String> { fn read_file(path: PathBuf) -> RhaiResultOf<String> {
use std::io::Read; let mut f = File::open(path.clone()).map_err(|err| {
let mut f = std::fs::File::open(path.clone()).map_err(|err| {
ERR::ErrorSystem( ERR::ErrorSystem(
format!("Cannot open script file '{}'", path.to_string_lossy()), format!("Cannot open script file '{}'", path.to_string_lossy()),
err.into(), err.into(),
@ -62,7 +61,7 @@ impl Engine {
/// # } /// # }
/// ``` /// ```
#[inline(always)] #[inline(always)]
pub fn compile_file(&self, path: std::path::PathBuf) -> RhaiResultOf<AST> { pub fn compile_file(&self, path: PathBuf) -> RhaiResultOf<AST> {
self.compile_file_with_scope(&Scope::new(), path) self.compile_file_with_scope(&Scope::new(), path)
} }
/// Compile a script file into an [`AST`] using own scope, which can be used later for evaluation. /// Compile a script file into an [`AST`] using own scope, which can be used later for evaluation.
@ -100,11 +99,7 @@ impl Engine {
/// # } /// # }
/// ``` /// ```
#[inline] #[inline]
pub fn compile_file_with_scope( pub fn compile_file_with_scope(&self, scope: &Scope, path: PathBuf) -> RhaiResultOf<AST> {
&self,
scope: &Scope,
path: std::path::PathBuf,
) -> RhaiResultOf<AST> {
Self::read_file(path).and_then(|contents| Ok(self.compile_with_scope(scope, &contents)?)) Self::read_file(path).and_then(|contents| Ok(self.compile_with_scope(scope, &contents)?))
} }
/// Evaluate a script file. /// Evaluate a script file.
@ -125,7 +120,7 @@ impl Engine {
/// # } /// # }
/// ``` /// ```
#[inline] #[inline]
pub fn eval_file<T: Variant + Clone>(&self, path: std::path::PathBuf) -> RhaiResultOf<T> { pub fn eval_file<T: Variant + Clone>(&self, path: PathBuf) -> RhaiResultOf<T> {
Self::read_file(path).and_then(|contents| self.eval::<T>(&contents)) Self::read_file(path).and_then(|contents| self.eval::<T>(&contents))
} }
/// Evaluate a script file with own scope. /// Evaluate a script file with own scope.
@ -160,7 +155,7 @@ impl Engine {
pub fn eval_file_with_scope<T: Variant + Clone>( pub fn eval_file_with_scope<T: Variant + Clone>(
&self, &self,
scope: &mut Scope, scope: &mut Scope,
path: std::path::PathBuf, path: PathBuf,
) -> RhaiResultOf<T> { ) -> RhaiResultOf<T> {
Self::read_file(path).and_then(|contents| self.eval_with_scope(scope, &contents)) Self::read_file(path).and_then(|contents| self.eval_with_scope(scope, &contents))
} }
@ -168,7 +163,7 @@ impl Engine {
/// ///
/// Not available under `no_std` or `WASM`. /// Not available under `no_std` or `WASM`.
#[inline] #[inline]
pub fn run_file(&self, path: std::path::PathBuf) -> RhaiResultOf<()> { pub fn run_file(&self, path: PathBuf) -> RhaiResultOf<()> {
Self::read_file(path).and_then(|contents| self.run(&contents)) Self::read_file(path).and_then(|contents| self.run(&contents))
} }
/// Evaluate a file with own scope, returning any error (if any). /// Evaluate a file with own scope, returning any error (if any).
@ -182,11 +177,7 @@ impl Engine {
/// ///
/// This allows functions to be optimized based on dynamic global constants. /// This allows functions to be optimized based on dynamic global constants.
#[inline] #[inline]
pub fn run_file_with_scope( pub fn run_file_with_scope(&self, scope: &mut Scope, path: PathBuf) -> RhaiResultOf<()> {
&self,
scope: &mut Scope,
path: std::path::PathBuf,
) -> RhaiResultOf<()> {
Self::read_file(path).and_then(|contents| self.run_with_scope(scope, &contents)) Self::read_file(path).and_then(|contents| self.run_with_scope(scope, &contents))
} }
} }

View File

@ -27,7 +27,7 @@ pub struct Limits {
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
pub max_function_expr_depth: Option<NonZeroUsize>, pub max_function_expr_depth: Option<NonZeroUsize>,
/// Maximum number of operations allowed to run. /// Maximum number of operations allowed to run.
pub max_operations: Option<std::num::NonZeroU64>, pub max_operations: Option<NonZeroU64>,
/// Maximum number of [modules][crate::Module] allowed to load. /// Maximum number of [modules][crate::Module] allowed to load.
/// ///
/// Set to zero to effectively disable loading any [module][crate::Module]. /// Set to zero to effectively disable loading any [module][crate::Module].

View File

@ -8,6 +8,7 @@ use std::{
fmt, fmt,
hash::Hash, hash::Hash,
ops::{Add, AddAssign}, ops::{Add, AddAssign},
ptr,
}; };
/// Compiled AST (abstract syntax tree) of a Rhai script. /// Compiled AST (abstract syntax tree) of a Rhai script.
@ -870,8 +871,8 @@ impl PartialEq for ASTNode<'_> {
#[inline(always)] #[inline(always)]
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
match (self, other) { match (self, other) {
(Self::Stmt(x), Self::Stmt(y)) => std::ptr::eq(*x, *y), (Self::Stmt(x), Self::Stmt(y)) => ptr::eq(*x, *y),
(Self::Expr(x), Self::Expr(y)) => std::ptr::eq(*x, *y), (Self::Expr(x), Self::Expr(y)) => ptr::eq(*x, *y),
_ => false, _ => false,
} }
} }

View File

@ -17,7 +17,11 @@ use std::{
}; };
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
use std::str::FromStr; use std::{
hash::Hasher,
ops::{Deref, DerefMut},
str::FromStr,
};
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
use num_traits::float::FloatCore as Float; use num_traits::float::FloatCore as Float;
@ -230,7 +234,7 @@ pub struct FloatWrapper<F>(F);
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
impl Hash for FloatWrapper<crate::FLOAT> { impl Hash for FloatWrapper<crate::FLOAT> {
#[inline(always)] #[inline(always)]
fn hash<H: std::hash::Hasher>(&self, state: &mut H) { fn hash<H: Hasher>(&self, state: &mut H) {
self.0.to_ne_bytes().hash(state); self.0.to_ne_bytes().hash(state);
} }
} }
@ -252,7 +256,7 @@ impl<F: Float> AsMut<F> for FloatWrapper<F> {
} }
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
impl<F: Float> std::ops::Deref for FloatWrapper<F> { impl<F: Float> Deref for FloatWrapper<F> {
type Target = F; type Target = F;
#[inline(always)] #[inline(always)]
@ -262,7 +266,7 @@ impl<F: Float> std::ops::Deref for FloatWrapper<F> {
} }
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
impl<F: Float> std::ops::DerefMut for FloatWrapper<F> { impl<F: Float> DerefMut for FloatWrapper<F> {
#[inline(always)] #[inline(always)]
fn deref_mut(&mut self) -> &mut Self::Target { fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0 &mut self.0

View File

@ -6,7 +6,7 @@ use crate::ast::{ASTNode, Expr, Stmt};
use crate::{Dynamic, Engine, EvalAltResult, Identifier, Module, Position, RhaiResultOf, Scope}; use crate::{Dynamic, Engine, EvalAltResult, Identifier, Module, Position, RhaiResultOf, Scope};
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
use std::{fmt, mem}; use std::{fmt, iter::repeat, mem};
/// Callback function to initialize the debugger. /// Callback function to initialize the debugger.
#[cfg(not(feature = "sync"))] #[cfg(not(feature = "sync"))]
@ -160,10 +160,7 @@ impl fmt::Display for BreakPoint {
f, f,
"{} ({})", "{} ({})",
fn_name, fn_name,
std::iter::repeat("_") repeat("_").take(*args).collect::<Vec<_>>().join(", ")
.take(*args)
.collect::<Vec<_>>()
.join(", ")
)?; )?;
if !*enabled { if !*enabled {
f.write_str(" (disabled)")?; f.write_str(" (disabled)")?;

View File

@ -2,10 +2,9 @@
use crate::func::call::FnResolutionCache; use crate::func::call::FnResolutionCache;
use crate::StaticVec; use crate::StaticVec;
use std::collections::BTreeMap;
use std::marker::PhantomData;
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
use std::{collections::BTreeMap, marker::PhantomData};
/// _(internals)_ A type that holds all the current states of the [`Engine`][crate::Engine]. /// _(internals)_ A type that holds all the current states of the [`Engine`][crate::Engine].
/// Exported under the `internals` feature only. /// Exported under the `internals` feature only.

View File

@ -48,7 +48,7 @@ pub fn by_value<T: Variant + Clone>(data: &mut Dynamic) -> T {
// # Safety // # Safety
// //
// We already checked that `T` is `&str`, so it is safe to cast here. // We already checked that `T` is `&str`, so it is safe to cast here.
return unsafe { std::mem::transmute_copy::<_, T>(&ref_str) }; return unsafe { mem::transmute_copy::<_, T>(&ref_str) };
} }
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

View File

@ -1,7 +1,7 @@
use crate::{Engine, Module, ModuleResolver, Position, RhaiResultOf, Shared, ERR}; use crate::{Engine, Module, ModuleResolver, Position, RhaiResultOf, Shared, ERR};
use std::ops::AddAssign;
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
use std::{ops::AddAssign, vec::IntoIter};
/// [Module] resolution service that holds a collection of module resolvers, /// [Module] resolution service that holds a collection of module resolvers,
/// to be searched in sequential order. /// to be searched in sequential order.
@ -108,7 +108,7 @@ impl ModuleResolversCollection {
impl IntoIterator for ModuleResolversCollection { impl IntoIterator for ModuleResolversCollection {
type Item = Box<dyn ModuleResolver>; type Item = Box<dyn ModuleResolver>;
type IntoIter = std::vec::IntoIter<Box<dyn ModuleResolver>>; type IntoIter = IntoIter<Box<dyn ModuleResolver>>;
#[inline(always)] #[inline(always)]
fn into_iter(self) -> Self::IntoIter { fn into_iter(self) -> Self::IntoIter {

View File

@ -3,7 +3,7 @@ use crate::{
}; };
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
use std::{collections::BTreeMap, ops::AddAssign}; use std::{collections::btree_map::IntoIter, collections::BTreeMap, ops::AddAssign};
/// A static [module][Module] resolution service that serves [modules][Module] added into it. /// A static [module][Module] resolution service that serves [modules][Module] added into it.
/// ///
@ -120,7 +120,7 @@ impl StaticModuleResolver {
impl IntoIterator for StaticModuleResolver { impl IntoIterator for StaticModuleResolver {
type Item = (Identifier, Shared<Module>); type Item = (Identifier, Shared<Module>);
type IntoIter = std::collections::btree_map::IntoIter<SmartString, Shared<Module>>; type IntoIter = IntoIter<SmartString, Shared<Module>>;
fn into_iter(self) -> Self::IntoIter { fn into_iter(self) -> Self::IntoIter {
self.0.into_iter() self.0.into_iter()