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)))
|
||||
|
Reference in New Issue
Block a user