Minor refactor.

This commit is contained in:
Stephen Chung
2022-03-09 09:25:55 +08:00
parent 89426f8b3a
commit 1e4abd012c
11 changed files with 86 additions and 74 deletions

View File

@@ -1,7 +1,8 @@
use crate::eval::calc_index;
use crate::plugin::*;
use crate::{
def_package, ExclusiveRange, InclusiveRange, Position, RhaiResultOf, ERR, INT, UNSIGNED_INT,
def_package, ExclusiveRange, InclusiveRange, Position, RhaiResultOf, ERR, INT, INT_BITS,
UNSIGNED_INT,
};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
@@ -17,8 +18,6 @@ def_package! {
#[export_module]
mod bit_field_functions {
const BITS: usize = std::mem::size_of::<INT>() * 8;
/// Return `true` if the specified `bit` in the number is set.
///
/// If `bit` < 0, position counts from the MSB (Most Significant Bit).
@@ -36,8 +35,8 @@ mod bit_field_functions {
/// ```
#[rhai_fn(return_raw)]
pub fn get_bit(value: INT, bit: INT) -> RhaiResultOf<bool> {
let bit = calc_index(BITS, bit, true, || {
ERR::ErrorBitFieldBounds(BITS, bit, Position::NONE).into()
let bit = calc_index(INT_BITS, bit, true, || {
ERR::ErrorBitFieldBounds(INT_BITS, bit, Position::NONE).into()
})?;
Ok((value & (1 << bit)) != 0)
@@ -66,8 +65,8 @@ mod bit_field_functions {
/// ```
#[rhai_fn(return_raw)]
pub fn set_bit(value: &mut INT, bit: INT, new_value: bool) -> RhaiResultOf<()> {
let bit = calc_index(BITS, bit, true, || {
ERR::ErrorBitFieldBounds(BITS, bit, Position::NONE).into()
let bit = calc_index(INT_BITS, bit, true, || {
ERR::ErrorBitFieldBounds(INT_BITS, bit, Position::NONE).into()
})?;
let mask = 1 << bit;
@@ -128,17 +127,17 @@ mod bit_field_functions {
return Ok(0);
}
let bit = calc_index(BITS, start, true, || {
ERR::ErrorBitFieldBounds(BITS, start, Position::NONE).into()
let bit = calc_index(INT_BITS, start, true, || {
ERR::ErrorBitFieldBounds(INT_BITS, start, Position::NONE).into()
})?;
let bits = if bit + bits as usize > BITS {
BITS - bit
let bits = if bit + bits as usize > INT_BITS {
INT_BITS - bit
} else {
bits as usize
};
if bit == 0 && bits == BITS {
if bit == 0 && bits == INT_BITS {
return Ok(value);
}
@@ -214,17 +213,17 @@ mod bit_field_functions {
return Ok(());
}
let bit = calc_index(BITS, bit, true, || {
ERR::ErrorBitFieldBounds(BITS, bit, Position::NONE).into()
let bit = calc_index(INT_BITS, bit, true, || {
ERR::ErrorBitFieldBounds(INT_BITS, bit, Position::NONE).into()
})?;
let bits = if bit + bits as usize > BITS {
BITS - bit
let bits = if bit + bits as usize > INT_BITS {
INT_BITS - bit
} else {
bits as usize
};
if bit == 0 && bits == BITS {
if bit == 0 && bits == INT_BITS {
*value = new_value;
return Ok(());
}

View File

@@ -4,19 +4,14 @@ use crate::eval::{calc_index, calc_offset_len};
use crate::plugin::*;
use crate::{
def_package, Array, Blob, Dynamic, ExclusiveRange, InclusiveRange, NativeCallContext, Position,
RhaiResultOf, INT,
RhaiResultOf, INT, INT_BYTES,
};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
use std::{any::TypeId, mem};
#[cfg(not(feature = "no_float"))]
use crate::FLOAT;
const INT_BYTES: usize = mem::size_of::<INT>();
#[cfg(not(feature = "no_float"))]
const FLOAT_BYTES: usize = mem::size_of::<FLOAT>();
use crate::{FLOAT, FLOAT_BYTES};
def_package! {
/// Package of basic BLOB utilities.

View File

@@ -1,7 +1,7 @@
use crate::eval::calc_index;
use crate::plugin::*;
use crate::types::dynamic::Variant;
use crate::{def_package, ExclusiveRange, InclusiveRange, RhaiResultOf, INT};
use crate::{def_package, ExclusiveRange, InclusiveRange, RhaiResultOf, INT, INT_BITS};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
use std::{
@@ -117,18 +117,16 @@ impl<T> FusedIterator for StepRange<T> where
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
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) -> RhaiResultOf<Self> {
let from = calc_index(BITS, from, true, || {
crate::ERR::ErrorBitFieldBounds(BITS, from, Position::NONE).into()
let from = calc_index(INT_BITS, from, true, || {
crate::ERR::ErrorBitFieldBounds(INT_BITS, from, Position::NONE).into()
})?;
let len = if len < 0 {
0
} else if from + (len as usize) > BITS {
BITS - from
} else if from + (len as usize) > INT_BITS {
INT_BITS - from
} else {
len as usize
};

View File

@@ -31,6 +31,16 @@ mod time_functions {
}
/// Return the number of seconds between the current system time and the timestamp.
///
/// # Example
///
/// ```rhai
/// let now = timestamp();
///
/// sleep(10.0); // sleep for 10 seconds
///
/// print(now.elapsed); // prints 10.???
/// ```
#[rhai_fn(name = "elapsed", get = "elapsed", return_raw)]
pub fn elapsed(timestamp: Instant) -> RhaiResult {
#[cfg(not(feature = "no_float"))]