Struct rhai::Scope [−][src]
pub struct Scope<'a>(_);Expand description
Type containing information about the current scope.
Useful for keeping state between Engine evaluation runs.
Example
use rhai::{Engine, Scope};
let engine = Engine::new();
let mut my_scope = Scope::new();
my_scope.push("z", 40_i64);
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);When searching for entries, newly-added entries are found before similarly-named but older entries, allowing for automatic shadowing.
Currently, Scope is neither Send nor Sync. Turn on the sync feature to make it Send + Sync.
Implementations
Create a new Scope.
Examples
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);Empty the Scope.
Examples
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());Get the number of entries inside the Scope.
Examples
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);Is the Scope empty?
Examples
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());Add (push) a new entry to the Scope.
Examples
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);Add (push) a new Dynamic entry to the Scope.
Examples
use rhai::{Dynamic, Scope};
let mut my_scope = Scope::new();
my_scope.push_dynamic("x", Dynamic::from(42_i64));
assert_eq!(my_scope.get_value::<i64>("x").unwrap(), 42);Add (push) a new constant to the Scope.
Constants are immutable and cannot be assigned to. Their values never change. Constants propagation is a technique used to optimize an AST.
However, in order to be used for optimization, constants must be in one of the recognized types:
INT (default to i64, i32 if only_i32), f64, String, char and bool.
Examples
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);Add (push) a new constant with a Dynamic value to the Scope.
Constants are immutable and cannot be assigned to. Their values never change. Constants propagation is a technique used to optimize an AST.
However, in order to be used for optimization, the Dynamic value must be in one of the
recognized types:
INT (default to i64, i32 if only_i32), f64, String, char and bool.
Examples
use rhai::{Dynamic, Scope};
let mut my_scope = Scope::new();
my_scope.push_constant_dynamic("x", Dynamic::from(42_i64));
assert_eq!(my_scope.get_value::<i64>("x").unwrap(), 42);Truncate (rewind) the Scope to a previous size.
Examples
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());Does the scope contain the entry?
Examples
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"));Get the value of an entry in the Scope, starting from the last.
Examples
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);Update the value of the named entry. 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.
Panics
Panics when trying to update the value of a constant.
Examples
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);Get an iterator to entries in the Scope.
Examples
use rhai::{Dynamic, Scope};
let mut my_scope = Scope::new();
my_scope.push("x", 42_i64);
my_scope.push_constant("foo", "hello".to_string());
let mut iter = my_scope.iter();
let (name, constant, value) = iter.next().unwrap();
assert_eq!(name, "x");
assert!(!constant);
assert_eq!(value.cast::<i64>(), 42);
let (name, constant, value) = iter.next().unwrap();
assert_eq!(name, "foo");
assert!(constant);
assert_eq!(value.cast::<String>(), "hello");Trait Implementations
Extends a collection with the contents of an iterator. Read more
extend_one)Extends a collection with exactly one element.
extend_one)Reserves capacity in a collection for the given number of additional elements. Read more
Auto Trait Implementations
impl<'a> !RefUnwindSafe for Scope<'a>
impl<'a> !UnwindSafe for Scope<'a>
Blanket Implementations
Mutably borrows from an owned value. Read more