Add into_array and into_typed_array.

This commit is contained in:
Stephen Chung 2021-11-23 12:45:59 +08:00
parent 90200e607c
commit 6ad7b344bf
5 changed files with 192 additions and 83 deletions

View File

@ -1,6 +1,15 @@
Rhai Release Notes Rhai Release Notes
================== ==================
Version 1.3.0
=============
Enhancements
------------
* Added `into_array` and `into_typed_array` for `Dynamic`.
Version 1.2.0 Version 1.2.0
============= =============

View File

@ -1862,7 +1862,7 @@ pub struct FnCallExpr {
/// Constant arguments are very common in function calls, and keeping each constant in /// Constant arguments are very common in function calls, and keeping each constant in
/// an [`Expr::DynamicConstant`] involves an additional allocation. Keeping the constant /// an [`Expr::DynamicConstant`] involves an additional allocation. Keeping the constant
/// values in an inlined array avoids these extra allocations. /// values in an inlined array avoids these extra allocations.
pub constants: smallvec::SmallVec<[Dynamic; 2]>, pub constants: StaticVec<Dynamic>,
/// Does this function call capture the parent scope? /// Does this function call capture the parent scope?
pub capture_parent_scope: bool, pub capture_parent_scope: bool,
} }

View File

@ -1798,7 +1798,7 @@ impl Dynamic {
_ => None, _ => None,
} }
} }
/// Cast the [`Dynamic`] as a unit `()` and return it. /// Cast the [`Dynamic`] as a unit `()`.
/// Returns the name of the actual type if the cast fails. /// Returns the name of the actual type if the cast fails.
#[inline] #[inline]
pub fn as_unit(&self) -> Result<(), &'static str> { pub fn as_unit(&self) -> Result<(), &'static str> {
@ -1809,7 +1809,7 @@ impl Dynamic {
_ => Err(self.type_name()), _ => Err(self.type_name()),
} }
} }
/// Cast the [`Dynamic`] as the system integer type [`INT`] and return it. /// Cast the [`Dynamic`] as the system integer type [`INT`].
/// Returns the name of the actual type if the cast fails. /// Returns the name of the actual type if the cast fails.
#[inline] #[inline]
pub fn as_int(&self) -> Result<INT, &'static str> { pub fn as_int(&self) -> Result<INT, &'static str> {
@ -1820,7 +1820,7 @@ impl Dynamic {
_ => Err(self.type_name()), _ => Err(self.type_name()),
} }
} }
/// Cast the [`Dynamic`] as the system floating-point type [`FLOAT`] and return it. /// Cast the [`Dynamic`] as the system floating-point type [`FLOAT`].
/// Returns the name of the actual type if the cast fails. /// Returns the name of the actual type if the cast fails.
/// ///
/// Not available under `no_float`. /// Not available under `no_float`.
@ -1834,7 +1834,7 @@ impl Dynamic {
_ => Err(self.type_name()), _ => Err(self.type_name()),
} }
} }
/// _(decimal)_ Cast the [`Dynamic`] as a [`Decimal`](https://docs.rs/rust_decimal) and return it. /// _(decimal)_ Cast the [`Dynamic`] as a [`Decimal`](https://docs.rs/rust_decimal).
/// Returns the name of the actual type if the cast fails. /// Returns the name of the actual type if the cast fails.
/// ///
/// Exported under the `decimal` feature only. /// Exported under the `decimal` feature only.
@ -1848,7 +1848,7 @@ impl Dynamic {
_ => Err(self.type_name()), _ => Err(self.type_name()),
} }
} }
/// Cast the [`Dynamic`] as a [`bool`] and return it. /// Cast the [`Dynamic`] as a [`bool`].
/// Returns the name of the actual type if the cast fails. /// Returns the name of the actual type if the cast fails.
#[inline] #[inline]
pub fn as_bool(&self) -> Result<bool, &'static str> { pub fn as_bool(&self) -> Result<bool, &'static str> {
@ -1859,7 +1859,7 @@ impl Dynamic {
_ => Err(self.type_name()), _ => Err(self.type_name()),
} }
} }
/// Cast the [`Dynamic`] as a [`char`] and return it. /// Cast the [`Dynamic`] as a [`char`].
/// Returns the name of the actual type if the cast fails. /// Returns the name of the actual type if the cast fails.
#[inline] #[inline]
pub fn as_char(&self) -> Result<char, &'static str> { pub fn as_char(&self) -> Result<char, &'static str> {
@ -1870,7 +1870,7 @@ impl Dynamic {
_ => Err(self.type_name()), _ => Err(self.type_name()),
} }
} }
/// Cast the [`Dynamic`] as an [`ImmutableString`] and return it as a string slice. /// Cast the [`Dynamic`] as a string slice.
/// Returns the name of the actual type if the cast fails. /// Returns the name of the actual type if the cast fails.
/// ///
/// # Panics /// # Panics
@ -1885,7 +1885,7 @@ impl Dynamic {
_ => Err(self.type_name()), _ => Err(self.type_name()),
} }
} }
/// Convert the [`Dynamic`] into a [`String`] and return it. /// Convert the [`Dynamic`] into a [`String`].
/// If there are other references to the same string, a cloned copy is returned. /// If there are other references to the same string, a cloned copy is returned.
/// Returns the name of the actual type if the cast fails. /// Returns the name of the actual type if the cast fails.
#[inline] #[inline]
@ -1893,7 +1893,7 @@ impl Dynamic {
self.into_immutable_string() self.into_immutable_string()
.map(ImmutableString::into_owned) .map(ImmutableString::into_owned)
} }
/// Convert the [`Dynamic`] into an [`ImmutableString`] and return it. /// Convert the [`Dynamic`] into an [`ImmutableString`].
/// Returns the name of the actual type if the cast fails. /// Returns the name of the actual type if the cast fails.
#[inline] #[inline]
pub fn into_immutable_string(self) -> Result<ImmutableString, &'static str> { pub fn into_immutable_string(self) -> Result<ImmutableString, &'static str> {
@ -2021,6 +2021,82 @@ impl<T: Variant + Clone> std::iter::FromIterator<T> for Dynamic {
)) ))
} }
} }
#[cfg(not(feature = "no_index"))]
impl Dynamic {
/// Convert the [`Dynamic`] into an [`Array`].
/// Returns the name of the actual type if the cast fails.
#[inline(always)]
pub fn into_array(self) -> Result<Array, &'static str> {
match self.0 {
Union::Array(a, _, _) => Ok(*a),
#[cfg(not(feature = "no_closure"))]
Union::Shared(cell, _, _) => {
#[cfg(not(feature = "sync"))]
let value = cell.borrow();
#[cfg(feature = "sync")]
let value = cell.read().unwrap();
match value.0 {
Union::Array(ref a, _, _) => Ok(a.as_ref().clone()),
_ => Err((*value).type_name()),
}
}
_ => Err(self.type_name()),
}
}
/// Convert the [`Dynamic`] into a [`Vec`].
/// Returns the name of the actual type if any cast fails.
#[inline(always)]
pub fn into_typed_array<T: Variant + Clone>(self) -> Result<Vec<T>, &'static str> {
match self.0 {
Union::Array(a, _, _) => a
.into_iter()
.map(|v| {
#[cfg(not(feature = "no_closure"))]
let typ = if v.is_shared() {
// Avoid panics/deadlocks with shared values
"<shared>"
} else {
v.type_name()
};
#[cfg(feature = "no_closure")]
let typ = v.type_name();
v.try_cast::<T>().ok_or_else(|| typ)
})
.collect(),
#[cfg(not(feature = "no_closure"))]
Union::Shared(cell, _, _) => {
#[cfg(not(feature = "sync"))]
let value = cell.borrow();
#[cfg(feature = "sync")]
let value = cell.read().unwrap();
match value.0 {
Union::Array(ref a, _, _) => {
a.iter()
.map(|v| {
#[cfg(not(feature = "no_closure"))]
let typ = if v.is_shared() {
// Avoid panics/deadlocks with shared values
"<shared>"
} else {
v.type_name()
};
#[cfg(feature = "no_closure")]
let typ = v.type_name();
v.read_lock::<T>().ok_or_else(|| typ).map(|v| v.clone())
})
.collect()
}
_ => Err((*value).type_name()),
}
}
_ => Err(self.type_name()),
}
}
}
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
impl Dynamic { impl Dynamic {
/// Create a [`Dynamic`] from a [`Map`]. /// Create a [`Dynamic`] from a [`Map`].

View File

@ -10,13 +10,13 @@ use std::{borrow::Cow, iter::Extend};
/// Keep a number of entries inline (since [`Dynamic`] is usually small enough). /// Keep a number of entries inline (since [`Dynamic`] is usually small enough).
const SCOPE_ENTRIES_INLINED: usize = 8; const SCOPE_ENTRIES_INLINED: usize = 8;
/// Type containing information about the current scope. /// Type containing information about the current scope. Useful for keeping state between
/// Useful for keeping state between [`Engine`][crate::Engine] evaluation runs. /// [`Engine`][crate::Engine] evaluation runs.
/// ///
/// # Thread Safety /// # Thread Safety
/// ///
/// Currently, [`Scope`] is neither [`Send`] nor [`Sync`]. /// Currently, [`Scope`] is neither [`Send`] nor [`Sync`]. Turn on the `sync` feature to make it
/// Turn on the `sync` feature to make it [`Send`] `+` [`Sync`]. /// [`Send`] `+` [`Sync`].
/// ///
/// # Example /// # Example
/// ///
@ -39,17 +39,19 @@ const SCOPE_ENTRIES_INLINED: usize = 8;
/// # } /// # }
/// ``` /// ```
/// ///
/// When searching for entries, newly-added entries are found before similarly-named but older entries, /// When searching for entries, newly-added entries are found before similarly-named but older
/// allowing for automatic _shadowing_. /// entries, allowing for automatic _shadowing_.
// //
// # Implementation Notes // # Implementation Notes
// //
// [`Scope`] is implemented as two [`Vec`]'s of exactly the same length. Variables data (name, type, etc.) // [`Scope`] is implemented as two [`Vec`]'s of exactly the same length. Variables data (name,
// is manually split into two equal-length arrays. That's because variable names take up the most space, // type, etc.) is manually split into two equal-length arrays. That's because variable names take
// with [`Cow<str>`][Cow] being four words long, but in the vast majority of cases the name is NOT used to // up the most space, with [`Cow<str>`][Cow] being four words long, but in the vast majority of
// look up a variable. Variable lookup is usually via direct indexing, by-passing the name altogether. // cases the name is NOT used to look up a variable. Variable lookup is usually via direct
// indexing, by-passing the name altogether.
// //
// Since [`Dynamic`] is reasonably small, packing it tightly improves cache locality when variables are accessed. // Since [`Dynamic`] is reasonably small, packing it tightly improves cache locality when variables
// are accessed.
#[derive(Debug, Clone, Hash, Default)] #[derive(Debug, Clone, Hash, Default)]
pub struct Scope<'a> { pub struct Scope<'a> {
/// Current value of the entry. /// Current value of the entry.
@ -493,7 +495,7 @@ impl<'a> Scope<'a> {
pub(crate) fn get_mut_by_index(&mut self, index: usize) -> &mut Dynamic { pub(crate) fn get_mut_by_index(&mut self, index: usize) -> &mut Dynamic {
self.values.get_mut(index).expect("valid index") self.values.get_mut(index).expect("valid index")
} }
/// Update the access type of an entry in the [`Scope`]. /// Add an alias to an entry in the [`Scope`].
/// ///
/// # Panics /// # Panics
/// ///

View File

@ -1,9 +1,5 @@
#![cfg(not(feature = "no_index"))] #![cfg(not(feature = "no_index"))]
use rhai::{Array, Engine, EvalAltResult, INT}; use rhai::{Array, Dynamic, Engine, EvalAltResult, INT};
fn convert_to_vec<T: Clone + 'static>(array: Array) -> Vec<T> {
array.into_iter().map(|v| v.clone_cast::<T>()).collect()
}
#[test] #[test]
fn test_arrays() -> Result<(), Box<EvalAltResult>> { fn test_arrays() -> Result<(), Box<EvalAltResult>> {
@ -27,30 +23,42 @@ fn test_arrays() -> Result<(), Box<EvalAltResult>> {
assert!(engine.eval::<bool>("let y = [1, 2, 3]; 2 in y")?); assert!(engine.eval::<bool>("let y = [1, 2, 3]; 2 in y")?);
assert_eq!(engine.eval::<INT>("let y = [1, 2, 3]; y += 4; y[3]")?, 4); assert_eq!(engine.eval::<INT>("let y = [1, 2, 3]; y += 4; y[3]")?, 4);
assert_eq!( assert_eq!(
convert_to_vec::<INT>(engine.eval("let y = [1, 2, 3]; y[1] += 4; y")?), engine
.eval::<Dynamic>("let y = [1, 2, 3]; y[1] += 4; y")?
.into_typed_array::<INT>()?,
[1, 6, 3] [1, 6, 3]
); );
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
{ {
assert_eq!( assert_eq!(
convert_to_vec::<INT>(engine.eval("let y = [1, 2, 3]; y.push(4); y")?), engine
.eval::<Dynamic>("let y = [1, 2, 3]; y.push(4); y")?
.into_typed_array::<INT>()?,
[1, 2, 3, 4] [1, 2, 3, 4]
); );
assert_eq!( assert_eq!(
convert_to_vec::<INT>(engine.eval("let y = [1, 2, 3]; y.insert(0, 4); y")?), engine
.eval::<Dynamic>("let y = [1, 2, 3]; y.insert(0, 4); y")?
.into_typed_array::<INT>()?,
[4, 1, 2, 3] [4, 1, 2, 3]
); );
assert_eq!( assert_eq!(
convert_to_vec::<INT>(engine.eval("let y = [1, 2, 3]; y.insert(999, 4); y")?), engine
.eval::<Dynamic>("let y = [1, 2, 3]; y.insert(999, 4); y")?
.into_typed_array::<INT>()?,
[1, 2, 3, 4] [1, 2, 3, 4]
); );
assert_eq!( assert_eq!(
convert_to_vec::<INT>(engine.eval("let y = [1, 2, 3]; y.insert(-2, 4); y")?), engine
.eval::<Dynamic>("let y = [1, 2, 3]; y.insert(-2, 4); y")?
.into_typed_array::<INT>()?,
[1, 4, 2, 3] [1, 4, 2, 3]
); );
assert_eq!( assert_eq!(
convert_to_vec::<INT>(engine.eval("let y = [1, 2, 3]; y.insert(-999, 4); y")?), engine
.eval::<Dynamic>("let y = [1, 2, 3]; y.insert(-999, 4); y")?
.into_typed_array::<INT>()?,
[4, 1, 2, 3] [4, 1, 2, 3]
); );
assert_eq!( assert_eq!(
@ -67,43 +75,49 @@ fn test_arrays() -> Result<(), Box<EvalAltResult>> {
); );
assert_eq!( assert_eq!(
convert_to_vec::<INT>(engine.eval( engine
" .eval::<Dynamic>(
let x = [2, 9]; "
x.insert(-1, 1); let x = [2, 9];
x.insert(999, 3); x.insert(-1, 1);
x.insert(-9, 99); x.insert(999, 3);
x.insert(-9, 99);
let r = x.remove(2); let r = x.remove(2);
let y = [4, 5]; let y = [4, 5];
x.append(y); x.append(y);
x x
" "
)?), )?
.into_typed_array::<INT>()?,
[99, 2, 9, 3, 4, 5] [99, 2, 9, 3, 4, 5]
); );
} }
assert_eq!( assert_eq!(
convert_to_vec::<INT>(engine.eval( engine
" .eval::<Dynamic>(
let x = [1, 2, 3]; "
x += [4, 5]; let x = [1, 2, 3];
x x += [4, 5];
" x
)?), "
)?
.into_typed_array::<INT>()?,
[1, 2, 3, 4, 5] [1, 2, 3, 4, 5]
); );
assert_eq!( assert_eq!(
convert_to_vec::<INT>(engine.eval( engine
" .eval::<Dynamic>(
let x = [1, 2, 3]; "
let y = [4, 5]; let x = [1, 2, 3];
x + y let y = [4, 5];
" x + y
)?), "
)?
.into_typed_array::<INT>()?,
[1, 2, 3, 4, 5] [1, 2, 3, 4, 5]
); );
@ -172,42 +186,50 @@ fn test_arrays_map_reduce() -> Result<(), Box<EvalAltResult>> {
assert_eq!(engine.eval::<INT>("([1].map(|x| x + 41))[0]")?, 42); assert_eq!(engine.eval::<INT>("([1].map(|x| x + 41))[0]")?, 42);
assert_eq!( assert_eq!(
convert_to_vec::<INT>(engine.eval( engine
" .eval::<Dynamic>(
let x = [1, 2, 3]; "
x.filter(|v| v > 2) let x = [1, 2, 3];
" x.filter(|v| v > 2)
)?), "
)?
.into_typed_array::<INT>()?,
[3] [3]
); );
assert_eq!( assert_eq!(
convert_to_vec::<INT>(engine.eval( engine
" .eval::<Dynamic>(
let x = [1, 2, 3]; "
x.filter(|v, i| v > i) let x = [1, 2, 3];
" x.filter(|v, i| v > i)
)?), "
)?
.into_typed_array::<INT>()?,
[1, 2, 3] [1, 2, 3]
); );
assert_eq!( assert_eq!(
convert_to_vec::<INT>(engine.eval( engine
" .eval::<Dynamic>(
let x = [1, 2, 3]; "
x.map(|v| v * 2) let x = [1, 2, 3];
" x.map(|v| v * 2)
)?), "
)?
.into_typed_array::<INT>()?,
[2, 4, 6] [2, 4, 6]
); );
assert_eq!( assert_eq!(
convert_to_vec::<INT>(engine.eval( engine
" .eval::<Dynamic>(
let x = [1, 2, 3]; "
x.map(|v, i| v * i) let x = [1, 2, 3];
" x.map(|v, i| v * i)
)?), "
)?
.into_typed_array::<INT>()?,
[0, 2, 6] [0, 2, 6]
); );