Use type alias

This commit is contained in:
Stephen Chung
2021-12-25 23:49:14 +08:00
parent 43363e0660
commit 01c35808cb
42 changed files with 538 additions and 702 deletions

View File

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