Struct quad_compat_rhai::Scope[][src]

pub struct Scope<'a> { /* fields omitted */ }
Expand description

Type containing information about the current scope. Useful for keeping state between Engine evaluation runs.

Thread Safety

Currently, Scope is neither Send nor Sync. Turn on the sync feature to make it Send + Sync.

Example

use quad_compat_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").expect("x should exist"), 41);
assert_eq!(my_scope.get_value::<i64>("z").expect("z should exist"), 0);

When searching for entries, newly-added entries are found before similarly-named but older entries, allowing for automatic shadowing.

Implementations

Create a new Scope.

Example
use quad_compat_rhai::Scope;

let mut my_scope = Scope::new();

my_scope.push("x", 42_i64);
assert_eq!(my_scope.get_value::<i64>("x").expect("x should exist"), 42);

Empty the Scope.

Example
use quad_compat_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.

Example
use quad_compat_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?

Example
use quad_compat_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.

Example
use quad_compat_rhai::Scope;

let mut my_scope = Scope::new();

my_scope.push("x", 42_i64);
assert_eq!(my_scope.get_value::<i64>("x").expect("x should exist"), 42);

Add (push) a new Dynamic entry to the Scope.

Example
use quad_compat_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").expect("x should exist"), 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.

Example
use quad_compat_rhai::Scope;

let mut my_scope = Scope::new();

my_scope.push_constant("x", 42_i64);
assert_eq!(my_scope.get_value::<i64>("x").expect("x should exist"), 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.

Example
use quad_compat_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").expect("x should exist"), 42);

Truncate (rewind) the Scope to a previous size.

Example
use quad_compat_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?

Example
use quad_compat_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.

Example
use quad_compat_rhai::Scope;

let mut my_scope = Scope::new();

my_scope.push("x", 42_i64);
assert_eq!(my_scope.get_value::<i64>("x").expect("x should exist"), 42);

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 quad_compat_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);

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 quad_compat_rhai::Scope;

let mut my_scope = Scope::new();

my_scope.set_or_push("x", 42_i64);
assert_eq!(my_scope.get_value::<i64>("x").expect("x should exist"), 42);
assert_eq!(my_scope.len(), 1);

my_scope.set_or_push("x", 0_i64);
assert_eq!(my_scope.get_value::<i64>("x").expect("x should exist"), 0);
assert_eq!(my_scope.len(), 1);

my_scope.set_or_push("y", 123_i64);
assert_eq!(my_scope.get_value::<i64>("y").expect("y should exist"), 123);
assert_eq!(my_scope.len(), 2);

Update the value of the named entry in the Scope.

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.

Example
use quad_compat_rhai::Scope;

let mut my_scope = Scope::new();

my_scope.push("x", 42_i64);
assert_eq!(my_scope.get_value::<i64>("x").expect("x should exist"), 42);

my_scope.set_value("x", 0_i64);
assert_eq!(my_scope.get_value::<i64>("x").expect("x should exist"), 0);

Get a mutable reference to an entry in the Scope.

If the entry by the specified name is not found, of if it is read-only, None is returned.

Example
use quad_compat_rhai::Scope;

let mut my_scope = Scope::new();

my_scope.push("x", 42_i64);
assert_eq!(my_scope.get_value::<i64>("x").expect("x should exist"), 42);

let ptr = my_scope.get_mut("x").expect("x should exist");
*ptr = 123_i64.into();

assert_eq!(my_scope.get_value::<i64>("x").expect("x should exist"), 123);

my_scope.push_constant("Z", 1_i64);
assert!(my_scope.get_mut("Z").is_none());

Clone the Scope, keeping only the last instances of each variable name. Shadowed variables are omitted in the copy.

Get an iterator to entries in the Scope. Shared values are flatten-cloned.

Example
use quad_compat_rhai::{Dynamic, Scope};

let mut my_scope = Scope::new();

my_scope.push("x", 42_i64);
my_scope.push_constant("foo", "hello");

let mut iter = my_scope.iter();

let (name, is_constant, value) = iter.next().expect("value should exist");
assert_eq!(name, "x");
assert!(!is_constant);
assert_eq!(value.cast::<i64>(), 42);

let (name, is_constant, value) = iter.next().expect("value should exist");
assert_eq!(name, "foo");
assert!(is_constant);
assert_eq!(value.cast::<String>(), "hello");

Get an iterator to entries in the Scope. Shared values are not expanded.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Extends a collection with the contents of an iterator. Read more

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

Extends a collection with the contents of an iterator. Read more

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

Creates a value from an iterator. Read more

Creates a value from an iterator. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.