Use type alias
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use crate::plugin::*;
|
||||
use crate::{def_package, EvalAltResult, Position, INT};
|
||||
use crate::{def_package, EvalAltResult, Position, RhaiError, RhaiResultOf, INT};
|
||||
#[cfg(feature = "no_std")]
|
||||
use std::prelude::v1::*;
|
||||
|
||||
@@ -10,7 +10,7 @@ use std::prelude::v1::*;
|
||||
use num_traits::Float;
|
||||
|
||||
#[inline(never)]
|
||||
pub fn make_err(msg: impl Into<String>) -> Box<EvalAltResult> {
|
||||
pub fn make_err(msg: impl Into<String>) -> RhaiError {
|
||||
EvalAltResult::ErrorArithmetic(msg.into(), Position::NONE).into()
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ macro_rules! gen_arithmetic_functions {
|
||||
#[export_module]
|
||||
pub mod functions {
|
||||
#[rhai_fn(name = "+", return_raw)]
|
||||
pub fn add(x: $arg_type, y: $arg_type) -> Result<$arg_type, Box<EvalAltResult>> {
|
||||
pub fn add(x: $arg_type, y: $arg_type) -> RhaiResultOf<$arg_type> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
x.checked_add(y).ok_or_else(|| make_err(format!("Addition overflow: {} + {}", x, y)))
|
||||
} else {
|
||||
@@ -30,7 +30,7 @@ macro_rules! gen_arithmetic_functions {
|
||||
}
|
||||
}
|
||||
#[rhai_fn(name = "-", return_raw)]
|
||||
pub fn subtract(x: $arg_type, y: $arg_type) -> Result<$arg_type, Box<EvalAltResult>> {
|
||||
pub fn subtract(x: $arg_type, y: $arg_type) -> RhaiResultOf<$arg_type> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
x.checked_sub(y).ok_or_else(|| make_err(format!("Subtraction overflow: {} - {}", x, y)))
|
||||
} else {
|
||||
@@ -38,7 +38,7 @@ macro_rules! gen_arithmetic_functions {
|
||||
}
|
||||
}
|
||||
#[rhai_fn(name = "*", return_raw)]
|
||||
pub fn multiply(x: $arg_type, y: $arg_type) -> Result<$arg_type, Box<EvalAltResult>> {
|
||||
pub fn multiply(x: $arg_type, y: $arg_type) -> RhaiResultOf<$arg_type> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
x.checked_mul(y).ok_or_else(|| make_err(format!("Multiplication overflow: {} * {}", x, y)))
|
||||
} else {
|
||||
@@ -46,7 +46,7 @@ macro_rules! gen_arithmetic_functions {
|
||||
}
|
||||
}
|
||||
#[rhai_fn(name = "/", return_raw)]
|
||||
pub fn divide(x: $arg_type, y: $arg_type) -> Result<$arg_type, Box<EvalAltResult>> {
|
||||
pub fn divide(x: $arg_type, y: $arg_type) -> RhaiResultOf<$arg_type> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
// Detect division by zero
|
||||
if y == 0 {
|
||||
@@ -59,7 +59,7 @@ macro_rules! gen_arithmetic_functions {
|
||||
}
|
||||
}
|
||||
#[rhai_fn(name = "%", return_raw)]
|
||||
pub fn modulo(x: $arg_type, y: $arg_type) -> Result<$arg_type, Box<EvalAltResult>> {
|
||||
pub fn modulo(x: $arg_type, y: $arg_type) -> RhaiResultOf<$arg_type> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
x.checked_rem(y).ok_or_else(|| make_err(format!("Modulo division by zero or overflow: {} % {}", x, y)))
|
||||
} else {
|
||||
@@ -67,7 +67,7 @@ macro_rules! gen_arithmetic_functions {
|
||||
}
|
||||
}
|
||||
#[rhai_fn(name = "**", return_raw)]
|
||||
pub fn power(x: $arg_type, y: INT) -> Result<$arg_type, Box<EvalAltResult>> {
|
||||
pub fn power(x: $arg_type, y: INT) -> RhaiResultOf<$arg_type> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
if cfg!(not(feature = "only_i32")) && y > (u32::MAX as INT) {
|
||||
Err(make_err(format!("Integer raised to too large an index: {} ~ {}", x, y)))
|
||||
@@ -82,7 +82,7 @@ macro_rules! gen_arithmetic_functions {
|
||||
}
|
||||
|
||||
#[rhai_fn(name = "<<", return_raw)]
|
||||
pub fn shift_left(x: $arg_type, y: INT) -> Result<$arg_type, Box<EvalAltResult>> {
|
||||
pub fn shift_left(x: $arg_type, y: INT) -> RhaiResultOf<$arg_type> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
if cfg!(not(feature = "only_i32")) && y > (u32::MAX as INT) {
|
||||
Err(make_err(format!("Left-shift by too many bits: {} << {}", x, y)))
|
||||
@@ -96,7 +96,7 @@ macro_rules! gen_arithmetic_functions {
|
||||
}
|
||||
}
|
||||
#[rhai_fn(name = ">>", return_raw)]
|
||||
pub fn shift_right(x: $arg_type, y: INT) -> Result<$arg_type, Box<EvalAltResult>> {
|
||||
pub fn shift_right(x: $arg_type, y: INT) -> RhaiResultOf<$arg_type> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
if cfg!(not(feature = "only_i32")) && y > (u32::MAX as INT) {
|
||||
Err(make_err(format!("Right-shift by too many bits: {} >> {}", x, y)))
|
||||
@@ -146,7 +146,7 @@ macro_rules! gen_signed_functions {
|
||||
#[export_module]
|
||||
pub mod functions {
|
||||
#[rhai_fn(name = "-", return_raw)]
|
||||
pub fn neg(x: $arg_type) -> Result<$arg_type, Box<EvalAltResult>> {
|
||||
pub fn neg(x: $arg_type) -> RhaiResultOf<$arg_type> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
x.checked_neg().ok_or_else(|| make_err(format!("Negation overflow: -{}", x)))
|
||||
} else {
|
||||
@@ -158,7 +158,7 @@ macro_rules! gen_signed_functions {
|
||||
x
|
||||
}
|
||||
#[rhai_fn(return_raw)]
|
||||
pub fn abs(x: $arg_type) -> Result<$arg_type, Box<EvalAltResult>> {
|
||||
pub fn abs(x: $arg_type) -> RhaiResultOf<$arg_type> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
x.checked_abs().ok_or_else(|| make_err(format!("Negation overflow: -{}", x)))
|
||||
} else {
|
||||
@@ -254,8 +254,6 @@ gen_signed_functions!(signed_num_128 => i128);
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
#[export_module]
|
||||
mod f32_functions {
|
||||
use crate::EvalAltResult;
|
||||
|
||||
#[cfg(not(feature = "f32_float"))]
|
||||
pub mod basic_arithmetic {
|
||||
#[rhai_fn(name = "+")]
|
||||
@@ -337,7 +335,7 @@ mod f32_functions {
|
||||
x.abs()
|
||||
}
|
||||
#[rhai_fn(return_raw)]
|
||||
pub fn sign(x: f32) -> Result<INT, Box<EvalAltResult>> {
|
||||
pub fn sign(x: f32) -> RhaiResultOf<INT> {
|
||||
match x.signum() {
|
||||
_ if x == 0.0 => Ok(0),
|
||||
x if x.is_nan() => Err(make_err("Sign of NaN is undefined")),
|
||||
@@ -349,7 +347,7 @@ mod f32_functions {
|
||||
x == 0.0
|
||||
}
|
||||
#[rhai_fn(name = "**", return_raw)]
|
||||
pub fn pow_f_i(x: f32, y: INT) -> Result<f32, Box<EvalAltResult>> {
|
||||
pub fn pow_f_i(x: f32, y: INT) -> RhaiResultOf<f32> {
|
||||
if cfg!(not(feature = "unchecked")) && y > (i32::MAX as INT) {
|
||||
Err(make_err(format!(
|
||||
"Number raised to too large an index: {} ~ {}",
|
||||
@@ -445,7 +443,7 @@ mod f64_functions {
|
||||
x.abs()
|
||||
}
|
||||
#[rhai_fn(return_raw)]
|
||||
pub fn sign(x: f64) -> Result<INT, Box<EvalAltResult>> {
|
||||
pub fn sign(x: f64) -> RhaiResultOf<INT> {
|
||||
match x.signum() {
|
||||
_ if x == 0.0 => Ok(0),
|
||||
x if x.is_nan() => Err(make_err("Sign of NaN is undefined")),
|
||||
@@ -465,7 +463,7 @@ pub mod decimal_functions {
|
||||
use rust_decimal::{prelude::Zero, Decimal, MathematicalOps};
|
||||
|
||||
#[rhai_fn(skip, return_raw)]
|
||||
pub fn add(x: Decimal, y: Decimal) -> Result<Decimal, Box<EvalAltResult>> {
|
||||
pub fn add(x: Decimal, y: Decimal) -> RhaiResultOf<Decimal> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
x.checked_add(y)
|
||||
.ok_or_else(|| make_err(format!("Addition overflow: {} + {}", x, y)))
|
||||
@@ -474,7 +472,7 @@ pub mod decimal_functions {
|
||||
}
|
||||
}
|
||||
#[rhai_fn(skip, return_raw)]
|
||||
pub fn subtract(x: Decimal, y: Decimal) -> Result<Decimal, Box<EvalAltResult>> {
|
||||
pub fn subtract(x: Decimal, y: Decimal) -> RhaiResultOf<Decimal> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
x.checked_sub(y)
|
||||
.ok_or_else(|| make_err(format!("Subtraction overflow: {} - {}", x, y)))
|
||||
@@ -483,7 +481,7 @@ pub mod decimal_functions {
|
||||
}
|
||||
}
|
||||
#[rhai_fn(skip, return_raw)]
|
||||
pub fn multiply(x: Decimal, y: Decimal) -> Result<Decimal, Box<EvalAltResult>> {
|
||||
pub fn multiply(x: Decimal, y: Decimal) -> RhaiResultOf<Decimal> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
x.checked_mul(y)
|
||||
.ok_or_else(|| make_err(format!("Multiplication overflow: {} * {}", x, y)))
|
||||
@@ -492,7 +490,7 @@ pub mod decimal_functions {
|
||||
}
|
||||
}
|
||||
#[rhai_fn(skip, return_raw)]
|
||||
pub fn divide(x: Decimal, y: Decimal) -> Result<Decimal, Box<EvalAltResult>> {
|
||||
pub fn divide(x: Decimal, y: Decimal) -> RhaiResultOf<Decimal> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
// Detect division by zero
|
||||
if y == Decimal::zero() {
|
||||
@@ -506,7 +504,7 @@ pub mod decimal_functions {
|
||||
}
|
||||
}
|
||||
#[rhai_fn(skip, return_raw)]
|
||||
pub fn modulo(x: Decimal, y: Decimal) -> Result<Decimal, Box<EvalAltResult>> {
|
||||
pub fn modulo(x: Decimal, y: Decimal) -> RhaiResultOf<Decimal> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
x.checked_rem(y).ok_or_else(|| {
|
||||
make_err(format!(
|
||||
@@ -519,7 +517,7 @@ pub mod decimal_functions {
|
||||
}
|
||||
}
|
||||
#[rhai_fn(skip, return_raw)]
|
||||
pub fn power(x: Decimal, y: Decimal) -> Result<Decimal, Box<EvalAltResult>> {
|
||||
pub fn power(x: Decimal, y: Decimal) -> RhaiResultOf<Decimal> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
x.checked_powd(y)
|
||||
.ok_or_else(|| make_err(format!("Exponential overflow: {} + {}", x, y)))
|
||||
|
@@ -5,7 +5,7 @@ use crate::engine::OP_EQUALS;
|
||||
use crate::plugin::*;
|
||||
use crate::{
|
||||
def_package, Array, Dynamic, EvalAltResult, ExclusiveRange, FnPtr, InclusiveRange,
|
||||
NativeCallContext, Position, INT,
|
||||
NativeCallContext, Position, RhaiResultOf, INT,
|
||||
};
|
||||
#[cfg(feature = "no_std")]
|
||||
use std::prelude::v1::*;
|
||||
@@ -80,7 +80,7 @@ pub mod array_functions {
|
||||
array: &mut Array,
|
||||
len: INT,
|
||||
item: Dynamic,
|
||||
) -> Result<(), Box<EvalAltResult>> {
|
||||
) -> RhaiResultOf<()> {
|
||||
if len <= 0 {
|
||||
return Ok(());
|
||||
}
|
||||
@@ -263,11 +263,7 @@ pub mod array_functions {
|
||||
}
|
||||
}
|
||||
#[rhai_fn(return_raw, pure)]
|
||||
pub fn map(
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
mapper: FnPtr,
|
||||
) -> Result<Array, Box<EvalAltResult>> {
|
||||
pub fn map(ctx: NativeCallContext, array: &mut Array, mapper: FnPtr) -> RhaiResultOf<Array> {
|
||||
if array.is_empty() {
|
||||
return Ok(array.clone());
|
||||
}
|
||||
@@ -304,7 +300,7 @@ pub mod array_functions {
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
mapper: &str,
|
||||
) -> Result<Array, Box<EvalAltResult>> {
|
||||
) -> RhaiResultOf<Array> {
|
||||
map(ctx, array, FnPtr::new(mapper)?)
|
||||
}
|
||||
|
||||
@@ -313,7 +309,7 @@ pub mod array_functions {
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
filter: FnPtr,
|
||||
) -> Result<Array, Box<EvalAltResult>> {
|
||||
) -> RhaiResultOf<Array> {
|
||||
if array.is_empty() {
|
||||
return Ok(array.clone());
|
||||
}
|
||||
@@ -353,7 +349,7 @@ pub mod array_functions {
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
filter_func: &str,
|
||||
) -> Result<Array, Box<EvalAltResult>> {
|
||||
) -> RhaiResultOf<Array> {
|
||||
filter(ctx, array, FnPtr::new(filter_func)?)
|
||||
}
|
||||
#[rhai_fn(return_raw, pure)]
|
||||
@@ -361,7 +357,7 @@ pub mod array_functions {
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
value: Dynamic,
|
||||
) -> Result<bool, Box<EvalAltResult>> {
|
||||
) -> RhaiResultOf<bool> {
|
||||
if array.is_empty() {
|
||||
return Ok(false);
|
||||
}
|
||||
@@ -396,7 +392,7 @@ pub mod array_functions {
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
value: Dynamic,
|
||||
) -> Result<INT, Box<EvalAltResult>> {
|
||||
) -> RhaiResultOf<INT> {
|
||||
if array.is_empty() {
|
||||
Ok(-1)
|
||||
} else {
|
||||
@@ -409,7 +405,7 @@ pub mod array_functions {
|
||||
array: &mut Array,
|
||||
value: Dynamic,
|
||||
start: INT,
|
||||
) -> Result<INT, Box<EvalAltResult>> {
|
||||
) -> RhaiResultOf<INT> {
|
||||
if array.is_empty() {
|
||||
return Ok(-1);
|
||||
}
|
||||
@@ -455,7 +451,7 @@ pub mod array_functions {
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
filter: &str,
|
||||
) -> Result<INT, Box<EvalAltResult>> {
|
||||
) -> RhaiResultOf<INT> {
|
||||
index_of_filter(ctx, array, FnPtr::new(filter)?)
|
||||
}
|
||||
#[rhai_fn(name = "index_of", return_raw, pure)]
|
||||
@@ -463,7 +459,7 @@ pub mod array_functions {
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
filter: FnPtr,
|
||||
) -> Result<INT, Box<EvalAltResult>> {
|
||||
) -> RhaiResultOf<INT> {
|
||||
if array.is_empty() {
|
||||
Ok(-1)
|
||||
} else {
|
||||
@@ -476,7 +472,7 @@ pub mod array_functions {
|
||||
array: &mut Array,
|
||||
filter: FnPtr,
|
||||
start: INT,
|
||||
) -> Result<INT, Box<EvalAltResult>> {
|
||||
) -> RhaiResultOf<INT> {
|
||||
if array.is_empty() {
|
||||
return Ok(-1);
|
||||
}
|
||||
@@ -526,15 +522,11 @@ pub mod array_functions {
|
||||
array: &mut Array,
|
||||
filter: &str,
|
||||
start: INT,
|
||||
) -> Result<INT, Box<EvalAltResult>> {
|
||||
) -> RhaiResultOf<INT> {
|
||||
index_of_filter_starting_from(ctx, array, FnPtr::new(filter)?, start)
|
||||
}
|
||||
#[rhai_fn(return_raw, pure)]
|
||||
pub fn some(
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
filter: FnPtr,
|
||||
) -> Result<bool, Box<EvalAltResult>> {
|
||||
pub fn some(ctx: NativeCallContext, array: &mut Array, filter: FnPtr) -> RhaiResultOf<bool> {
|
||||
if array.is_empty() {
|
||||
return Ok(false);
|
||||
}
|
||||
@@ -572,15 +564,11 @@ pub mod array_functions {
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
filter: &str,
|
||||
) -> Result<bool, Box<EvalAltResult>> {
|
||||
) -> RhaiResultOf<bool> {
|
||||
some(ctx, array, FnPtr::new(filter)?)
|
||||
}
|
||||
#[rhai_fn(return_raw, pure)]
|
||||
pub fn all(
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
filter: FnPtr,
|
||||
) -> Result<bool, Box<EvalAltResult>> {
|
||||
pub fn all(ctx: NativeCallContext, array: &mut Array, filter: FnPtr) -> RhaiResultOf<bool> {
|
||||
if array.is_empty() {
|
||||
return Ok(true);
|
||||
}
|
||||
@@ -618,11 +606,11 @@ pub mod array_functions {
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
filter: &str,
|
||||
) -> Result<bool, Box<EvalAltResult>> {
|
||||
) -> RhaiResultOf<bool> {
|
||||
all(ctx, array, FnPtr::new(filter)?)
|
||||
}
|
||||
#[rhai_fn(return_raw)]
|
||||
pub fn dedup(ctx: NativeCallContext, array: &mut Array) -> Result<(), Box<EvalAltResult>> {
|
||||
pub fn dedup(ctx: NativeCallContext, array: &mut Array) -> RhaiResultOf<()> {
|
||||
dedup_with_fn_name(ctx, array, OP_EQUALS)
|
||||
}
|
||||
#[rhai_fn(name = "dedup", return_raw)]
|
||||
@@ -630,7 +618,7 @@ pub mod array_functions {
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
comparer: FnPtr,
|
||||
) -> Result<(), Box<EvalAltResult>> {
|
||||
) -> RhaiResultOf<()> {
|
||||
if array.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
@@ -650,15 +638,11 @@ pub mod array_functions {
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
comparer: &str,
|
||||
) -> Result<(), Box<EvalAltResult>> {
|
||||
) -> RhaiResultOf<()> {
|
||||
dedup_by_comparer(ctx, array, FnPtr::new(comparer)?)
|
||||
}
|
||||
#[rhai_fn(return_raw, pure)]
|
||||
pub fn reduce(
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
reducer: FnPtr,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
pub fn reduce(ctx: NativeCallContext, array: &mut Array, reducer: FnPtr) -> RhaiResult {
|
||||
reduce_with_initial(ctx, array, reducer, Dynamic::UNIT)
|
||||
}
|
||||
#[rhai_fn(name = "reduce", return_raw, pure)]
|
||||
@@ -666,7 +650,7 @@ pub mod array_functions {
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
reducer: &str,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
) -> RhaiResult {
|
||||
reduce(ctx, array, FnPtr::new(reducer)?)
|
||||
}
|
||||
#[rhai_fn(name = "reduce", return_raw, pure)]
|
||||
@@ -675,7 +659,7 @@ pub mod array_functions {
|
||||
array: &mut Array,
|
||||
reducer: FnPtr,
|
||||
initial: Dynamic,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
) -> RhaiResult {
|
||||
if array.is_empty() {
|
||||
return Ok(initial);
|
||||
}
|
||||
@@ -713,15 +697,11 @@ pub mod array_functions {
|
||||
array: &mut Array,
|
||||
reducer: &str,
|
||||
initial: Dynamic,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
) -> RhaiResult {
|
||||
reduce_with_initial(ctx, array, FnPtr::new(reducer)?, initial)
|
||||
}
|
||||
#[rhai_fn(return_raw, pure)]
|
||||
pub fn reduce_rev(
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
reducer: FnPtr,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
pub fn reduce_rev(ctx: NativeCallContext, array: &mut Array, reducer: FnPtr) -> RhaiResult {
|
||||
reduce_rev_with_initial(ctx, array, reducer, Dynamic::UNIT)
|
||||
}
|
||||
#[rhai_fn(name = "reduce_rev", return_raw, pure)]
|
||||
@@ -729,7 +709,7 @@ pub mod array_functions {
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
reducer: &str,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
) -> RhaiResult {
|
||||
reduce_rev(ctx, array, FnPtr::new(reducer)?)
|
||||
}
|
||||
#[rhai_fn(name = "reduce_rev", return_raw, pure)]
|
||||
@@ -738,7 +718,7 @@ pub mod array_functions {
|
||||
array: &mut Array,
|
||||
reducer: FnPtr,
|
||||
initial: Dynamic,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
) -> RhaiResult {
|
||||
if array.is_empty() {
|
||||
return Ok(initial);
|
||||
}
|
||||
@@ -777,7 +757,7 @@ pub mod array_functions {
|
||||
array: &mut Array,
|
||||
reducer: &str,
|
||||
initial: Dynamic,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
) -> RhaiResult {
|
||||
reduce_rev_with_initial(ctx, array, FnPtr::new(reducer)?, initial)
|
||||
}
|
||||
#[rhai_fn(name = "sort", return_raw)]
|
||||
@@ -785,15 +765,11 @@ pub mod array_functions {
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
comparer: &str,
|
||||
) -> Result<(), Box<EvalAltResult>> {
|
||||
) -> RhaiResultOf<()> {
|
||||
sort(ctx, array, FnPtr::new(comparer)?)
|
||||
}
|
||||
#[rhai_fn(return_raw)]
|
||||
pub fn sort(
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
comparer: FnPtr,
|
||||
) -> Result<(), Box<EvalAltResult>> {
|
||||
pub fn sort(ctx: NativeCallContext, array: &mut Array, comparer: FnPtr) -> RhaiResultOf<()> {
|
||||
if array.len() <= 1 {
|
||||
return Ok(());
|
||||
}
|
||||
@@ -815,7 +791,7 @@ pub mod array_functions {
|
||||
Ok(())
|
||||
}
|
||||
#[rhai_fn(name = "sort", return_raw)]
|
||||
pub fn sort_with_builtin(array: &mut Array) -> Result<(), Box<EvalAltResult>> {
|
||||
pub fn sort_with_builtin(array: &mut Array) -> RhaiResultOf<()> {
|
||||
if array.len() <= 1 {
|
||||
return Ok(());
|
||||
}
|
||||
@@ -891,7 +867,7 @@ pub mod array_functions {
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
filter: FnPtr,
|
||||
) -> Result<Array, Box<EvalAltResult>> {
|
||||
) -> RhaiResultOf<Array> {
|
||||
if array.is_empty() {
|
||||
return Ok(Array::new());
|
||||
}
|
||||
@@ -938,7 +914,7 @@ pub mod array_functions {
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
filter: &str,
|
||||
) -> Result<Array, Box<EvalAltResult>> {
|
||||
) -> RhaiResultOf<Array> {
|
||||
drain(ctx, array, FnPtr::new(filter)?)
|
||||
}
|
||||
#[rhai_fn(name = "drain")]
|
||||
@@ -985,7 +961,7 @@ pub mod array_functions {
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
filter: FnPtr,
|
||||
) -> Result<Array, Box<EvalAltResult>> {
|
||||
) -> RhaiResultOf<Array> {
|
||||
if array.is_empty() {
|
||||
return Ok(Array::new());
|
||||
}
|
||||
@@ -1032,7 +1008,7 @@ pub mod array_functions {
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
filter: &str,
|
||||
) -> Result<Array, Box<EvalAltResult>> {
|
||||
) -> RhaiResultOf<Array> {
|
||||
retain(ctx, array, FnPtr::new(filter)?)
|
||||
}
|
||||
#[rhai_fn(name = "retain")]
|
||||
@@ -1082,7 +1058,7 @@ pub mod array_functions {
|
||||
ctx: NativeCallContext,
|
||||
array1: &mut Array,
|
||||
array2: Array,
|
||||
) -> Result<bool, Box<EvalAltResult>> {
|
||||
) -> RhaiResultOf<bool> {
|
||||
if array1.len() != array2.len() {
|
||||
return Ok(false);
|
||||
}
|
||||
@@ -1122,7 +1098,7 @@ pub mod array_functions {
|
||||
ctx: NativeCallContext,
|
||||
array1: &mut Array,
|
||||
array2: Array,
|
||||
) -> Result<bool, Box<EvalAltResult>> {
|
||||
) -> RhaiResultOf<bool> {
|
||||
equals(ctx, array1, array2).map(|r| !r)
|
||||
}
|
||||
}
|
||||
|
@@ -4,7 +4,7 @@
|
||||
use crate::plugin::*;
|
||||
use crate::{
|
||||
def_package, Blob, Dynamic, EvalAltResult, ExclusiveRange, InclusiveRange, NativeCallContext,
|
||||
Position, INT,
|
||||
Position, RhaiResultOf, INT,
|
||||
};
|
||||
#[cfg(feature = "no_std")]
|
||||
use std::prelude::v1::*;
|
||||
@@ -31,10 +31,7 @@ pub mod blob_functions {
|
||||
Blob::new()
|
||||
}
|
||||
#[rhai_fn(name = "blob", return_raw)]
|
||||
pub fn blob_with_capacity(
|
||||
ctx: NativeCallContext,
|
||||
len: INT,
|
||||
) -> Result<Blob, Box<EvalAltResult>> {
|
||||
pub fn blob_with_capacity(ctx: NativeCallContext, len: INT) -> RhaiResultOf<Blob> {
|
||||
blob_with_capacity_and_value(ctx, len, 0)
|
||||
}
|
||||
#[rhai_fn(name = "blob", return_raw)]
|
||||
@@ -42,7 +39,7 @@ pub mod blob_functions {
|
||||
ctx: NativeCallContext,
|
||||
len: INT,
|
||||
value: INT,
|
||||
) -> Result<Blob, Box<EvalAltResult>> {
|
||||
) -> RhaiResultOf<Blob> {
|
||||
let len = if len < 0 { 0 } else { len as usize };
|
||||
let _ctx = ctx;
|
||||
|
||||
@@ -118,7 +115,7 @@ pub mod blob_functions {
|
||||
blob: &mut Blob,
|
||||
len: INT,
|
||||
item: INT,
|
||||
) -> Result<(), Box<EvalAltResult>> {
|
||||
) -> RhaiResultOf<()> {
|
||||
if len <= 0 {
|
||||
return Ok(());
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
use crate::plugin::*;
|
||||
use crate::types::dynamic::Variant;
|
||||
use crate::{def_package, EvalAltResult, ExclusiveRange, InclusiveRange, INT};
|
||||
use crate::{def_package, EvalAltResult, ExclusiveRange, InclusiveRange, RhaiResultOf, INT};
|
||||
use std::iter::{ExactSizeIterator, FusedIterator};
|
||||
use std::ops::{Range, RangeInclusive};
|
||||
#[cfg(feature = "no_std")]
|
||||
@@ -22,7 +22,7 @@ impl<T> StepRange<T>
|
||||
where
|
||||
T: Variant + Copy + PartialOrd + Add<Output = T> + Sub<Output = T>,
|
||||
{
|
||||
pub fn new(from: T, to: T, step: T) -> Result<Self, Box<EvalAltResult>> {
|
||||
pub fn new(from: T, to: T, step: T) -> RhaiResultOf<Self> {
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
if let Some(r) = from.checked_add(&step) {
|
||||
if r == from {
|
||||
@@ -117,7 +117,7 @@ struct BitRange(INT, INT, usize);
|
||||
const BITS: usize = std::mem::size_of::<INT>() * 8;
|
||||
|
||||
impl BitRange {
|
||||
pub fn new(value: INT, from: INT, len: INT) -> Result<Self, Box<EvalAltResult>> {
|
||||
pub fn new(value: INT, from: INT, len: INT) -> RhaiResultOf<Self> {
|
||||
let from = if from >= 0 {
|
||||
let offset = from as usize;
|
||||
|
||||
@@ -334,7 +334,7 @@ def_package! {
|
||||
struct StepFloatRange(FLOAT, FLOAT, FLOAT);
|
||||
|
||||
impl StepFloatRange {
|
||||
pub fn new(from: FLOAT, to: FLOAT, step: FLOAT) -> Result<Self, Box<EvalAltResult>> {
|
||||
pub fn new(from: FLOAT, to: FLOAT, step: FLOAT) -> RhaiResultOf<Self> {
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
if step == 0.0 {
|
||||
return Err(EvalAltResult::ErrorInFunctionCall("range".to_string(), "".to_string(),
|
||||
@@ -396,7 +396,7 @@ def_package! {
|
||||
struct StepDecimalRange(Decimal, Decimal, Decimal);
|
||||
|
||||
impl StepDecimalRange {
|
||||
pub fn new(from: Decimal, to: Decimal, step: Decimal) -> Result<Self, Box<EvalAltResult>> {
|
||||
pub fn new(from: Decimal, to: Decimal, step: Decimal) -> RhaiResultOf<Self> {
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
if step.is_zero() {
|
||||
return Err(EvalAltResult::ErrorInFunctionCall("range".to_string(), "".to_string(),
|
||||
|
@@ -1,7 +1,7 @@
|
||||
use crate::def_package;
|
||||
use crate::plugin::*;
|
||||
use crate::types::dynamic::Tag;
|
||||
use crate::{Dynamic, EvalAltResult, INT};
|
||||
use crate::{Dynamic, EvalAltResult, RhaiResultOf, INT};
|
||||
#[cfg(feature = "no_std")]
|
||||
use std::prelude::v1::*;
|
||||
|
||||
@@ -21,7 +21,7 @@ mod core_functions {
|
||||
value.tag() as INT
|
||||
}
|
||||
#[rhai_fn(name = "set_tag", set = "tag", return_raw)]
|
||||
pub fn set_tag(value: &mut Dynamic, tag: INT) -> Result<(), Box<EvalAltResult>> {
|
||||
pub fn set_tag(value: &mut Dynamic, tag: INT) -> RhaiResultOf<()> {
|
||||
if tag < Tag::MIN as INT {
|
||||
Err(EvalAltResult::ErrorArithmetic(
|
||||
format!(
|
||||
|
@@ -1,7 +1,7 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use crate::plugin::*;
|
||||
use crate::{def_package, EvalAltResult, ExclusiveRange, InclusiveRange, INT};
|
||||
use crate::{def_package, EvalAltResult, ExclusiveRange, InclusiveRange, RhaiResultOf, INT};
|
||||
#[cfg(feature = "no_std")]
|
||||
use std::prelude::v1::*;
|
||||
|
||||
@@ -202,7 +202,7 @@ mod bit_field_functions {
|
||||
const BITS: usize = std::mem::size_of::<INT>() * 8;
|
||||
|
||||
#[rhai_fn(return_raw)]
|
||||
pub fn get_bit(value: INT, index: INT) -> Result<bool, Box<EvalAltResult>> {
|
||||
pub fn get_bit(value: INT, index: INT) -> RhaiResultOf<bool> {
|
||||
if index >= 0 {
|
||||
let offset = index as usize;
|
||||
|
||||
@@ -225,7 +225,7 @@ mod bit_field_functions {
|
||||
}
|
||||
}
|
||||
#[rhai_fn(return_raw)]
|
||||
pub fn set_bit(value: &mut INT, index: INT, new_value: bool) -> Result<(), Box<EvalAltResult>> {
|
||||
pub fn set_bit(value: &mut INT, index: INT, new_value: bool) -> RhaiResultOf<()> {
|
||||
if index >= 0 {
|
||||
let offset = index as usize;
|
||||
|
||||
@@ -260,22 +260,19 @@ mod bit_field_functions {
|
||||
}
|
||||
}
|
||||
#[rhai_fn(name = "get_bits", return_raw)]
|
||||
pub fn get_bits_range(value: INT, range: ExclusiveRange) -> Result<INT, Box<EvalAltResult>> {
|
||||
pub fn get_bits_range(value: INT, range: ExclusiveRange) -> RhaiResultOf<INT> {
|
||||
let from = INT::max(range.start, 0);
|
||||
let to = INT::max(range.end, from);
|
||||
get_bits(value, from, to - from)
|
||||
}
|
||||
#[rhai_fn(name = "get_bits", return_raw)]
|
||||
pub fn get_bits_range_inclusive(
|
||||
value: INT,
|
||||
range: InclusiveRange,
|
||||
) -> Result<INT, Box<EvalAltResult>> {
|
||||
pub fn get_bits_range_inclusive(value: INT, range: InclusiveRange) -> RhaiResultOf<INT> {
|
||||
let from = INT::max(*range.start(), 0);
|
||||
let to = INT::max(*range.end(), from - 1);
|
||||
get_bits(value, from, to - from + 1)
|
||||
}
|
||||
#[rhai_fn(return_raw)]
|
||||
pub fn get_bits(value: INT, index: INT, bits: INT) -> Result<INT, Box<EvalAltResult>> {
|
||||
pub fn get_bits(value: INT, index: INT, bits: INT) -> RhaiResultOf<INT> {
|
||||
if bits < 1 {
|
||||
return Ok(0);
|
||||
}
|
||||
@@ -321,7 +318,7 @@ mod bit_field_functions {
|
||||
value: &mut INT,
|
||||
range: ExclusiveRange,
|
||||
new_value: INT,
|
||||
) -> Result<(), Box<EvalAltResult>> {
|
||||
) -> RhaiResultOf<()> {
|
||||
let from = INT::max(range.start, 0);
|
||||
let to = INT::max(range.end, from);
|
||||
set_bits(value, from, to - from, new_value)
|
||||
@@ -331,18 +328,13 @@ mod bit_field_functions {
|
||||
value: &mut INT,
|
||||
range: InclusiveRange,
|
||||
new_value: INT,
|
||||
) -> Result<(), Box<EvalAltResult>> {
|
||||
) -> RhaiResultOf<()> {
|
||||
let from = INT::max(*range.start(), 0);
|
||||
let to = INT::max(*range.end(), from - 1);
|
||||
set_bits(value, from, to - from + 1, new_value)
|
||||
}
|
||||
#[rhai_fn(return_raw)]
|
||||
pub fn set_bits(
|
||||
value: &mut INT,
|
||||
index: INT,
|
||||
bits: INT,
|
||||
new_value: INT,
|
||||
) -> Result<(), Box<EvalAltResult>> {
|
||||
pub fn set_bits(value: &mut INT, index: INT, bits: INT, new_value: INT) -> RhaiResultOf<()> {
|
||||
if bits < 1 {
|
||||
return Ok(());
|
||||
}
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
use crate::engine::OP_EQUALS;
|
||||
use crate::plugin::*;
|
||||
use crate::{def_package, Dynamic, ImmutableString, Map, INT};
|
||||
use crate::{def_package, Dynamic, ImmutableString, Map, RhaiResultOf, INT};
|
||||
#[cfg(feature = "no_std")]
|
||||
use std::prelude::v1::*;
|
||||
|
||||
@@ -66,11 +66,7 @@ mod map_functions {
|
||||
}
|
||||
}
|
||||
#[rhai_fn(name = "==", return_raw, pure)]
|
||||
pub fn equals(
|
||||
ctx: NativeCallContext,
|
||||
map1: &mut Map,
|
||||
map2: Map,
|
||||
) -> Result<bool, Box<EvalAltResult>> {
|
||||
pub fn equals(ctx: NativeCallContext, map1: &mut Map, map2: Map) -> RhaiResultOf<bool> {
|
||||
if map1.len() != map2.len() {
|
||||
return Ok(false);
|
||||
}
|
||||
@@ -96,11 +92,7 @@ mod map_functions {
|
||||
Ok(true)
|
||||
}
|
||||
#[rhai_fn(name = "!=", return_raw, pure)]
|
||||
pub fn not_equals(
|
||||
ctx: NativeCallContext,
|
||||
map1: &mut Map,
|
||||
map2: Map,
|
||||
) -> Result<bool, Box<EvalAltResult>> {
|
||||
pub fn not_equals(ctx: NativeCallContext, map1: &mut Map, map2: Map) -> RhaiResultOf<bool> {
|
||||
equals(ctx, map1, map2).map(|r| !r)
|
||||
}
|
||||
|
||||
|
@@ -1,7 +1,7 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use crate::plugin::*;
|
||||
use crate::{def_package, Position, INT, INT_BASE};
|
||||
use crate::{def_package, Position, RhaiResultOf, INT, INT_BASE};
|
||||
#[cfg(feature = "no_std")]
|
||||
use std::prelude::v1::*;
|
||||
|
||||
@@ -114,7 +114,7 @@ def_package! {
|
||||
#[export_module]
|
||||
mod int_functions {
|
||||
#[rhai_fn(name = "parse_int", return_raw)]
|
||||
pub fn parse_int_radix(string: &str, radix: INT) -> Result<INT, Box<EvalAltResult>> {
|
||||
pub fn parse_int_radix(string: &str, radix: INT) -> RhaiResultOf<INT> {
|
||||
if !(2..=36).contains(&radix) {
|
||||
return Err(EvalAltResult::ErrorArithmetic(
|
||||
format!("Invalid radix: '{}'", radix),
|
||||
@@ -134,7 +134,7 @@ mod int_functions {
|
||||
})
|
||||
}
|
||||
#[rhai_fn(name = "parse_int", return_raw)]
|
||||
pub fn parse_int(string: &str) -> Result<INT, Box<EvalAltResult>> {
|
||||
pub fn parse_int(string: &str) -> RhaiResultOf<INT> {
|
||||
parse_int_radix(string, 10)
|
||||
}
|
||||
}
|
||||
@@ -263,7 +263,7 @@ mod float_functions {
|
||||
x.is_infinite()
|
||||
}
|
||||
#[rhai_fn(name = "to_int", return_raw)]
|
||||
pub fn f32_to_int(x: f32) -> Result<INT, Box<EvalAltResult>> {
|
||||
pub fn f32_to_int(x: f32) -> RhaiResultOf<INT> {
|
||||
if cfg!(not(feature = "unchecked")) && x > (MAX_INT as f32) {
|
||||
Err(EvalAltResult::ErrorArithmetic(
|
||||
format!("Integer overflow: to_int({})", x),
|
||||
@@ -275,7 +275,7 @@ mod float_functions {
|
||||
}
|
||||
}
|
||||
#[rhai_fn(name = "to_int", return_raw)]
|
||||
pub fn f64_to_int(x: f64) -> Result<INT, Box<EvalAltResult>> {
|
||||
pub fn f64_to_int(x: f64) -> RhaiResultOf<INT> {
|
||||
if cfg!(not(feature = "unchecked")) && x > (MAX_INT as f64) {
|
||||
Err(EvalAltResult::ErrorArithmetic(
|
||||
format!("Integer overflow: to_int({})", x),
|
||||
@@ -287,7 +287,7 @@ mod float_functions {
|
||||
}
|
||||
}
|
||||
#[rhai_fn(return_raw)]
|
||||
pub fn parse_float(string: &str) -> Result<FLOAT, Box<EvalAltResult>> {
|
||||
pub fn parse_float(string: &str) -> RhaiResultOf<FLOAT> {
|
||||
string.trim().parse::<FLOAT>().map_err(|err| {
|
||||
EvalAltResult::ErrorArithmetic(
|
||||
format!("Error parsing floating-point number '{}': {}", string, err),
|
||||
@@ -325,7 +325,7 @@ mod decimal_functions {
|
||||
}
|
||||
#[cfg(feature = "no_float")]
|
||||
#[rhai_fn(return_raw)]
|
||||
pub fn parse_float(s: &str) -> Result<Decimal, Box<EvalAltResult>> {
|
||||
pub fn parse_float(s: &str) -> RhaiResultOf<Decimal> {
|
||||
parse_decimal(s)
|
||||
}
|
||||
|
||||
@@ -339,12 +339,12 @@ mod decimal_functions {
|
||||
x.tan()
|
||||
}
|
||||
#[rhai_fn(return_raw)]
|
||||
pub fn sqrt(x: Decimal) -> Result<Decimal, Box<EvalAltResult>> {
|
||||
pub fn sqrt(x: Decimal) -> RhaiResultOf<Decimal> {
|
||||
x.sqrt()
|
||||
.ok_or_else(|| make_err(format!("Error taking the square root of {}", x,)))
|
||||
}
|
||||
#[rhai_fn(return_raw)]
|
||||
pub fn exp(x: Decimal) -> Result<Decimal, Box<EvalAltResult>> {
|
||||
pub fn exp(x: Decimal) -> RhaiResultOf<Decimal> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
x.checked_exp()
|
||||
.ok_or_else(|| make_err(format!("Exponential overflow: e ** {}", x,)))
|
||||
@@ -353,7 +353,7 @@ mod decimal_functions {
|
||||
}
|
||||
}
|
||||
#[rhai_fn(return_raw)]
|
||||
pub fn ln(x: Decimal) -> Result<Decimal, Box<EvalAltResult>> {
|
||||
pub fn ln(x: Decimal) -> RhaiResultOf<Decimal> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
x.checked_ln()
|
||||
.ok_or_else(|| make_err(format!("Error taking the natural log of {}", x)))
|
||||
@@ -362,7 +362,7 @@ mod decimal_functions {
|
||||
}
|
||||
}
|
||||
#[rhai_fn(name = "log", return_raw)]
|
||||
pub fn log10(x: Decimal) -> Result<Decimal, Box<EvalAltResult>> {
|
||||
pub fn log10(x: Decimal) -> RhaiResultOf<Decimal> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
x.checked_log10()
|
||||
.ok_or_else(|| make_err(format!("Error taking the log of {}", x)))
|
||||
@@ -383,7 +383,7 @@ mod decimal_functions {
|
||||
x.round()
|
||||
}
|
||||
#[rhai_fn(name = "round", return_raw)]
|
||||
pub fn round_dp(x: Decimal, dp: INT) -> Result<Decimal, Box<EvalAltResult>> {
|
||||
pub fn round_dp(x: Decimal, dp: INT) -> RhaiResultOf<Decimal> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
if dp < 0 {
|
||||
return Err(make_err(format!(
|
||||
@@ -399,7 +399,7 @@ mod decimal_functions {
|
||||
Ok(x.round_dp(dp as u32))
|
||||
}
|
||||
#[rhai_fn(return_raw)]
|
||||
pub fn round_up(x: Decimal, dp: INT) -> Result<Decimal, Box<EvalAltResult>> {
|
||||
pub fn round_up(x: Decimal, dp: INT) -> RhaiResultOf<Decimal> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
if dp < 0 {
|
||||
return Err(make_err(format!(
|
||||
@@ -415,7 +415,7 @@ mod decimal_functions {
|
||||
Ok(x.round_dp_with_strategy(dp as u32, RoundingStrategy::AwayFromZero))
|
||||
}
|
||||
#[rhai_fn(return_raw)]
|
||||
pub fn round_down(x: Decimal, dp: INT) -> Result<Decimal, Box<EvalAltResult>> {
|
||||
pub fn round_down(x: Decimal, dp: INT) -> RhaiResultOf<Decimal> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
if dp < 0 {
|
||||
return Err(make_err(format!(
|
||||
@@ -431,7 +431,7 @@ mod decimal_functions {
|
||||
Ok(x.round_dp_with_strategy(dp as u32, RoundingStrategy::ToZero))
|
||||
}
|
||||
#[rhai_fn(return_raw)]
|
||||
pub fn round_half_up(x: Decimal, dp: INT) -> Result<Decimal, Box<EvalAltResult>> {
|
||||
pub fn round_half_up(x: Decimal, dp: INT) -> RhaiResultOf<Decimal> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
if dp < 0 {
|
||||
return Err(make_err(format!(
|
||||
@@ -447,7 +447,7 @@ mod decimal_functions {
|
||||
Ok(x.round_dp_with_strategy(dp as u32, RoundingStrategy::MidpointAwayFromZero))
|
||||
}
|
||||
#[rhai_fn(return_raw)]
|
||||
pub fn round_half_down(x: Decimal, dp: INT) -> Result<Decimal, Box<EvalAltResult>> {
|
||||
pub fn round_half_down(x: Decimal, dp: INT) -> RhaiResultOf<Decimal> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
if dp < 0 {
|
||||
return Err(make_err(format!(
|
||||
@@ -471,7 +471,7 @@ mod decimal_functions {
|
||||
x.fract()
|
||||
}
|
||||
#[rhai_fn(return_raw)]
|
||||
pub fn parse_decimal(string: &str) -> Result<Decimal, Box<EvalAltResult>> {
|
||||
pub fn parse_decimal(string: &str) -> RhaiResultOf<Decimal> {
|
||||
Decimal::from_str(string)
|
||||
.or_else(|_| Decimal::from_scientific(string))
|
||||
.map_err(|err| {
|
||||
@@ -485,7 +485,7 @@ mod decimal_functions {
|
||||
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
#[rhai_fn(name = "to_decimal", return_raw)]
|
||||
pub fn f32_to_decimal(x: f32) -> Result<Decimal, Box<EvalAltResult>> {
|
||||
pub fn f32_to_decimal(x: f32) -> RhaiResultOf<Decimal> {
|
||||
Decimal::try_from(x).map_err(|_| {
|
||||
EvalAltResult::ErrorArithmetic(
|
||||
format!("Cannot convert to Decimal: to_decimal({})", x),
|
||||
@@ -496,7 +496,7 @@ mod decimal_functions {
|
||||
}
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
#[rhai_fn(name = "to_decimal", return_raw)]
|
||||
pub fn f64_to_decimal(x: f64) -> Result<Decimal, Box<EvalAltResult>> {
|
||||
pub fn f64_to_decimal(x: f64) -> RhaiResultOf<Decimal> {
|
||||
Decimal::try_from(x).map_err(|_| {
|
||||
EvalAltResult::ErrorArithmetic(
|
||||
format!("Cannot convert to Decimal: to_decimal({})", x),
|
||||
@@ -507,7 +507,7 @@ mod decimal_functions {
|
||||
}
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
#[rhai_fn(return_raw)]
|
||||
pub fn to_float(x: Decimal) -> Result<FLOAT, Box<EvalAltResult>> {
|
||||
pub fn to_float(x: Decimal) -> RhaiResultOf<FLOAT> {
|
||||
FLOAT::try_from(x).map_err(|_| {
|
||||
EvalAltResult::ErrorArithmetic(
|
||||
format!("Cannot convert to floating-point: to_float({})", x),
|
||||
|
@@ -1,7 +1,7 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use crate::plugin::*;
|
||||
use crate::{def_package, Dynamic, ExclusiveRange, InclusiveRange, StaticVec, INT};
|
||||
use crate::{def_package, Dynamic, ExclusiveRange, InclusiveRange, RhaiResultOf, StaticVec, INT};
|
||||
#[cfg(feature = "no_std")]
|
||||
use std::prelude::v1::*;
|
||||
use std::{any::TypeId, mem};
|
||||
@@ -500,7 +500,7 @@ mod string_functions {
|
||||
string: &mut ImmutableString,
|
||||
len: INT,
|
||||
character: char,
|
||||
) -> Result<(), Box<crate::EvalAltResult>> {
|
||||
) -> RhaiResultOf<()> {
|
||||
if len <= 0 {
|
||||
return Ok(());
|
||||
}
|
||||
@@ -544,7 +544,7 @@ mod string_functions {
|
||||
string: &mut ImmutableString,
|
||||
len: INT,
|
||||
padding: &str,
|
||||
) -> Result<(), Box<crate::EvalAltResult>> {
|
||||
) -> RhaiResultOf<()> {
|
||||
if len <= 0 {
|
||||
return Ok(());
|
||||
}
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
use super::{arithmetic::make_err as make_arithmetic_err, math_basic::MAX_INT};
|
||||
use crate::plugin::*;
|
||||
use crate::{def_package, Dynamic, EvalAltResult, INT};
|
||||
use crate::{def_package, Dynamic, EvalAltResult, RhaiResult, RhaiResultOf, INT};
|
||||
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
use crate::FLOAT;
|
||||
@@ -30,7 +30,7 @@ mod time_functions {
|
||||
}
|
||||
|
||||
#[rhai_fn(name = "elapsed", get = "elapsed", return_raw)]
|
||||
pub fn elapsed(timestamp: Instant) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
pub fn elapsed(timestamp: Instant) -> RhaiResult {
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
if timestamp > Instant::now() {
|
||||
Err(make_arithmetic_err("Time-stamp is later than now"))
|
||||
@@ -56,10 +56,7 @@ mod time_functions {
|
||||
}
|
||||
|
||||
#[rhai_fn(return_raw, name = "-")]
|
||||
pub fn time_diff(
|
||||
timestamp1: Instant,
|
||||
timestamp2: Instant,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
pub fn time_diff(timestamp1: Instant, timestamp2: Instant) -> RhaiResult {
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
return Ok(if timestamp2 > timestamp1 {
|
||||
-(timestamp2 - timestamp1).as_secs_f64() as FLOAT
|
||||
@@ -96,7 +93,7 @@ mod time_functions {
|
||||
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
pub mod float_functions {
|
||||
fn add_impl(timestamp: Instant, seconds: FLOAT) -> Result<Instant, Box<EvalAltResult>> {
|
||||
fn add_impl(timestamp: Instant, seconds: FLOAT) -> RhaiResultOf<Instant> {
|
||||
if seconds < 0.0 {
|
||||
subtract_impl(timestamp, -seconds)
|
||||
} else if cfg!(not(feature = "unchecked")) {
|
||||
@@ -119,10 +116,7 @@ mod time_functions {
|
||||
Ok(timestamp + Duration::from_millis((seconds * 1000.0) as u64))
|
||||
}
|
||||
}
|
||||
fn subtract_impl(
|
||||
timestamp: Instant,
|
||||
seconds: FLOAT,
|
||||
) -> Result<Instant, Box<EvalAltResult>> {
|
||||
fn subtract_impl(timestamp: Instant, seconds: FLOAT) -> RhaiResultOf<Instant> {
|
||||
if seconds < 0.0 {
|
||||
add_impl(timestamp, -seconds)
|
||||
} else if cfg!(not(feature = "unchecked")) {
|
||||
@@ -147,32 +141,26 @@ mod time_functions {
|
||||
}
|
||||
|
||||
#[rhai_fn(return_raw, name = "+")]
|
||||
pub fn add(timestamp: Instant, seconds: FLOAT) -> Result<Instant, Box<EvalAltResult>> {
|
||||
pub fn add(timestamp: Instant, seconds: FLOAT) -> RhaiResultOf<Instant> {
|
||||
add_impl(timestamp, seconds)
|
||||
}
|
||||
#[rhai_fn(return_raw, name = "+=")]
|
||||
pub fn add_assign(
|
||||
timestamp: &mut Instant,
|
||||
seconds: FLOAT,
|
||||
) -> Result<(), Box<EvalAltResult>> {
|
||||
pub fn add_assign(timestamp: &mut Instant, seconds: FLOAT) -> RhaiResultOf<()> {
|
||||
*timestamp = add_impl(*timestamp, seconds)?;
|
||||
Ok(())
|
||||
}
|
||||
#[rhai_fn(return_raw, name = "-")]
|
||||
pub fn subtract(timestamp: Instant, seconds: FLOAT) -> Result<Instant, Box<EvalAltResult>> {
|
||||
pub fn subtract(timestamp: Instant, seconds: FLOAT) -> RhaiResultOf<Instant> {
|
||||
subtract_impl(timestamp, seconds)
|
||||
}
|
||||
#[rhai_fn(return_raw, name = "-=")]
|
||||
pub fn subtract_assign(
|
||||
timestamp: &mut Instant,
|
||||
seconds: FLOAT,
|
||||
) -> Result<(), Box<EvalAltResult>> {
|
||||
pub fn subtract_assign(timestamp: &mut Instant, seconds: FLOAT) -> RhaiResultOf<()> {
|
||||
*timestamp = subtract_impl(*timestamp, seconds)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn add_impl(timestamp: Instant, seconds: INT) -> Result<Instant, Box<EvalAltResult>> {
|
||||
fn add_impl(timestamp: Instant, seconds: INT) -> RhaiResultOf<Instant> {
|
||||
if seconds < 0 {
|
||||
subtract_impl(timestamp, -seconds)
|
||||
} else if cfg!(not(feature = "unchecked")) {
|
||||
@@ -188,7 +176,7 @@ mod time_functions {
|
||||
Ok(timestamp + Duration::from_secs(seconds as u64))
|
||||
}
|
||||
}
|
||||
fn subtract_impl(timestamp: Instant, seconds: INT) -> Result<Instant, Box<EvalAltResult>> {
|
||||
fn subtract_impl(timestamp: Instant, seconds: INT) -> RhaiResultOf<Instant> {
|
||||
if seconds < 0 {
|
||||
add_impl(timestamp, -seconds)
|
||||
} else if cfg!(not(feature = "unchecked")) {
|
||||
@@ -206,23 +194,20 @@ mod time_functions {
|
||||
}
|
||||
|
||||
#[rhai_fn(return_raw, name = "+")]
|
||||
pub fn add(timestamp: Instant, seconds: INT) -> Result<Instant, Box<EvalAltResult>> {
|
||||
pub fn add(timestamp: Instant, seconds: INT) -> RhaiResultOf<Instant> {
|
||||
add_impl(timestamp, seconds)
|
||||
}
|
||||
#[rhai_fn(return_raw, name = "+=")]
|
||||
pub fn add_assign(timestamp: &mut Instant, seconds: INT) -> Result<(), Box<EvalAltResult>> {
|
||||
pub fn add_assign(timestamp: &mut Instant, seconds: INT) -> RhaiResultOf<()> {
|
||||
*timestamp = add_impl(*timestamp, seconds)?;
|
||||
Ok(())
|
||||
}
|
||||
#[rhai_fn(return_raw, name = "-")]
|
||||
pub fn subtract(timestamp: Instant, seconds: INT) -> Result<Instant, Box<EvalAltResult>> {
|
||||
pub fn subtract(timestamp: Instant, seconds: INT) -> RhaiResultOf<Instant> {
|
||||
subtract_impl(timestamp, seconds)
|
||||
}
|
||||
#[rhai_fn(return_raw, name = "-=")]
|
||||
pub fn subtract_assign(
|
||||
timestamp: &mut Instant,
|
||||
seconds: INT,
|
||||
) -> Result<(), Box<EvalAltResult>> {
|
||||
pub fn subtract_assign(timestamp: &mut Instant, seconds: INT) -> RhaiResultOf<()> {
|
||||
*timestamp = subtract_impl(*timestamp, seconds)?;
|
||||
Ok(())
|
||||
}
|
||||
|
Reference in New Issue
Block a user