rhai/src/scope.rs

601 lines
18 KiB
Rust
Raw Normal View History

2020-11-20 09:52:28 +01:00
//! Module that defines the [`Scope`] type representing a function call-stack scope.
2020-03-08 12:54:02 +01:00
use crate::dynamic::{AccessMode, Variant};
2021-03-29 05:36:02 +02:00
use crate::{Dynamic, Identifier, StaticVec};
2021-04-17 09:15:54 +02:00
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
use std::{borrow::Cow, iter::Extend};
2020-03-03 08:20:20 +01:00
2021-03-12 06:26:47 +01:00
/// Keep a number of entries inline (since [`Dynamic`] is usually small enough).
2021-06-28 12:06:05 +02:00
const SCOPE_ENTRIES_INLINED: usize = 8;
2021-03-12 06:26:47 +01:00
2020-05-15 15:40:54 +02:00
/// Type containing information about the current scope.
2020-11-20 09:52:28 +01:00
/// Useful for keeping state between [`Engine`][crate::Engine] evaluation runs.
2020-03-03 08:20:20 +01:00
///
2020-10-27 04:30:38 +01:00
/// # Thread Safety
///
2020-11-20 09:52:28 +01:00
/// Currently, [`Scope`] is neither [`Send`] nor [`Sync`].
2020-11-25 02:36:06 +01:00
/// Turn on the `sync` feature to make it [`Send`] `+` [`Sync`].
2020-10-27 04:30:38 +01:00
///
2020-03-04 15:00:01 +01:00
/// # Example
///
2020-03-19 06:52:10 +01:00
/// ```
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
2020-03-03 08:20:20 +01:00
/// use rhai::{Engine, Scope};
///
/// let engine = Engine::new();
2020-03-03 08:20:20 +01:00
/// let mut my_scope = Scope::new();
///
2020-04-05 17:43:40 +02:00
/// my_scope.push("z", 40_i64);
2020-03-09 14:09:53 +01:00
///
2020-04-05 17:43:40 +02:00
/// engine.eval_with_scope::<()>(&mut my_scope, "let x = z + 1; z = 0;")?;
///
/// assert_eq!(engine.eval_with_scope::<i64>(&mut my_scope, "x + 1")?, 42);
///
/// assert_eq!(my_scope.get_value::<i64>("x").unwrap(), 41);
/// assert_eq!(my_scope.get_value::<i64>("z").unwrap(), 0);
2020-03-09 14:09:53 +01:00
/// # Ok(())
/// # }
2020-03-03 08:20:20 +01:00
/// ```
///
2020-03-25 04:27:18 +01:00
/// When searching for entries, newly-added entries are found before similarly-named but older entries,
/// allowing for automatic _shadowing_.
2020-11-01 15:46:46 +01:00
//
// # Implementation Notes
//
2020-12-08 15:47:38 +01:00
// [`Scope`] is implemented as two [`Vec`]'s of exactly the same length. Variables data (name, type, etc.)
2020-12-24 02:42:03 +01:00
// is manually split into two equal-length arrays. That's because variable names take up the most space,
// with [`Cow<str>`][Cow] being four words long, but in the vast majority of cases the name is NOT used to
2020-12-26 06:05:57 +01:00
// look up a variable. Variable lookup is usually via direct indexing, by-passing the name altogether.
2020-11-01 15:46:46 +01:00
//
2020-11-20 09:52:28 +01:00
// Since [`Dynamic`] is reasonably small, packing it tightly improves cache locality when variables are accessed.
#[derive(Debug, Clone, Hash, Default)]
2020-11-01 15:46:46 +01:00
pub struct Scope<'a> {
/// Current value of the entry.
2021-06-28 12:06:05 +02:00
values: smallvec::SmallVec<[Dynamic; SCOPE_ENTRIES_INLINED]>,
2020-12-24 02:42:03 +01:00
/// (Name, aliases) of the entry.
2021-06-29 17:22:54 +02:00
names: smallvec::SmallVec<
[(Cow<'a, str>, Option<Box<StaticVec<Identifier>>>); SCOPE_ENTRIES_INLINED],
>,
2020-11-01 15:46:46 +01:00
}
2020-03-03 08:20:20 +01:00
2021-02-18 16:35:22 +01:00
impl<'a> IntoIterator for Scope<'a> {
type Item = (Cow<'a, str>, Dynamic);
type IntoIter = Box<dyn Iterator<Item = Self::Item> + 'a>;
#[inline]
2021-02-18 16:35:22 +01:00
fn into_iter(self) -> Self::IntoIter {
Box::new(
self.values
.into_iter()
.zip(self.names.into_iter())
.map(|(value, (name, _))| (name, value)),
)
}
}
2020-03-07 03:15:42 +01:00
impl<'a> Scope<'a> {
2020-11-20 09:52:28 +01:00
/// Create a new [`Scope`].
2020-04-05 17:43:40 +02:00
///
2020-10-27 04:30:38 +01:00
/// # Example
2020-04-05 17:43:40 +02:00
///
/// ```
/// use rhai::Scope;
///
/// let mut my_scope = Scope::new();
///
/// my_scope.push("x", 42_i64);
/// assert_eq!(my_scope.get_value::<i64>("x").unwrap(), 42);
/// ```
2020-10-08 16:25:50 +02:00
#[inline(always)]
2021-06-12 16:47:43 +02:00
#[must_use]
2020-03-03 08:20:20 +01:00
pub fn new() -> Self {
Default::default()
2020-03-03 08:20:20 +01:00
}
2020-11-20 09:52:28 +01:00
/// Empty the [`Scope`].
2020-04-05 17:43:40 +02:00
///
2020-10-27 04:30:38 +01:00
/// # Example
2020-04-05 17:43:40 +02:00
///
/// ```
/// use rhai::Scope;
///
/// let mut my_scope = Scope::new();
///
/// my_scope.push("x", 42_i64);
/// assert!(my_scope.contains("x"));
/// assert_eq!(my_scope.len(), 1);
/// assert!(!my_scope.is_empty());
///
/// my_scope.clear();
/// assert!(!my_scope.contains("x"));
/// assert_eq!(my_scope.len(), 0);
/// assert!(my_scope.is_empty());
/// ```
2020-10-08 16:25:50 +02:00
#[inline(always)]
2020-07-12 05:46:53 +02:00
pub fn clear(&mut self) -> &mut Self {
2020-11-01 15:46:46 +01:00
self.names.clear();
self.values.clear();
2020-07-12 05:46:53 +02:00
self
2020-03-03 08:20:20 +01:00
}
2020-11-20 09:52:28 +01:00
/// Get the number of entries inside the [`Scope`].
2020-04-05 17:43:40 +02:00
///
2020-10-27 04:30:38 +01:00
/// # Example
2020-04-05 17:43:40 +02:00
///
/// ```
/// use rhai::Scope;
///
/// let mut my_scope = Scope::new();
/// assert_eq!(my_scope.len(), 0);
///
/// my_scope.push("x", 42_i64);
/// assert_eq!(my_scope.len(), 1);
/// ```
2020-10-08 16:25:50 +02:00
#[inline(always)]
2021-06-12 16:47:43 +02:00
#[must_use]
2020-03-03 08:20:20 +01:00
pub fn len(&self) -> usize {
2020-11-01 15:46:46 +01:00
self.values.len()
2020-03-03 08:20:20 +01:00
}
2020-11-20 09:52:28 +01:00
/// Is the [`Scope`] empty?
2020-04-05 17:43:40 +02:00
///
2020-10-27 04:30:38 +01:00
/// # Example
2020-04-05 17:43:40 +02:00
///
/// ```
/// use rhai::Scope;
///
/// let mut my_scope = Scope::new();
/// assert!(my_scope.is_empty());
///
/// my_scope.push("x", 42_i64);
/// assert!(!my_scope.is_empty());
/// ```
2020-10-08 16:25:50 +02:00
#[inline(always)]
2021-06-12 16:47:43 +02:00
#[must_use]
2020-03-24 09:57:35 +01:00
pub fn is_empty(&self) -> bool {
2021-06-28 12:06:05 +02:00
self.values.is_empty()
2020-03-24 09:57:35 +01:00
}
2020-11-20 09:52:28 +01:00
/// Add (push) a new entry to the [`Scope`].
2020-04-05 17:43:40 +02:00
///
2020-10-27 04:30:38 +01:00
/// # Example
2020-04-05 17:43:40 +02:00
///
/// ```
/// use rhai::Scope;
///
/// let mut my_scope = Scope::new();
///
/// my_scope.push("x", 42_i64);
/// assert_eq!(my_scope.get_value::<i64>("x").unwrap(), 42);
/// ```
2020-10-08 16:25:50 +02:00
#[inline(always)]
2020-11-03 14:50:14 +01:00
pub fn push(
2020-07-12 05:46:53 +02:00
&mut self,
2020-11-03 14:50:14 +01:00
name: impl Into<Cow<'a, str>>,
value: impl Variant + Clone,
2020-07-12 05:46:53 +02:00
) -> &mut Self {
self.push_dynamic_value(name, AccessMode::ReadWrite, Dynamic::from(value))
2020-03-25 04:27:18 +01:00
}
2020-11-20 09:52:28 +01:00
/// Add (push) a new [`Dynamic`] entry to the [`Scope`].
2020-04-05 17:43:40 +02:00
///
2020-10-27 04:30:38 +01:00
/// # Example
2020-04-05 17:43:40 +02:00
///
/// ```
2020-04-12 17:00:06 +02:00
/// use rhai::{Dynamic, Scope};
2020-04-05 17:43:40 +02:00
///
/// let mut my_scope = Scope::new();
///
2020-04-12 17:00:06 +02:00
/// my_scope.push_dynamic("x", Dynamic::from(42_i64));
2020-04-05 17:43:40 +02:00
/// assert_eq!(my_scope.get_value::<i64>("x").unwrap(), 42);
/// ```
2020-10-08 16:25:50 +02:00
#[inline(always)]
2020-11-03 14:50:14 +01:00
pub fn push_dynamic(&mut self, name: impl Into<Cow<'a, str>>, value: Dynamic) -> &mut Self {
self.push_dynamic_value(name, value.access_mode(), value)
2020-03-03 08:20:20 +01:00
}
2020-11-20 09:52:28 +01:00
/// Add (push) a new constant to the [`Scope`].
///
/// Constants are immutable and cannot be assigned to. Their values never change.
2020-11-20 09:52:28 +01:00
/// Constants propagation is a technique used to optimize an [`AST`][crate::AST].
2020-04-05 17:43:40 +02:00
///
2020-10-27 04:30:38 +01:00
/// # Example
2020-04-05 17:43:40 +02:00
///
/// ```
/// use rhai::Scope;
///
/// let mut my_scope = Scope::new();
///
/// my_scope.push_constant("x", 42_i64);
/// assert_eq!(my_scope.get_value::<i64>("x").unwrap(), 42);
/// ```
2020-10-08 16:25:50 +02:00
#[inline(always)]
2020-11-03 14:50:14 +01:00
pub fn push_constant(
2020-07-12 05:46:53 +02:00
&mut self,
2020-11-03 14:50:14 +01:00
name: impl Into<Cow<'a, str>>,
value: impl Variant + Clone,
2020-07-12 05:46:53 +02:00
) -> &mut Self {
self.push_dynamic_value(name, AccessMode::ReadOnly, Dynamic::from(value))
2020-03-25 04:27:18 +01:00
}
2020-11-20 09:52:28 +01:00
/// Add (push) a new constant with a [`Dynamic`] value to the Scope.
2020-03-25 04:27:18 +01:00
///
/// Constants are immutable and cannot be assigned to. Their values never change.
2020-11-20 09:52:28 +01:00
/// Constants propagation is a technique used to optimize an [`AST`][crate::AST].
2020-04-05 17:43:40 +02:00
///
2020-10-27 04:30:38 +01:00
/// # Example
2020-04-05 17:43:40 +02:00
///
/// ```
2020-04-12 17:00:06 +02:00
/// use rhai::{Dynamic, Scope};
2020-04-05 17:43:40 +02:00
///
/// let mut my_scope = Scope::new();
///
2020-04-12 17:00:06 +02:00
/// my_scope.push_constant_dynamic("x", Dynamic::from(42_i64));
2020-04-05 17:43:40 +02:00
/// assert_eq!(my_scope.get_value::<i64>("x").unwrap(), 42);
/// ```
2020-10-08 16:25:50 +02:00
#[inline(always)]
2020-11-03 14:50:14 +01:00
pub fn push_constant_dynamic(
2020-07-12 05:46:53 +02:00
&mut self,
2020-11-03 14:50:14 +01:00
name: impl Into<Cow<'a, str>>,
2020-07-12 05:46:53 +02:00
value: Dynamic,
) -> &mut Self {
self.push_dynamic_value(name, AccessMode::ReadOnly, value)
2020-03-13 11:12:41 +01:00
}
2020-11-20 09:52:28 +01:00
/// Add (push) a new entry with a [`Dynamic`] value to the [`Scope`].
#[inline]
2020-11-03 14:50:14 +01:00
pub(crate) fn push_dynamic_value(
2020-03-13 11:12:41 +01:00
&mut self,
2020-11-03 14:50:14 +01:00
name: impl Into<Cow<'a, str>>,
access: AccessMode,
2020-12-08 15:47:38 +01:00
mut value: Dynamic,
2020-07-12 05:46:53 +02:00
) -> &mut Self {
2021-06-29 17:22:54 +02:00
self.names.push((name.into(), Default::default()));
value.set_access_mode(access);
2021-07-24 08:11:16 +02:00
self.values.push(value);
2020-07-12 05:46:53 +02:00
self
2020-03-03 08:20:20 +01:00
}
2020-11-20 09:52:28 +01:00
/// Truncate (rewind) the [`Scope`] to a previous size.
2020-04-05 17:43:40 +02:00
///
2020-10-27 04:30:38 +01:00
/// # Example
2020-04-05 17:43:40 +02:00
///
/// ```
/// use rhai::Scope;
///
/// let mut my_scope = Scope::new();
///
/// my_scope.push("x", 42_i64);
/// my_scope.push("y", 123_i64);
/// assert!(my_scope.contains("x"));
/// assert!(my_scope.contains("y"));
/// assert_eq!(my_scope.len(), 2);
///
/// my_scope.rewind(1);
/// assert!(my_scope.contains("x"));
/// assert!(!my_scope.contains("y"));
/// assert_eq!(my_scope.len(), 1);
///
/// my_scope.rewind(0);
/// assert!(!my_scope.contains("x"));
/// assert!(!my_scope.contains("y"));
/// assert_eq!(my_scope.len(), 0);
/// assert!(my_scope.is_empty());
/// ```
2020-10-08 16:25:50 +02:00
#[inline(always)]
2020-07-12 05:46:53 +02:00
pub fn rewind(&mut self, size: usize) -> &mut Self {
2020-11-01 15:46:46 +01:00
self.names.truncate(size);
self.values.truncate(size);
2020-07-12 05:46:53 +02:00
self
2020-03-03 08:20:20 +01:00
}
2020-11-20 09:52:28 +01:00
/// Does the [`Scope`] contain the entry?
2020-04-05 17:43:40 +02:00
///
2020-10-27 04:30:38 +01:00
/// # Example
2020-04-05 17:43:40 +02:00
///
/// ```
/// use rhai::Scope;
///
/// let mut my_scope = Scope::new();
///
/// my_scope.push("x", 42_i64);
/// assert!(my_scope.contains("x"));
/// assert!(!my_scope.contains("y"));
/// ```
#[inline]
2021-06-12 16:47:43 +02:00
#[must_use]
2020-04-05 17:43:40 +02:00
pub fn contains(&self, name: &str) -> bool {
2020-11-01 15:46:46 +01:00
self.names
2020-03-19 13:55:49 +01:00
.iter()
.rev() // Always search a Scope in reverse order
2020-11-01 15:46:46 +01:00
.any(|(key, _)| name == key.as_ref())
2020-03-19 13:55:49 +01:00
}
2020-11-20 09:52:28 +01:00
/// Find an entry in the [`Scope`], starting from the last.
#[inline]
2021-06-12 16:47:43 +02:00
#[must_use]
pub(crate) fn get_index(&self, name: &str) -> Option<(usize, AccessMode)> {
2020-11-01 15:46:46 +01:00
self.names
2020-03-03 08:20:20 +01:00
.iter()
.enumerate()
2020-03-04 15:00:01 +01:00
.rev() // Always search a Scope in reverse order
2020-11-01 15:46:46 +01:00
.find_map(|(index, (key, _))| {
if name == key.as_ref() {
Some((index, self.values[index].access_mode()))
} else {
None
2020-05-04 13:36:58 +02:00
}
})
}
2020-11-20 09:52:28 +01:00
/// Get the value of an entry in the [`Scope`], starting from the last.
2020-04-05 17:43:40 +02:00
///
2020-10-27 04:30:38 +01:00
/// # Example
2020-04-05 17:43:40 +02:00
///
/// ```
/// use rhai::Scope;
///
/// let mut my_scope = Scope::new();
///
/// my_scope.push("x", 42_i64);
/// assert_eq!(my_scope.get_value::<i64>("x").unwrap(), 42);
/// ```
#[inline]
2021-06-12 16:47:43 +02:00
#[must_use]
2020-04-12 17:00:06 +02:00
pub fn get_value<T: Variant + Clone>(&self, name: &str) -> Option<T> {
2020-11-01 15:46:46 +01:00
self.names
.iter()
.enumerate()
.rev()
.find(|(_, (key, _))| name == key.as_ref())
.and_then(|(index, _)| self.values[index].flatten_clone().try_cast())
2020-03-03 08:20:20 +01:00
}
2021-08-13 16:47:03 +02:00
/// Check if the named entry in the [`Scope`] is constant.
///
/// Search starts backwards from the last, stopping at the first entry matching the specified name.
///
/// Returns [`None`] if no entry matching the specified name is found.
///
/// # Example
///
/// ```
/// use rhai::Scope;
///
/// let mut my_scope = Scope::new();
///
/// my_scope.push_constant("x", 42_i64);
/// assert_eq!(my_scope.is_constant("x"), Some(true));
/// assert_eq!(my_scope.is_constant("y"), None);
/// ```
#[inline]
pub fn is_constant(&self, name: &str) -> Option<bool> {
self.get_index(name).and_then(|(_, access)| match access {
AccessMode::ReadWrite => None,
AccessMode::ReadOnly => Some(true),
})
}
2021-08-14 08:56:15 +02:00
/// Update the value of the named entry in the [`Scope`] if it already exists and is not constant.
/// Push a new entry with the value into the [`Scope`] if the name doesn't exist or if the
/// existing entry is constant.
///
/// Search starts backwards from the last, and only the first entry matching the specified name is updated.
///
/// # Example
///
/// ```
/// use rhai::Scope;
///
/// let mut my_scope = Scope::new();
///
/// my_scope.set_or_push("x", 42_i64);
/// assert_eq!(my_scope.get_value::<i64>("x").unwrap(), 42);
/// assert_eq!(my_scope.len(), 1);
///
/// my_scope.set_or_push("x", 0_i64);
/// assert_eq!(my_scope.get_value::<i64>("x").unwrap(), 0);
/// assert_eq!(my_scope.len(), 1);
///
/// my_scope.set_or_push("y", 123_i64);
/// assert_eq!(my_scope.get_value::<i64>("y").unwrap(), 123);
/// assert_eq!(my_scope.len(), 2);
/// ```
#[inline]
pub fn set_or_push(
&mut self,
name: impl AsRef<str> + Into<Cow<'a, str>>,
value: impl Variant + Clone,
) -> &mut Self {
match self.get_index(name.as_ref()) {
None | Some((_, AccessMode::ReadOnly)) => {
self.push(name, value);
}
Some((index, AccessMode::ReadWrite)) => {
let value_ref = self
.values
.get_mut(index)
.expect("never fails because the index is returned by `get_index`");
*value_ref = Dynamic::from(value);
}
}
self
}
2020-11-20 09:52:28 +01:00
/// Update the value of the named entry in the [`Scope`].
///
2020-04-05 17:43:40 +02:00
/// Search starts backwards from the last, and only the first entry matching the specified name is updated.
/// If no entry matching the specified name is found, a new one is added.
2020-04-05 13:17:48 +02:00
///
/// # Panics
///
/// Panics when trying to update the value of a constant.
2020-04-05 17:43:40 +02:00
///
2020-10-27 04:30:38 +01:00
/// # Example
2020-04-05 17:43:40 +02:00
///
/// ```
/// use rhai::Scope;
///
/// let mut my_scope = Scope::new();
///
/// my_scope.push("x", 42_i64);
/// assert_eq!(my_scope.get_value::<i64>("x").unwrap(), 42);
///
/// my_scope.set_value("x", 0_i64);
/// assert_eq!(my_scope.get_value::<i64>("x").unwrap(), 0);
/// ```
#[inline]
pub fn set_value(
&mut self,
name: impl AsRef<str> + Into<Cow<'a, str>>,
value: impl Variant + Clone,
) -> &mut Self {
match self.get_index(name.as_ref()) {
2020-07-12 05:46:53 +02:00
None => {
self.push(name, value);
}
Some((_, AccessMode::ReadOnly)) => panic!("variable {} is constant", name.as_ref()),
Some((index, AccessMode::ReadWrite)) => {
2021-05-22 13:14:24 +02:00
let value_ref = self
.values
.get_mut(index)
.expect("never fails because the index is returned by `get_index`");
*value_ref = Dynamic::from(value);
2020-04-27 16:49:09 +02:00
}
2020-04-05 13:17:48 +02:00
}
2020-07-12 05:46:53 +02:00
self
2020-04-05 13:17:48 +02:00
}
2020-11-20 09:52:28 +01:00
/// Get a mutable reference to an entry in the [`Scope`].
2020-12-26 06:05:57 +01:00
///
/// If the entry by the specified name is not found, of if it is read-only,
/// [`None`] is returned.
///
/// # Example
///
/// ```
/// use rhai::Scope;
///
/// let mut my_scope = Scope::new();
///
/// my_scope.push("x", 42_i64);
/// assert_eq!(my_scope.get_value::<i64>("x").unwrap(), 42);
///
/// let ptr = my_scope.get_mut("x").unwrap();
/// *ptr = 123_i64.into();
///
/// assert_eq!(my_scope.get_value::<i64>("x").unwrap(), 123);
/// ```
#[inline]
2021-06-12 16:47:43 +02:00
#[must_use]
2020-12-26 06:05:57 +01:00
pub fn get_mut(&mut self, name: &str) -> Option<&mut Dynamic> {
self.get_index(name)
.and_then(move |(index, access)| match access {
AccessMode::ReadWrite => Some(self.get_mut_by_index(index)),
AccessMode::ReadOnly => None,
})
}
/// Get a mutable reference to an entry in the [`Scope`] based on the index.
2021-05-22 13:14:24 +02:00
///
/// # Panics
///
/// Panics if the index is out of bounds.
2020-10-08 16:25:50 +02:00
#[inline(always)]
2021-06-12 16:47:43 +02:00
#[must_use]
2020-12-26 06:05:57 +01:00
pub(crate) fn get_mut_by_index(&mut self, index: usize) -> &mut Dynamic {
2021-05-22 13:14:24 +02:00
self.values
.get_mut(index)
.expect("never fails unless the index is out of bounds")
2020-03-03 08:20:20 +01:00
}
2020-11-20 09:52:28 +01:00
/// Update the access type of an entry in the [`Scope`].
2021-05-22 13:14:24 +02:00
///
/// # Panics
///
/// Panics if the index is out of bounds.
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "no_module"))]
#[inline]
2021-05-22 13:14:24 +02:00
pub(crate) fn add_entry_alias(&mut self, index: usize, alias: Identifier) -> &mut Self {
2021-06-29 17:22:54 +02:00
let (_, aliases) = self
2021-05-22 13:14:24 +02:00
.names
.get_mut(index)
.expect("never fails unless the index is out of bounds");
2021-06-29 17:22:54 +02:00
match aliases {
None => {
let mut list = StaticVec::new();
list.push(alias);
*aliases = Some(list.into());
}
Some(aliases) if !aliases.iter().any(|a| a == &alias) => aliases.push(alias),
Some(_) => (),
2020-11-09 07:38:33 +01:00
}
2020-07-12 05:46:53 +02:00
self
2020-05-08 10:49:24 +02:00
}
2020-11-20 09:52:28 +01:00
/// Clone the [`Scope`], keeping only the last instances of each variable name.
/// Shadowed variables are omitted in the copy.
#[inline]
2021-06-12 16:47:43 +02:00
#[must_use]
2020-10-12 11:00:58 +02:00
pub(crate) fn clone_visible(&self) -> Self {
let mut entries = Self::new();
2020-11-01 15:46:46 +01:00
self.names
.iter()
.enumerate()
.rev()
2021-03-17 06:30:47 +01:00
.for_each(|(i, (name, alias))| {
2020-11-01 15:46:46 +01:00
if !entries.names.iter().any(|(key, _)| key == name) {
entries.names.push((name.clone(), alias.clone()));
2021-03-17 06:30:47 +01:00
entries.values.push(self.values[i].clone());
2020-11-01 15:46:46 +01:00
}
});
2020-11-01 15:46:46 +01:00
entries
}
2020-11-20 09:52:28 +01:00
/// Get an iterator to entries in the [`Scope`].
#[inline]
2020-11-16 09:28:04 +01:00
#[allow(dead_code)]
2020-12-11 05:57:07 +01:00
pub(crate) fn into_iter(
self,
2021-03-29 05:36:02 +02:00
) -> impl Iterator<Item = (Cow<'a, str>, Dynamic, Vec<Identifier>)> {
2020-11-01 15:46:46 +01:00
self.names
.into_iter()
2020-12-08 15:47:38 +01:00
.zip(self.values.into_iter())
2021-03-12 06:26:47 +01:00
.map(|((name, alias), value)| {
(name, value, alias.map(|a| a.to_vec()).unwrap_or_default())
})
2020-03-03 08:20:20 +01:00
}
2020-11-20 09:52:28 +01:00
/// Get an iterator to entries in the [`Scope`].
2020-11-01 15:46:46 +01:00
/// Shared values are flatten-cloned.
2020-06-24 16:45:34 +02:00
///
2020-10-27 04:30:38 +01:00
/// # Example
2020-06-24 16:45:34 +02:00
///
/// ```
/// use rhai::{Dynamic, Scope};
///
/// let mut my_scope = Scope::new();
///
/// my_scope.push("x", 42_i64);
2021-01-02 16:30:10 +01:00
/// my_scope.push_constant("foo", "hello");
2020-06-24 16:45:34 +02:00
///
/// let mut iter = my_scope.iter();
///
2021-02-09 07:22:55 +01:00
/// let (name, is_constant, value) = iter.next().unwrap();
2020-06-24 16:45:34 +02:00
/// assert_eq!(name, "x");
2021-02-09 07:22:55 +01:00
/// assert!(!is_constant);
2020-08-03 06:10:20 +02:00
/// assert_eq!(value.cast::<i64>(), 42);
2020-06-24 16:45:34 +02:00
///
2021-02-09 07:22:55 +01:00
/// let (name, is_constant, value) = iter.next().unwrap();
2020-06-24 16:45:34 +02:00
/// assert_eq!(name, "foo");
2021-02-09 07:22:55 +01:00
/// assert!(is_constant);
2020-08-03 06:10:20 +02:00
/// assert_eq!(value.cast::<String>(), "hello");
2020-06-24 16:45:34 +02:00
/// ```
2020-10-08 16:25:50 +02:00
#[inline(always)]
2020-10-09 07:23:44 +02:00
pub fn iter(&self) -> impl Iterator<Item = (&str, bool, Dynamic)> {
2020-08-03 06:10:20 +02:00
self.iter_raw()
2020-10-09 07:23:44 +02:00
.map(|(name, constant, value)| (name, constant, value.flatten_clone()))
2020-08-03 06:10:20 +02:00
}
2020-11-20 09:52:28 +01:00
/// Get an iterator to entries in the [`Scope`].
2020-08-03 06:10:20 +02:00
/// Shared values are not expanded.
#[inline]
2021-02-09 07:22:55 +01:00
pub fn iter_raw(&self) -> impl Iterator<Item = (&str, bool, &Dynamic)> {
2020-11-01 15:46:46 +01:00
self.names
.iter()
2020-12-08 15:47:38 +01:00
.zip(self.values.iter())
2020-12-08 16:09:12 +01:00
.map(|((name, _), value)| (name.as_ref(), value.is_read_only(), value))
2020-06-24 16:45:34 +02:00
}
2020-03-03 08:20:20 +01:00
}
2021-04-17 09:15:54 +02:00
impl<'a, K: Into<Cow<'a, str>>> Extend<(K, Dynamic)> for Scope<'a> {
2020-10-08 16:25:50 +02:00
#[inline(always)]
2020-12-08 15:47:38 +01:00
fn extend<T: IntoIterator<Item = (K, Dynamic)>>(&mut self, iter: T) {
iter.into_iter().for_each(|(name, value)| {
2021-06-29 17:22:54 +02:00
self.names.push((name.into(), Default::default()));
2020-11-01 15:46:46 +01:00
self.values.push(value);
});
}
}