Make all public API's return Box<EvalAltResult> to reduce footprint.

This commit is contained in:
Stephen Chung
2020-04-21 23:25:12 +08:00
parent 0a75479637
commit 69733688bf
63 changed files with 337 additions and 303 deletions

View File

@@ -450,7 +450,7 @@ impl Dynamic {
/// Cast the `Dynamic` as the system integer type `INT` and return it.
/// Returns the name of the actual type if the cast fails.
pub(crate) fn as_int(&self) -> Result<INT, &'static str> {
pub fn as_int(&self) -> Result<INT, &'static str> {
match self.0 {
Union::Int(n) => Ok(n),
_ => Err(self.type_name()),
@@ -459,7 +459,7 @@ impl Dynamic {
/// Cast the `Dynamic` as a `bool` and return it.
/// Returns the name of the actual type if the cast fails.
pub(crate) fn as_bool(&self) -> Result<bool, &'static str> {
pub fn as_bool(&self) -> Result<bool, &'static str> {
match self.0 {
Union::Bool(b) => Ok(b),
_ => Err(self.type_name()),
@@ -468,7 +468,7 @@ impl Dynamic {
/// Cast the `Dynamic` as a `char` and return it.
/// Returns the name of the actual type if the cast fails.
pub(crate) fn as_char(&self) -> Result<char, &'static str> {
pub fn as_char(&self) -> Result<char, &'static str> {
match self.0 {
Union::Char(n) => Ok(n),
_ => Err(self.type_name()),
@@ -477,7 +477,7 @@ impl Dynamic {
/// Cast the `Dynamic` as a string and return the string slice.
/// Returns the name of the actual type if the cast fails.
pub(crate) fn as_str(&self) -> Result<&str, &'static str> {
pub fn as_str(&self) -> Result<&str, &'static str> {
match &self.0 {
Union::Str(s) => Ok(s),
_ => Err(self.type_name()),
@@ -486,30 +486,36 @@ impl Dynamic {
/// Convert the `Dynamic` into `String` and return it.
/// Returns the name of the actual type if the cast fails.
pub(crate) fn take_string(self) -> Result<String, &'static str> {
pub fn take_string(self) -> Result<String, &'static str> {
match self.0 {
Union::Str(s) => Ok(*s),
_ => Err(self.type_name()),
}
}
pub(crate) fn from_unit() -> Self {
/// Create a `Dynamic` from `()`.
pub fn from_unit() -> Self {
Self(Union::Unit(()))
}
pub(crate) fn from_bool(value: bool) -> Self {
/// Create a `Dynamic` from a `bool`.
pub fn from_bool(value: bool) -> Self {
Self(Union::Bool(value))
}
pub(crate) fn from_int(value: INT) -> Self {
/// Create a `Dynamic` from an `INT`.
pub fn from_int(value: INT) -> Self {
Self(Union::Int(value))
}
/// Create a `Dynamic` from a `FLOAT`. Not available under `no_float`.
#[cfg(not(feature = "no_float"))]
pub(crate) fn from_float(value: FLOAT) -> Self {
Self(Union::Float(value))
}
pub(crate) fn from_char(value: char) -> Self {
/// Create a `Dynamic` from a `char`.
pub fn from_char(value: char) -> Self {
Self(Union::Char(value))
}
pub(crate) fn from_string(value: String) -> Self {
/// Create a `Dynamic` from a `String`.
pub fn from_string(value: String) -> Self {
Self(Union::Str(Box::new(value)))
}
}

View File

@@ -76,7 +76,7 @@ impl Engine {
/// fn update(&mut self, offset: i64) { self.field += offset; }
/// }
///
/// # fn main() -> Result<(), rhai::EvalAltResult> {
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// use rhai::{Engine, RegisterFn};
///
/// let mut engine = Engine::new();
@@ -116,7 +116,7 @@ impl Engine {
/// fn new() -> Self { TestStruct { field: 1 } }
/// }
///
/// # fn main() -> Result<(), rhai::EvalAltResult> {
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// use rhai::{Engine, RegisterFn};
///
/// let mut engine = Engine::new();
@@ -182,7 +182,7 @@ impl Engine {
/// fn get_field(&mut self) -> i64 { self.field }
/// }
///
/// # fn main() -> Result<(), rhai::EvalAltResult> {
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// use rhai::{Engine, RegisterFn};
///
/// let mut engine = Engine::new();
@@ -224,7 +224,7 @@ impl Engine {
/// fn set_field(&mut self, new_val: i64) { self.field = new_val; }
/// }
///
/// # fn main() -> Result<(), rhai::EvalAltResult> {
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// use rhai::{Engine, RegisterFn};
///
/// let mut engine = Engine::new();
@@ -275,7 +275,7 @@ impl Engine {
/// fn set_field(&mut self, new_val: i64) { self.field = new_val; }
/// }
///
/// # fn main() -> Result<(), rhai::EvalAltResult> {
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// use rhai::{Engine, RegisterFn};
///
/// let mut engine = Engine::new();
@@ -310,7 +310,7 @@ impl Engine {
/// # Example
///
/// ```
/// # fn main() -> Result<(), rhai::EvalAltResult> {
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// use rhai::Engine;
///
/// let engine = Engine::new();
@@ -324,7 +324,7 @@ impl Engine {
/// # Ok(())
/// # }
/// ```
pub fn compile(&self, script: &str) -> Result<AST, ParseError> {
pub fn compile(&self, script: &str) -> Result<AST, Box<ParseError>> {
self.compile_with_scope(&Scope::new(), script)
}
@@ -335,7 +335,7 @@ impl Engine {
/// # Example
///
/// ```
/// # fn main() -> Result<(), rhai::EvalAltResult> {
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// # #[cfg(not(feature = "no_optimize"))]
/// # {
/// use rhai::{Engine, Scope, OptimizationLevel};
@@ -365,7 +365,7 @@ impl Engine {
/// # Ok(())
/// # }
/// ```
pub fn compile_with_scope(&self, scope: &Scope, script: &str) -> Result<AST, ParseError> {
pub fn compile_with_scope(&self, scope: &Scope, script: &str) -> Result<AST, Box<ParseError>> {
self.compile_with_scope_and_optimization_level(scope, script, self.optimization_level)
}
@@ -375,22 +375,22 @@ impl Engine {
scope: &Scope,
script: &str,
optimization_level: OptimizationLevel,
) -> Result<AST, ParseError> {
) -> Result<AST, Box<ParseError>> {
let scripts = [script];
let stream = lex(&scripts);
parse(&mut stream.peekable(), self, scope, optimization_level).map_err(|err| *err)
parse(&mut stream.peekable(), self, scope, optimization_level)
}
/// Read the contents of a file into a string.
#[cfg(not(feature = "no_std"))]
fn read_file(path: PathBuf) -> Result<String, EvalAltResult> {
fn read_file(path: PathBuf) -> Result<String, Box<EvalAltResult>> {
let mut f = File::open(path.clone())
.map_err(|err| EvalAltResult::ErrorReadingScriptFile(path.clone(), err))?;
.map_err(|err| Box::new(EvalAltResult::ErrorReadingScriptFile(path.clone(), err)))?;
let mut contents = String::new();
f.read_to_string(&mut contents)
.map_err(|err| EvalAltResult::ErrorReadingScriptFile(path.clone(), err))?;
.map_err(|err| Box::new(EvalAltResult::ErrorReadingScriptFile(path.clone(), err)))?;
Ok(contents)
}
@@ -400,7 +400,7 @@ impl Engine {
/// # Example
///
/// ```no_run
/// # fn main() -> Result<(), rhai::EvalAltResult> {
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// use rhai::Engine;
///
/// let engine = Engine::new();
@@ -416,7 +416,7 @@ impl Engine {
/// # }
/// ```
#[cfg(not(feature = "no_std"))]
pub fn compile_file(&self, path: PathBuf) -> Result<AST, EvalAltResult> {
pub fn compile_file(&self, path: PathBuf) -> Result<AST, Box<EvalAltResult>> {
self.compile_file_with_scope(&Scope::new(), path)
}
@@ -427,7 +427,7 @@ impl Engine {
/// # Example
///
/// ```no_run
/// # fn main() -> Result<(), rhai::EvalAltResult> {
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// # #[cfg(not(feature = "no_optimize"))]
/// # {
/// use rhai::{Engine, Scope, OptimizationLevel};
@@ -455,7 +455,7 @@ impl Engine {
&self,
scope: &Scope,
path: PathBuf,
) -> Result<AST, EvalAltResult> {
) -> Result<AST, Box<EvalAltResult>> {
Self::read_file(path).and_then(|contents| Ok(self.compile_with_scope(scope, &contents)?))
}
@@ -467,7 +467,7 @@ impl Engine {
/// # Example
///
/// ```
/// # fn main() -> Result<(), rhai::EvalAltResult> {
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// use rhai::Engine;
///
/// let engine = Engine::new();
@@ -483,7 +483,7 @@ impl Engine {
/// # }
/// ```
#[cfg(not(feature = "no_object"))]
pub fn parse_json(&self, json: &str, has_null: bool) -> Result<Map, EvalAltResult> {
pub fn parse_json(&self, json: &str, has_null: bool) -> Result<Map, Box<EvalAltResult>> {
let mut scope = Scope::new();
// Trims the JSON string and add a '#' in front
@@ -510,7 +510,7 @@ impl Engine {
/// # Example
///
/// ```
/// # fn main() -> Result<(), rhai::EvalAltResult> {
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// use rhai::Engine;
///
/// let engine = Engine::new();
@@ -524,7 +524,7 @@ impl Engine {
/// # Ok(())
/// # }
/// ```
pub fn compile_expression(&self, script: &str) -> Result<AST, ParseError> {
pub fn compile_expression(&self, script: &str) -> Result<AST, Box<ParseError>> {
self.compile_expression_with_scope(&Scope::new(), script)
}
@@ -537,7 +537,7 @@ impl Engine {
/// # Example
///
/// ```
/// # fn main() -> Result<(), rhai::EvalAltResult> {
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// # #[cfg(not(feature = "no_optimize"))]
/// # {
/// use rhai::{Engine, Scope, OptimizationLevel};
@@ -571,12 +571,11 @@ impl Engine {
&self,
scope: &Scope,
script: &str,
) -> Result<AST, ParseError> {
) -> Result<AST, Box<ParseError>> {
let scripts = [script];
let stream = lex(&scripts);
parse_global_expr(&mut stream.peekable(), self, scope, self.optimization_level)
.map_err(|err| *err)
}
/// Evaluate a script file.
@@ -584,7 +583,7 @@ impl Engine {
/// # Example
///
/// ```no_run
/// # fn main() -> Result<(), rhai::EvalAltResult> {
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// use rhai::Engine;
///
/// let engine = Engine::new();
@@ -595,7 +594,7 @@ impl Engine {
/// # }
/// ```
#[cfg(not(feature = "no_std"))]
pub fn eval_file<T: Variant + Clone>(&self, path: PathBuf) -> Result<T, EvalAltResult> {
pub fn eval_file<T: Variant + Clone>(&self, path: PathBuf) -> Result<T, Box<EvalAltResult>> {
Self::read_file(path).and_then(|contents| self.eval::<T>(&contents))
}
@@ -604,7 +603,7 @@ impl Engine {
/// # Example
///
/// ```no_run
/// # fn main() -> Result<(), rhai::EvalAltResult> {
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// use rhai::{Engine, Scope};
///
/// let engine = Engine::new();
@@ -623,7 +622,7 @@ impl Engine {
&self,
scope: &mut Scope,
path: PathBuf,
) -> Result<T, EvalAltResult> {
) -> Result<T, Box<EvalAltResult>> {
Self::read_file(path).and_then(|contents| self.eval_with_scope::<T>(scope, &contents))
}
@@ -632,7 +631,7 @@ impl Engine {
/// # Example
///
/// ```
/// # fn main() -> Result<(), rhai::EvalAltResult> {
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// use rhai::Engine;
///
/// let engine = Engine::new();
@@ -641,7 +640,7 @@ impl Engine {
/// # Ok(())
/// # }
/// ```
pub fn eval<T: Variant + Clone>(&self, script: &str) -> Result<T, EvalAltResult> {
pub fn eval<T: Variant + Clone>(&self, script: &str) -> Result<T, Box<EvalAltResult>> {
self.eval_with_scope(&mut Scope::new(), script)
}
@@ -650,7 +649,7 @@ impl Engine {
/// # Example
///
/// ```
/// # fn main() -> Result<(), rhai::EvalAltResult> {
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// use rhai::{Engine, Scope};
///
/// let engine = Engine::new();
@@ -671,7 +670,7 @@ impl Engine {
&self,
scope: &mut Scope,
script: &str,
) -> Result<T, EvalAltResult> {
) -> Result<T, Box<EvalAltResult>> {
// Since the AST will be thrown away afterwards, don't bother to optimize it
let ast =
self.compile_with_scope_and_optimization_level(scope, script, OptimizationLevel::None)?;
@@ -683,7 +682,7 @@ impl Engine {
/// # Example
///
/// ```
/// # fn main() -> Result<(), rhai::EvalAltResult> {
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// use rhai::Engine;
///
/// let engine = Engine::new();
@@ -692,7 +691,10 @@ impl Engine {
/// # Ok(())
/// # }
/// ```
pub fn eval_expression<T: Variant + Clone>(&self, script: &str) -> Result<T, EvalAltResult> {
pub fn eval_expression<T: Variant + Clone>(
&self,
script: &str,
) -> Result<T, Box<EvalAltResult>> {
self.eval_expression_with_scope(&mut Scope::new(), script)
}
@@ -701,7 +703,7 @@ impl Engine {
/// # Example
///
/// ```
/// # fn main() -> Result<(), rhai::EvalAltResult> {
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// use rhai::{Engine, Scope};
///
/// let engine = Engine::new();
@@ -718,7 +720,7 @@ impl Engine {
&self,
scope: &mut Scope,
script: &str,
) -> Result<T, EvalAltResult> {
) -> Result<T, Box<EvalAltResult>> {
let scripts = [script];
let stream = lex(&scripts);
// Since the AST will be thrown away afterwards, don't bother to optimize it
@@ -731,7 +733,7 @@ impl Engine {
/// # Example
///
/// ```
/// # fn main() -> Result<(), rhai::EvalAltResult> {
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// use rhai::Engine;
///
/// let engine = Engine::new();
@@ -744,7 +746,7 @@ impl Engine {
/// # Ok(())
/// # }
/// ```
pub fn eval_ast<T: Variant + Clone>(&self, ast: &AST) -> Result<T, EvalAltResult> {
pub fn eval_ast<T: Variant + Clone>(&self, ast: &AST) -> Result<T, Box<EvalAltResult>> {
self.eval_ast_with_scope(&mut Scope::new(), ast)
}
@@ -753,7 +755,7 @@ impl Engine {
/// # Example
///
/// ```
/// # fn main() -> Result<(), rhai::EvalAltResult> {
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// use rhai::{Engine, Scope};
///
/// let engine = Engine::new();
@@ -781,15 +783,16 @@ impl Engine {
&self,
scope: &mut Scope,
ast: &AST,
) -> Result<T, EvalAltResult> {
let result = self
.eval_ast_with_scope_raw(scope, ast)
.map_err(|err| *err)?;
) -> Result<T, Box<EvalAltResult>> {
let result = self.eval_ast_with_scope_raw(scope, ast)?;
let return_type = self.map_type_name(result.type_name());
return result.try_cast::<T>().ok_or_else(|| {
EvalAltResult::ErrorMismatchOutputType(return_type.to_string(), Position::none())
Box::new(EvalAltResult::ErrorMismatchOutputType(
return_type.to_string(),
Position::none(),
))
});
}
@@ -812,7 +815,7 @@ impl Engine {
/// Evaluate a file, but throw away the result and only return error (if any).
/// Useful for when you don't need the result, but still need to keep track of possible errors.
#[cfg(not(feature = "no_std"))]
pub fn consume_file(&self, path: PathBuf) -> Result<(), EvalAltResult> {
pub fn consume_file(&self, path: PathBuf) -> Result<(), Box<EvalAltResult>> {
Self::read_file(path).and_then(|contents| self.consume(&contents))
}
@@ -823,19 +826,23 @@ impl Engine {
&self,
scope: &mut Scope,
path: PathBuf,
) -> Result<(), EvalAltResult> {
) -> Result<(), Box<EvalAltResult>> {
Self::read_file(path).and_then(|contents| self.consume_with_scope(scope, &contents))
}
/// Evaluate a string, but throw away the result and only return error (if any).
/// Useful for when you don't need the result, but still need to keep track of possible errors.
pub fn consume(&self, script: &str) -> Result<(), EvalAltResult> {
pub fn consume(&self, script: &str) -> Result<(), Box<EvalAltResult>> {
self.consume_with_scope(&mut Scope::new(), script)
}
/// Evaluate a string with own scope, but throw away the result and only return error (if any).
/// Useful for when you don't need the result, but still need to keep track of possible errors.
pub fn consume_with_scope(&self, scope: &mut Scope, script: &str) -> Result<(), EvalAltResult> {
pub fn consume_with_scope(
&self,
scope: &mut Scope,
script: &str,
) -> Result<(), Box<EvalAltResult>> {
let scripts = [script];
let stream = lex(&scripts);
@@ -846,7 +853,7 @@ impl Engine {
/// Evaluate an AST, but throw away the result and only return error (if any).
/// Useful for when you don't need the result, but still need to keep track of possible errors.
pub fn consume_ast(&self, ast: &AST) -> Result<(), EvalAltResult> {
pub fn consume_ast(&self, ast: &AST) -> Result<(), Box<EvalAltResult>> {
self.consume_ast_with_scope(&mut Scope::new(), ast)
}
@@ -856,7 +863,7 @@ impl Engine {
&self,
scope: &mut Scope,
ast: &AST,
) -> Result<(), EvalAltResult> {
) -> Result<(), Box<EvalAltResult>> {
ast.0
.iter()
.try_fold(Dynamic::from_unit(), |_, stmt| {
@@ -865,7 +872,7 @@ impl Engine {
.map_or_else(
|err| match *err {
EvalAltResult::Return(_, _) => Ok(()),
err => Err(err),
err => Err(Box::new(err)),
},
|_| Ok(()),
)
@@ -876,7 +883,7 @@ impl Engine {
/// # Example
///
/// ```
/// # fn main() -> Result<(), rhai::EvalAltResult> {
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// # #[cfg(not(feature = "no_function"))]
/// # {
/// use rhai::{Engine, Scope};
@@ -913,21 +920,22 @@ impl Engine {
ast: &AST,
name: &str,
args: A,
) -> Result<T, EvalAltResult> {
) -> Result<T, Box<EvalAltResult>> {
let mut arg_values = args.into_vec();
let mut args: Vec<_> = arg_values.iter_mut().collect();
let fn_lib = Some(ast.1.as_ref());
let pos = Position::none();
let result = self
.call_fn_raw(Some(scope), fn_lib, name, &mut args, None, pos, 0)
.map_err(|err| *err)?;
let result = self.call_fn_raw(Some(scope), fn_lib, name, &mut args, None, pos, 0)?;
let return_type = self.map_type_name(result.type_name());
return result
.try_cast()
.ok_or_else(|| EvalAltResult::ErrorMismatchOutputType(return_type.into(), pos));
return result.try_cast().ok_or_else(|| {
Box::new(EvalAltResult::ErrorMismatchOutputType(
return_type.into(),
pos,
))
});
}
/// Optimize the `AST` with constants defined in an external Scope.
@@ -961,7 +969,7 @@ impl Engine {
/// # Example
///
/// ```
/// # fn main() -> Result<(), rhai::EvalAltResult> {
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// # use std::sync::RwLock;
/// # use std::sync::Arc;
/// use rhai::Engine;
@@ -989,7 +997,7 @@ impl Engine {
/// # Example
///
/// ```
/// # fn main() -> Result<(), rhai::EvalAltResult> {
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// # use std::sync::RwLock;
/// # use std::sync::Arc;
/// use rhai::Engine;
@@ -1046,7 +1054,7 @@ impl Engine {
/// # Example
///
/// ```
/// # fn main() -> Result<(), rhai::EvalAltResult> {
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// # use std::sync::RwLock;
/// # use std::sync::Arc;
/// use rhai::Engine;

View File

@@ -205,7 +205,7 @@ impl DerefMut for FunctionsLib {
/// Rhai main scripting engine.
///
/// ```
/// # fn main() -> Result<(), rhai::EvalAltResult> {
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// use rhai::Engine;
///
/// let engine = Engine::new();
@@ -1383,13 +1383,11 @@ impl Engine {
// Compile the script text
// No optimizations because we only run it once
let mut ast = self
.compile_with_scope_and_optimization_level(
&Scope::new(),
script,
OptimizationLevel::None,
)
.map_err(|err| EvalAltResult::ErrorParsing(Box::new(err)))?;
let mut ast = self.compile_with_scope_and_optimization_level(
&Scope::new(),
script,
OptimizationLevel::None,
)?;
// If new functions are defined within the eval string, it is an error
if ast.1.len() > 0 {

View File

@@ -21,18 +21,18 @@ pub trait Func<ARGS, RET> {
/// # Examples
///
/// ```
/// # fn main() -> Result<(), rhai::EvalAltResult> {
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// use rhai::{Engine, Func}; // use 'Func' for 'create_from_ast'
///
/// let engine = Engine::new(); // create a new 'Engine' just for this
///
/// let ast = engine.compile("fn calc(x, y) { x + y.len() < 42 }")?;
/// let ast = engine.compile("fn calc(x, y) { x + len(y) < 42 }")?;
///
/// // Func takes two type parameters:
/// // 1) a tuple made up of the types of the script function's parameters
/// // 2) the return type of the script function
/// //
/// // 'func' will have type Box<dyn Fn(i64, String) -> Result<bool, EvalAltResult>> and is callable!
/// // 'func' will have type Box<dyn Fn(i64, String) -> Result<bool, Box<EvalAltResult>>> and is callable!
/// let func = Func::<(i64, String), bool>::create_from_ast(
/// // ^^^^^^^^^^^^^ function parameter types in tuple
///
@@ -52,18 +52,18 @@ pub trait Func<ARGS, RET> {
/// # Examples
///
/// ```
/// # fn main() -> Result<(), rhai::EvalAltResult> {
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// use rhai::{Engine, Func}; // use 'Func' for 'create_from_script'
///
/// let engine = Engine::new(); // create a new 'Engine' just for this
///
/// let script = "fn calc(x, y) { x + y.len() < 42 }";
/// let script = "fn calc(x, y) { x + len(y) < 42 }";
///
/// // Func takes two type parameters:
/// // 1) a tuple made up of the types of the script function's parameters
/// // 2) the return type of the script function
/// //
/// // 'func' will have type Box<dyn Fn(i64, String) -> Result<bool, EvalAltResult>> and is callable!
/// // 'func' will have type Box<dyn Fn(i64, String) -> Result<bool, Box<EvalAltResult>>> and is callable!
/// let func = Func::<(i64, String), bool>::create_from_script(
/// // ^^^^^^^^^^^^^ function parameter types in tuple
///
@@ -80,7 +80,7 @@ pub trait Func<ARGS, RET> {
self,
script: &str,
entry_point: &str,
) -> Result<Self::Output, ParseError>;
) -> Result<Self::Output, Box<ParseError>>;
}
macro_rules! def_anonymous_fn {
@@ -91,10 +91,10 @@ macro_rules! def_anonymous_fn {
impl<$($par: Variant + Clone,)* RET: Variant + Clone> Func<($($par,)*), RET> for Engine
{
#[cfg(feature = "sync")]
type Output = Box<dyn Fn($($par),*) -> Result<RET, EvalAltResult> + Send + Sync>;
type Output = Box<dyn Fn($($par),*) -> Result<RET, Box<EvalAltResult>> + Send + Sync>;
#[cfg(not(feature = "sync"))]
type Output = Box<dyn Fn($($par),*) -> Result<RET, EvalAltResult>>;
type Output = Box<dyn Fn($($par),*) -> Result<RET, Box<EvalAltResult>>>;
fn create_from_ast(self, ast: AST, entry_point: &str) -> Self::Output {
let name = entry_point.to_string();
@@ -104,7 +104,7 @@ macro_rules! def_anonymous_fn {
})
}
fn create_from_script(self, script: &str, entry_point: &str) -> Result<Self::Output, ParseError> {
fn create_from_script(self, script: &str, entry_point: &str) -> Result<Self::Output, Box<ParseError>> {
let ast = self.compile(script)?;
Ok(Func::<($($par,)*), RET>::create_from_ast(self, ast, entry_point))
}

View File

@@ -16,7 +16,7 @@ pub trait RegisterFn<FN, ARGS, RET> {
/// # Example
///
/// ```
/// # fn main() -> Result<(), rhai::EvalAltResult> {
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// use rhai::{Engine, RegisterFn};
///
/// // Normal function
@@ -48,7 +48,7 @@ pub trait RegisterDynamicFn<FN, ARGS> {
/// # Example
///
/// ```
/// # fn main() -> Result<(), rhai::EvalAltResult> {
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// use rhai::{Engine, Dynamic, RegisterDynamicFn};
///
/// // Function that returns a Dynamic value
@@ -68,7 +68,7 @@ pub trait RegisterDynamicFn<FN, ARGS> {
fn register_dynamic_fn(&mut self, name: &str, f: FN);
}
/// A trait to register fallible custom functions returning Result<_, EvalAltResult> with the `Engine`.
/// A trait to register fallible custom functions returning `Result<_, Box<EvalAltResult>>` with the `Engine`.
pub trait RegisterResultFn<FN, ARGS, RET> {
/// Register a custom fallible function with the `Engine`.
///
@@ -78,9 +78,9 @@ pub trait RegisterResultFn<FN, ARGS, RET> {
/// use rhai::{Engine, RegisterResultFn, EvalAltResult};
///
/// // Normal function
/// fn div(x: i64, y: i64) -> Result<i64, EvalAltResult> {
/// fn div(x: i64, y: i64) -> Result<i64, Box<EvalAltResult>> {
/// if y == 0 {
/// // '.into()' automatically converts to 'EvalAltResult::ErrorRuntime'
/// // '.into()' automatically converts to 'Box<EvalAltResult::ErrorRuntime>'
/// Err("division by zero!".into())
/// } else {
/// Ok(x / y)
@@ -180,10 +180,10 @@ pub fn map_identity(data: Dynamic, _pos: Position) -> Result<Dynamic, Box<EvalAl
Ok(data)
}
/// To Result<Dynamic, EvalAltResult> mapping function.
/// To `Result<Dynamic, Box<EvalAltResult>>` mapping function.
#[inline]
pub fn map_result<T: Variant + Clone>(
data: Result<T, EvalAltResult>,
data: Result<T, Box<EvalAltResult>>,
pos: Position,
) -> Result<Dynamic, Box<EvalAltResult>> {
data.map(|v| v.into_dynamic())
@@ -241,9 +241,9 @@ macro_rules! def_register {
$($par: Variant + Clone,)*
#[cfg(feature = "sync")]
FN: Fn($($param),*) -> Result<RET, EvalAltResult> + Send + Sync + 'static,
FN: Fn($($param),*) -> Result<RET, Box<EvalAltResult>> + Send + Sync + 'static,
#[cfg(not(feature = "sync"))]
FN: Fn($($param),*) -> Result<RET, EvalAltResult> + 'static,
FN: Fn($($param),*) -> Result<RET, Box<EvalAltResult>> + 'static,
RET: Variant + Clone
> RegisterResultFn<FN, ($($mark,)*), RET> for Engine

View File

@@ -23,7 +23,7 @@
//! ```,no_run
//! use rhai::{Engine, EvalAltResult, RegisterFn};
//!
//! fn main() -> Result<(), EvalAltResult>
//! fn main() -> Result<(), Box<EvalAltResult>>
//! {
//! // Define external function
//! fn compute_something(x: i64) -> bool {
@@ -71,7 +71,7 @@ extern crate alloc;
mod any;
mod api;
mod builtin;
//mod builtin;
mod engine;
mod error;
mod fn_call;

View File

@@ -22,67 +22,73 @@ use crate::stdlib::{
};
// Checked add
fn add<T: Display + CheckedAdd>(x: T, y: T) -> Result<T, EvalAltResult> {
fn add<T: Display + CheckedAdd>(x: T, y: T) -> Result<T, Box<EvalAltResult>> {
x.checked_add(&y).ok_or_else(|| {
EvalAltResult::ErrorArithmetic(
Box::new(EvalAltResult::ErrorArithmetic(
format!("Addition overflow: {} + {}", x, y),
Position::none(),
)
))
})
}
// Checked subtract
fn sub<T: Display + CheckedSub>(x: T, y: T) -> Result<T, EvalAltResult> {
fn sub<T: Display + CheckedSub>(x: T, y: T) -> Result<T, Box<EvalAltResult>> {
x.checked_sub(&y).ok_or_else(|| {
EvalAltResult::ErrorArithmetic(
Box::new(EvalAltResult::ErrorArithmetic(
format!("Subtraction underflow: {} - {}", x, y),
Position::none(),
)
))
})
}
// Checked multiply
fn mul<T: Display + CheckedMul>(x: T, y: T) -> Result<T, EvalAltResult> {
fn mul<T: Display + CheckedMul>(x: T, y: T) -> Result<T, Box<EvalAltResult>> {
x.checked_mul(&y).ok_or_else(|| {
EvalAltResult::ErrorArithmetic(
Box::new(EvalAltResult::ErrorArithmetic(
format!("Multiplication overflow: {} * {}", x, y),
Position::none(),
)
))
})
}
// Checked divide
fn div<T>(x: T, y: T) -> Result<T, EvalAltResult>
fn div<T>(x: T, y: T) -> Result<T, Box<EvalAltResult>>
where
T: Display + CheckedDiv + PartialEq + Zero,
{
// Detect division by zero
if y == T::zero() {
return Err(EvalAltResult::ErrorArithmetic(
return Err(Box::new(EvalAltResult::ErrorArithmetic(
format!("Division by zero: {} / {}", x, y),
Position::none(),
));
)));
}
x.checked_div(&y).ok_or_else(|| {
EvalAltResult::ErrorArithmetic(
Box::new(EvalAltResult::ErrorArithmetic(
format!("Division overflow: {} / {}", x, y),
Position::none(),
)
))
})
}
// Checked negative - e.g. -(i32::MIN) will overflow i32::MAX
fn neg<T: Display + CheckedNeg>(x: T) -> Result<T, EvalAltResult> {
fn neg<T: Display + CheckedNeg>(x: T) -> Result<T, Box<EvalAltResult>> {
x.checked_neg().ok_or_else(|| {
EvalAltResult::ErrorArithmetic(format!("Negation overflow: -{}", x), Position::none())
Box::new(EvalAltResult::ErrorArithmetic(
format!("Negation overflow: -{}", x),
Position::none(),
))
})
}
// Checked absolute
fn abs<T: Display + CheckedNeg + PartialOrd + Zero>(x: T) -> Result<T, EvalAltResult> {
fn abs<T: Display + CheckedNeg + PartialOrd + Zero>(x: T) -> Result<T, Box<EvalAltResult>> {
// FIX - We don't use Signed::abs() here because, contrary to documentation, it panics
// when the number is ::MIN instead of returning ::MIN itself.
if x >= <T as Zero>::zero() {
Ok(x)
} else {
x.checked_neg().ok_or_else(|| {
EvalAltResult::ErrorArithmetic(format!("Negation overflow: -{}", x), Position::none())
Box::new(EvalAltResult::ErrorArithmetic(
format!("Negation overflow: -{}", x),
Position::none(),
))
})
}
}
@@ -129,37 +135,37 @@ fn binary_xor<T: BitXor>(x: T, y: T) -> <T as BitXor>::Output {
x ^ y
}
// Checked left-shift
fn shl<T: Display + CheckedShl>(x: T, y: INT) -> Result<T, EvalAltResult> {
fn shl<T: Display + CheckedShl>(x: T, y: INT) -> Result<T, Box<EvalAltResult>> {
// Cannot shift by a negative number of bits
if y < 0 {
return Err(EvalAltResult::ErrorArithmetic(
return Err(Box::new(EvalAltResult::ErrorArithmetic(
format!("Left-shift by a negative number: {} << {}", x, y),
Position::none(),
));
)));
}
CheckedShl::checked_shl(&x, y as u32).ok_or_else(|| {
EvalAltResult::ErrorArithmetic(
Box::new(EvalAltResult::ErrorArithmetic(
format!("Left-shift by too many bits: {} << {}", x, y),
Position::none(),
)
))
})
}
// Checked right-shift
fn shr<T: Display + CheckedShr>(x: T, y: INT) -> Result<T, EvalAltResult> {
fn shr<T: Display + CheckedShr>(x: T, y: INT) -> Result<T, Box<EvalAltResult>> {
// Cannot shift by a negative number of bits
if y < 0 {
return Err(EvalAltResult::ErrorArithmetic(
return Err(Box::new(EvalAltResult::ErrorArithmetic(
format!("Right-shift by a negative number: {} >> {}", x, y),
Position::none(),
));
)));
}
CheckedShr::checked_shr(&x, y as u32).ok_or_else(|| {
EvalAltResult::ErrorArithmetic(
Box::new(EvalAltResult::ErrorArithmetic(
format!("Right-shift by too many bits: {} % {}", x, y),
Position::none(),
)
))
})
}
// Unchecked left-shift - may panic if shifting by a negative number of bits
@@ -171,12 +177,12 @@ fn shr_u<T: Shr<T>>(x: T, y: T) -> <T as Shr<T>>::Output {
x.shr(y)
}
// Checked modulo
fn modulo<T: Display + CheckedRem>(x: T, y: T) -> Result<T, EvalAltResult> {
fn modulo<T: Display + CheckedRem>(x: T, y: T) -> Result<T, Box<EvalAltResult>> {
x.checked_rem(&y).ok_or_else(|| {
EvalAltResult::ErrorArithmetic(
Box::new(EvalAltResult::ErrorArithmetic(
format!("Modulo division by zero or overflow: {} % {}", x, y),
Position::none(),
)
))
})
}
// Unchecked modulo - may panic if dividing by zero
@@ -184,25 +190,25 @@ fn modulo_u<T: Rem>(x: T, y: T) -> <T as Rem>::Output {
x % y
}
// Checked power
fn pow_i_i(x: INT, y: INT) -> Result<INT, EvalAltResult> {
fn pow_i_i(x: INT, y: INT) -> Result<INT, Box<EvalAltResult>> {
#[cfg(not(feature = "only_i32"))]
{
if y > (u32::MAX as INT) {
Err(EvalAltResult::ErrorArithmetic(
Err(Box::new(EvalAltResult::ErrorArithmetic(
format!("Integer raised to too large an index: {} ~ {}", x, y),
Position::none(),
))
)))
} else if y < 0 {
Err(EvalAltResult::ErrorArithmetic(
Err(Box::new(EvalAltResult::ErrorArithmetic(
format!("Integer raised to a negative index: {} ~ {}", x, y),
Position::none(),
))
)))
} else {
x.checked_pow(y as u32).ok_or_else(|| {
EvalAltResult::ErrorArithmetic(
Box::new(EvalAltResult::ErrorArithmetic(
format!("Power overflow: {} ~ {}", x, y),
Position::none(),
)
))
})
}
}
@@ -210,16 +216,16 @@ fn pow_i_i(x: INT, y: INT) -> Result<INT, EvalAltResult> {
#[cfg(feature = "only_i32")]
{
if y < 0 {
Err(EvalAltResult::ErrorArithmetic(
Err(Box::new(EvalAltResult::ErrorArithmetic(
format!("Integer raised to a negative index: {} ~ {}", x, y),
Position::none(),
))
)))
} else {
x.checked_pow(y as u32).ok_or_else(|| {
EvalAltResult::ErrorArithmetic(
Box::new(EvalAltResult::ErrorArithmetic(
format!("Power overflow: {} ~ {}", x, y),
Position::none(),
)
))
})
}
}
@@ -235,13 +241,13 @@ fn pow_f_f(x: FLOAT, y: FLOAT) -> FLOAT {
}
// Checked power
#[cfg(not(feature = "no_float"))]
fn pow_f_i(x: FLOAT, y: INT) -> Result<FLOAT, EvalAltResult> {
fn pow_f_i(x: FLOAT, y: INT) -> Result<FLOAT, Box<EvalAltResult>> {
// Raise to power that is larger than an i32
if y > (i32::MAX as INT) {
return Err(EvalAltResult::ErrorArithmetic(
return Err(Box::new(EvalAltResult::ErrorArithmetic(
format!("Number raised to too large an index: {} ~ {}", x, y),
Position::none(),
));
)));
}
Ok(x.powi(y as i32))

View File

@@ -1,7 +1,7 @@
use super::{reg_binary, reg_binary_mut, reg_trinary_mut, reg_unary_mut};
use crate::def_package;
use crate::any::{Dynamic, Variant};
use crate::def_package;
use crate::engine::Array;
use crate::fn_register::{map_dynamic as map, map_identity as pass};
use crate::parser::INT;

View File

@@ -1,7 +1,7 @@
use super::{reg_binary, reg_trinary, reg_unary_mut, PackageStore};
use crate::def_package;
use crate::any::{Dynamic, Union, Variant};
use crate::def_package;
use crate::engine::{Array, Map};
use crate::fn_register::map_dynamic as map;
use crate::parser::INT;

View File

@@ -1,7 +1,7 @@
use super::{reg_binary, reg_binary_mut, reg_unary_mut};
use crate::def_package;
use crate::any::Dynamic;
use crate::def_package;
use crate::engine::Map;
use crate::fn_register::map_dynamic as map;
use crate::parser::INT;

View File

@@ -95,10 +95,10 @@ def_package!(BasicMathPackage:"Basic mathematic functions.", lib, {
"to_int",
|x: f32| {
if x > (MAX_INT as f32) {
return Err(EvalAltResult::ErrorArithmetic(
return Err(Box::new(EvalAltResult::ErrorArithmetic(
format!("Integer overflow: to_int({})", x),
Position::none(),
));
)));
}
Ok(x.trunc() as INT)
@@ -110,10 +110,10 @@ def_package!(BasicMathPackage:"Basic mathematic functions.", lib, {
"to_int",
|x: FLOAT| {
if x > (MAX_INT as FLOAT) {
return Err(EvalAltResult::ErrorArithmetic(
return Err(Box::new(EvalAltResult::ErrorArithmetic(
format!("Integer overflow: to_int({})", x),
Position::none(),
));
)));
}
Ok(x.trunc() as INT)

View File

@@ -18,9 +18,11 @@ mod time_basic;
mod utils;
pub use arithmetic::ArithmeticPackage;
#[cfg(not(feature = "no_index"))]
pub use array_basic::BasicArrayPackage;
pub use iter_basic::BasicIteratorPackage;
pub use logic::LogicPackage;
#[cfg(not(feature = "no_object"))]
pub use map_basic::BasicMapPackage;
pub use math_basic::BasicMathPackage;
pub use pkg_core::CorePackage;

View File

@@ -1,4 +1,6 @@
#[cfg(not(feature = "no_index"))]
use super::array_basic::BasicArrayPackage;
#[cfg(not(feature = "no_object"))]
use super::map_basic::BasicMapPackage;
use super::math_basic::BasicMathPackage;
use super::pkg_core::CorePackage;

View File

@@ -31,13 +31,13 @@ def_package!(BasicTimePackage:"Basic timing utilities.", lib, {
#[cfg(not(feature = "unchecked"))]
{
if seconds > (MAX_INT as u64) {
return Err(EvalAltResult::ErrorArithmetic(
return Err(Box::new(EvalAltResult::ErrorArithmetic(
format!(
"Integer overflow for timestamp duration: {}",
-(seconds as i64)
),
Position::none(),
));
)));
}
}
return Ok(-(seconds as INT));
@@ -53,10 +53,10 @@ def_package!(BasicTimePackage:"Basic timing utilities.", lib, {
#[cfg(not(feature = "unchecked"))]
{
if seconds > (MAX_INT as u64) {
return Err(EvalAltResult::ErrorArithmetic(
return Err(Box::new(EvalAltResult::ErrorArithmetic(
format!("Integer overflow for timestamp duration: {}", seconds),
Position::none(),
));
)));
}
}
return Ok(seconds as INT);

View File

@@ -72,7 +72,7 @@ impl AST {
/// # Example
///
/// ```
/// # fn main() -> Result<(), rhai::EvalAltResult> {
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// # #[cfg(not(feature = "no_function"))]
/// # {
/// use rhai::Engine;

View File

@@ -228,20 +228,23 @@ impl fmt::Display for EvalAltResult {
}
}
impl From<ParseError> for EvalAltResult {
impl From<ParseError> for Box<EvalAltResult> {
fn from(err: ParseError) -> Self {
Self::ErrorParsing(Box::new(err))
Box::new(EvalAltResult::ErrorParsing(Box::new(err)))
}
}
impl From<Box<ParseError>> for EvalAltResult {
impl From<Box<ParseError>> for Box<EvalAltResult> {
fn from(err: Box<ParseError>) -> Self {
Self::ErrorParsing(err)
Box::new(EvalAltResult::ErrorParsing(err))
}
}
impl<T: AsRef<str>> From<T> for EvalAltResult {
impl<T: AsRef<str>> From<T> for Box<EvalAltResult> {
fn from(err: T) -> Self {
Self::ErrorRuntime(err.as_ref().to_string(), Position::none())
Box::new(EvalAltResult::ErrorRuntime(
err.as_ref().to_string(),
Position::none(),
))
}
}

View File

@@ -42,7 +42,7 @@ pub(crate) struct EntryRef<'a> {
/// # Example
///
/// ```
/// # fn main() -> Result<(), rhai::EvalAltResult> {
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// use rhai::{Engine, Scope};
///
/// let engine = Engine::new();