rhai/src/scope.rs

124 lines
3.9 KiB
Rust
Raw Normal View History

2020-03-03 08:20:20 +01:00
use crate::any::{Any, Dynamic};
2020-03-07 03:15:42 +01:00
use std::borrow::Cow;
2020-03-03 08:20:20 +01:00
/// A type containing information about current scope.
/// Useful for keeping state between `Engine` runs
///
2020-03-04 15:00:01 +01:00
/// # Example
///
2020-03-03 08:20:20 +01:00
/// ```rust
/// use rhai::{Engine, Scope};
///
/// let mut engine = Engine::new();
/// let mut my_scope = Scope::new();
///
/// assert!(engine.eval_with_scope::<()>(&mut my_scope, "let x = 5;").is_ok());
/// assert_eq!(engine.eval_with_scope::<i64>(&mut my_scope, "x + 1").unwrap(), 6);
/// ```
///
2020-03-04 15:00:01 +01:00
/// When searching for variables, newly-added variables are found before similarly-named but older variables,
/// allowing for automatic _shadowing_ of variables.
2020-03-07 03:15:42 +01:00
pub struct Scope<'a>(Vec<(Cow<'a, str>, Dynamic)>);
2020-03-03 08:20:20 +01:00
2020-03-07 03:15:42 +01:00
impl<'a> Scope<'a> {
2020-03-03 08:20:20 +01:00
/// Create a new Scope.
pub fn new() -> Self {
Self(Vec::new())
}
/// Empty the Scope.
pub fn clear(&mut self) {
self.0.clear();
}
/// Get the number of variables inside the Scope.
pub fn len(&self) -> usize {
self.0.len()
}
/// Add (push) a new variable to the Scope.
2020-03-07 03:15:42 +01:00
pub fn push<K: Into<Cow<'a, str>>, T: Any>(&mut self, key: K, value: T) {
self.0.push((key.into(), Box::new(value)));
2020-03-03 08:20:20 +01:00
}
/// Add (push) a new variable to the Scope.
2020-03-07 03:15:42 +01:00
pub(crate) fn push_dynamic<K: Into<Cow<'a, str>>>(&mut self, key: K, value: Dynamic) {
self.0.push((key.into(), value));
2020-03-03 08:20:20 +01:00
}
/// Remove (pop) the last variable from the Scope.
pub fn pop(&mut self) -> Option<(String, Dynamic)> {
2020-03-07 03:15:42 +01:00
self.0.pop().map(|(key, value)| (key.to_string(), value))
2020-03-03 08:20:20 +01:00
}
/// Truncate (rewind) the Scope to a previous size.
pub fn rewind(&mut self, size: usize) {
self.0.truncate(size);
}
/// Find a variable in the Scope, starting from the last.
2020-03-07 03:15:42 +01:00
pub fn get(&self, key: &str) -> Option<(usize, &str, Dynamic)> {
2020-03-03 08:20:20 +01:00
self.0
.iter()
.enumerate()
2020-03-04 15:00:01 +01:00
.rev() // Always search a Scope in reverse order
2020-03-04 16:06:05 +01:00
.find(|(_, (name, _))| name == key)
2020-03-07 03:15:42 +01:00
.map(|(i, (name, value))| (i, name.as_ref(), value.clone()))
2020-03-03 08:20:20 +01:00
}
/// Get the value of a variable in the Scope, starting from the last.
pub fn get_value<T: Any + Clone>(&self, key: &str) -> Option<T> {
self.0
.iter()
.enumerate()
2020-03-04 15:00:01 +01:00
.rev() // Always search a Scope in reverse order
2020-03-04 16:06:05 +01:00
.find(|(_, (name, _))| name == key)
.and_then(|(_, (_, value))| value.downcast_ref::<T>())
.map(|value| value.clone())
2020-03-03 08:20:20 +01:00
}
/// Get a mutable reference to a variable in the Scope.
pub(crate) fn get_mut(&mut self, key: &str, index: usize) -> &mut Dynamic {
let entry = self.0.get_mut(index).expect("invalid index in Scope");
2020-03-04 16:06:05 +01:00
assert_eq!(entry.0, key, "incorrect key at Scope entry");
2020-03-03 08:20:20 +01:00
&mut entry.1
}
/// Get a mutable reference to a variable in the Scope and downcast it to a specific type
pub(crate) fn get_mut_by_type<T: Any + Clone>(&mut self, key: &str, index: usize) -> &mut T {
self.get_mut(key, index)
.downcast_mut::<T>()
.expect("wrong type cast")
}
2020-03-03 08:20:20 +01:00
/// Get an iterator to variables in the Scope.
2020-03-04 15:00:01 +01:00
pub fn iter(&self) -> impl Iterator<Item = (&str, &Dynamic)> {
self.0
.iter()
.rev() // Always search a Scope in reverse order
2020-03-07 03:15:42 +01:00
.map(|(key, value)| (key.as_ref(), value))
2020-03-03 08:20:20 +01:00
}
2020-03-07 03:15:42 +01:00
/*
2020-03-03 08:20:20 +01:00
/// Get a mutable iterator to variables in the Scope.
2020-03-04 15:00:01 +01:00
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&str, &mut Dynamic)> {
self.0
.iter_mut()
.rev() // Always search a Scope in reverse order
2020-03-07 03:15:42 +01:00
.map(|(key, value)| (key.as_ref(), value))
2020-03-03 08:20:20 +01:00
}
2020-03-07 03:15:42 +01:00
*/
2020-03-03 08:20:20 +01:00
}
2020-03-07 03:15:42 +01:00
impl<'a, K> std::iter::Extend<(K, Dynamic)> for Scope<'a>
where
K: Into<Cow<'a, str>>,
{
fn extend<T: IntoIterator<Item = (K, Dynamic)>>(&mut self, iter: T) {
self.0
.extend(iter.into_iter().map(|(key, value)| (key.into(), value)));
2020-03-03 08:20:20 +01:00
}
}