[−][src]Struct rhai::Scope
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 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.
Implementations
impl<'a> Scope<'a>
[src]
pub fn new() -> Self
[src]
Create a new Scope
.
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);
pub fn clear(&mut self) -> &mut SelfⓘNotable traits for &'_ mut W
impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, F> Future for &'_ mut F where
F: Unpin + Future + ?Sized, type Output = <F as Future>::Output;impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
[src]
Notable traits for &'_ mut W
impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, F> Future for &'_ mut F where
F: Unpin + Future + ?Sized, type Output = <F as Future>::Output;impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
Empty the Scope
.
Example
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());
pub fn len(&self) -> usize
[src]
Get the number of entries inside the Scope
.
Example
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);
pub fn is_empty(&self) -> bool
[src]
Is the Scope
empty?
Example
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());
pub fn push(
&mut self,
name: impl Into<Cow<'a, str>>,
value: impl Variant + Clone
) -> &mut SelfⓘNotable traits for &'_ mut W
impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, F> Future for &'_ mut F where
F: Unpin + Future + ?Sized, type Output = <F as Future>::Output;impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
[src]
&mut self,
name: impl Into<Cow<'a, str>>,
value: impl Variant + Clone
) -> &mut Selfⓘ
Notable traits for &'_ mut W
impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, F> Future for &'_ mut F where
F: Unpin + Future + ?Sized, type Output = <F as Future>::Output;impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
Add (push) a new entry to the Scope
.
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);
pub fn push_dynamic(
&mut self,
name: impl Into<Cow<'a, str>>,
value: Dynamic
) -> &mut SelfⓘNotable traits for &'_ mut W
impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, F> Future for &'_ mut F where
F: Unpin + Future + ?Sized, type Output = <F as Future>::Output;impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
[src]
&mut self,
name: impl Into<Cow<'a, str>>,
value: Dynamic
) -> &mut Selfⓘ
Notable traits for &'_ mut W
impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, F> Future for &'_ mut F where
F: Unpin + Future + ?Sized, type Output = <F as Future>::Output;impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
Add (push) a new Dynamic
entry to the Scope
.
Example
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);
pub fn push_constant(
&mut self,
name: impl Into<Cow<'a, str>>,
value: impl Variant + Clone
) -> &mut SelfⓘNotable traits for &'_ mut W
impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, F> Future for &'_ mut F where
F: Unpin + Future + ?Sized, type Output = <F as Future>::Output;impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
[src]
&mut self,
name: impl Into<Cow<'a, str>>,
value: impl Variant + Clone
) -> &mut Selfⓘ
Notable traits for &'_ mut W
impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, F> Future for &'_ mut F where
F: Unpin + Future + ?Sized, type Output = <F as Future>::Output;impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
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 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);
pub fn push_constant_dynamic(
&mut self,
name: impl Into<Cow<'a, str>>,
value: Dynamic
) -> &mut SelfⓘNotable traits for &'_ mut W
impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, F> Future for &'_ mut F where
F: Unpin + Future + ?Sized, type Output = <F as Future>::Output;impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
[src]
&mut self,
name: impl Into<Cow<'a, str>>,
value: Dynamic
) -> &mut Selfⓘ
Notable traits for &'_ mut W
impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, F> Future for &'_ mut F where
F: Unpin + Future + ?Sized, type Output = <F as Future>::Output;impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
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 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);
pub fn rewind(&mut self, size: usize) -> &mut SelfⓘNotable traits for &'_ mut W
impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, F> Future for &'_ mut F where
F: Unpin + Future + ?Sized, type Output = <F as Future>::Output;impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
[src]
Notable traits for &'_ mut W
impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, F> Future for &'_ mut F where
F: Unpin + Future + ?Sized, type Output = <F as Future>::Output;impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
Truncate (rewind) the Scope
to a previous size.
Example
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());
pub fn contains(&self, name: &str) -> bool
[src]
Does the Scope
contain the entry?
Example
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"));
pub fn get_value<T: Variant + Clone>(&self, name: &str) -> Option<T>
[src]
Get the value of an entry in the Scope
, starting from the last.
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);
pub fn set_value(
&mut self,
name: &'a str,
value: impl Variant + Clone
) -> &mut SelfⓘNotable traits for &'_ mut W
impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, F> Future for &'_ mut F where
F: Unpin + Future + ?Sized, type Output = <F as Future>::Output;impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
[src]
&mut self,
name: &'a str,
value: impl Variant + Clone
) -> &mut Selfⓘ
Notable traits for &'_ mut W
impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, F> Future for &'_ mut F where
F: Unpin + Future + ?Sized, type Output = <F as Future>::Output;impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
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 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);
pub fn iter(&self) -> impl Iterator<Item = (&str, bool, Dynamic)>
[src]
Get an iterator to entries in the Scope
.
Shared values are flatten-cloned.
Example
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");
pub fn iter_raw<'x: 'a>(
&'x self
) -> impl Iterator<Item = (&'a str, bool, &'x Dynamic)> + 'x
[src]
&'x self
) -> impl Iterator<Item = (&'a str, bool, &'x Dynamic)> + 'x
Get an iterator to entries in the Scope
.
Shared values are not expanded.
Trait Implementations
impl<'a> Clone for Scope<'a>
[src]
impl<'a> Debug for Scope<'a>
[src]
impl<'_> Default for Scope<'_>
[src]
impl<'a, K: Into<Cow<'a, str>>> Extend<(K, EntryType, Dynamic)> for Scope<'a>
[src]
pub fn extend<T: IntoIterator<Item = (K, EntryType, Dynamic)>>(
&mut self,
iter: T
)
[src]
&mut self,
iter: T
)
pub fn extend_one(&mut self, item: A)
[src]
pub fn extend_reserve(&mut self, additional: usize)
[src]
Auto Trait Implementations
impl<'a> !RefUnwindSafe for Scope<'a>
impl<'a> !Send for Scope<'a>
impl<'a> !Sync for Scope<'a>
impl<'a> Unpin for Scope<'a>
impl<'a> !UnwindSafe for Scope<'a>
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow(&self) -> &TⓘNotable traits for &'_ mut W
impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, F> Future for &'_ mut F where
F: Unpin + Future + ?Sized, type Output = <F as Future>::Output;impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
[src]
Notable traits for &'_ mut W
impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, F> Future for &'_ mut F where
F: Unpin + Future + ?Sized, type Output = <F as Future>::Output;impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut TⓘNotable traits for &'_ mut W
impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, F> Future for &'_ mut F where
F: Unpin + Future + ?Sized, type Output = <F as Future>::Output;impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
[src]
Notable traits for &'_ mut W
impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, F> Future for &'_ mut F where
F: Unpin + Future + ?Sized, type Output = <F as Future>::Output;impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,