Refine calc_index API.

This commit is contained in:
Stephen Chung 2022-01-15 10:24:08 +08:00
parent a8ec984b0f
commit 2a8a8c00f5
2 changed files with 24 additions and 22 deletions

View File

@ -790,8 +790,9 @@ impl Engine {
.as_int()
.map_err(|typ| self.make_type_mismatch_err::<crate::INT>(typ, pos))?;
let len = arr.len();
let arr_idx =
calc_index(len, index, true, || ERR::ErrorArrayBounds(len, index, pos))?;
let arr_idx = calc_index(len, index, true, || {
ERR::ErrorArrayBounds(len, index, pos).into()
})?;
Ok(arr.get_mut(arr_idx).map(Target::from).unwrap())
}
@ -803,8 +804,9 @@ impl Engine {
.as_int()
.map_err(|typ| self.make_type_mismatch_err::<crate::INT>(typ, pos))?;
let len = arr.len();
let arr_idx =
calc_index(len, index, true, || ERR::ErrorArrayBounds(len, index, pos))?;
let arr_idx = calc_index(len, index, true, || {
ERR::ErrorArrayBounds(len, index, pos).into()
})?;
let value = arr.get(arr_idx).map(|&v| (v as crate::INT).into()).unwrap();
@ -844,10 +846,10 @@ impl Engine {
let end = range.end;
let start = calc_index(BITS, start, false, || {
ERR::ErrorBitFieldBounds(BITS, start, pos)
ERR::ErrorBitFieldBounds(BITS, start, pos).into()
})?;
let end = calc_index(BITS, end, false, || {
ERR::ErrorBitFieldBounds(BITS, end, pos)
ERR::ErrorBitFieldBounds(BITS, end, pos).into()
})?;
if end <= start {
@ -869,10 +871,10 @@ impl Engine {
let end = *range.end();
let start = calc_index(BITS, start, false, || {
ERR::ErrorBitFieldBounds(BITS, start, pos)
ERR::ErrorBitFieldBounds(BITS, start, pos).into()
})?;
let end = calc_index(BITS, end, false, || {
ERR::ErrorBitFieldBounds(BITS, end, pos)
ERR::ErrorBitFieldBounds(BITS, end, pos).into()
})?;
if end < start {
@ -913,7 +915,7 @@ impl Engine {
const BITS: usize = std::mem::size_of::<crate::INT>() * 8;
let bit = calc_index(BITS, index, true, || {
ERR::ErrorBitFieldBounds(BITS, index, pos)
ERR::ErrorBitFieldBounds(BITS, index, pos).into()
})?;
let bit_value = (*value & (1 << bit)) != 0;

View File

@ -1,7 +1,7 @@
//! Type to hold a mutable reference to the target of an evaluation.
use crate::types::dynamic::Variant;
use crate::{Dynamic, EvalAltResult, RhaiResultOf, INT};
use crate::{Dynamic, RhaiResultOf, INT};
use std::ops::{Deref, DerefMut};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
@ -38,35 +38,35 @@ pub fn calc_offset_len(length: usize, start: INT, len: INT) -> (usize, usize) {
//
// Negative starting positions count from the end.
//
// Values going over bounds call the provided closure to create an error.
// Values going over bounds call the provided closure to return a default value or an error.
#[inline(always)]
pub fn calc_index(
pub fn calc_index<E>(
length: usize,
start: INT,
negative_count_from_end: bool,
err: impl Fn() -> EvalAltResult,
) -> RhaiResultOf<usize> {
err: impl Fn() -> Result<usize, E>,
) -> Result<usize, E> {
if start < 0 {
if negative_count_from_end {
// Count from end if negative
#[cfg(not(feature = "unchecked"))]
return start
.checked_abs()
.ok_or_else(|| err().into())
.and_then(|positive_start| {
return match start.checked_abs() {
Some(positive_start) => {
if (positive_start as usize) > length {
Err(err().into())
err()
} else {
Ok(length - (positive_start as usize))
}
});
}
None => err(),
};
#[cfg(feature = "unchecked")]
return Ok(length - (start.abs() as usize));
} else {
Err(err().into())
err()
}
} else if start as usize >= length {
Err(err().into())
err()
} else {
Ok(start as usize)
}