Skip to main content

Globals

Struct Globals 

Source
pub struct Globals { /* private fields */ }
Expand description

Storage for the static items declared by a Unit.

A unit assigns every static item it declares a zero-indexed slot. The values living in those slots are not stored in the unit itself, they are stored here and handed to a virtual machine when it is called. This means a single compiled unit can back any number of independent states.

Every slot keeps track of whether it has been initialized. A slot which has an initializer is lazily initialized the first time a script reads it, but a caller can also assign a value up front, in which case the initializer is never evaluated.

The storage is a cheaply cloned handle. Cloning it does not copy the slots, it produces a second handle to the same storage, which is how a caller can keep reading and writing static items while a virtual machine using them is running.

§Examples

use rune::{Context, Diagnostics, Source, Sources, Vm};
use rune::runtime::Globals;
use rune::sync::Arc;

let context = Context::with_default_modules()?;
let runtime = Arc::try_new(context.runtime()?)?;

let mut sources = Sources::new();
sources.insert(Source::memory(r#"
static COUNTER = 0;

pub fn main() {
    COUNTER += 1;
}
"#)?)?;

let mut diagnostics = Diagnostics::new();

let unit = rune::prepare(&mut sources)
    .with_context(&context)
    .with_diagnostics(&mut diagnostics)
    .build()?;

let unit = Arc::try_new(unit)?;
let globals = Globals::new(unit.clone())?;

let mut vm = Vm::new(runtime, unit).with_globals(globals.clone());
vm.call(["main"], ())?;
vm.call(["main"], ())?;

let counter = globals.get(["COUNTER"])?.expect("COUNTER to be initialized");
assert_eq!(rune::from_value::<i64>(counter)?, 2);

Implementations§

Source§

impl Globals

Source

pub const fn empty() -> Self

Construct an empty storage which has no slots.

This is what a Vm is constructed with. Reading or writing a static through it always errors, so a unit which declares statics needs real storage through Vm::with_globals.

Source

pub fn new(unit: Arc<Unit>) -> Result<Self>

Construct storage for all the statics declared by the given unit, with every slot uninitialized.

Source

pub fn is_configured(&self) -> bool

Test if this storage has been configured for a unit.

Note that this is not the same as is_empty, since a unit which declares no statics gets configured storage with no slots in it.

Source

pub fn len(&self) -> usize

The number of slots in this storage.

Source

pub fn is_empty(&self) -> bool

Test if this storage has no slots.

Source

pub fn is_same(&self, other: &Self) -> bool

Test if two handles refer to the same storage.

Source

pub fn is_same_unit(&self, unit: &Arc<Unit>) -> bool

Test if this storage was constructed for the given unit.

Source

pub fn slot(&self, name: impl ToTypeHash) -> Result<usize, GlobalsError>

Look up the slot assigned to the given static item.

Source

pub fn get(&self, name: impl ToTypeHash) -> Result<Option<Value>, GlobalsError>

Read the value of a static item, if it has been initialized.

Note that this does not evaluate the initializer of an uninitialized static, since doing so requires a runtime. A static which has an initializer but has never been read by a script therefore reads as None here.

Source

pub fn set( &self, name: impl ToTypeHash, value: Value, ) -> Result<(), GlobalsError>

Write the value of a static item.

Source

pub fn clear(&self, name: impl ToTypeHash) -> Result<(), GlobalsError>

Reset a static item back to being uninitialized.

The next script which reads it will evaluate its initializer again, if it has one.

Source

pub fn get_at(&self, slot: usize) -> Option<Value>

Read the value in the given slot, if it has been initialized.

Source

pub fn set_at(&self, slot: usize, value: Value) -> Result<(), GlobalsError>

Write the value in the given slot.

Source

pub fn clear_at(&self, slot: usize) -> Result<(), GlobalsError>

Reset the given slot back to being uninitialized.

Source

pub fn is_initialized(&self, slot: usize) -> bool

Test if the given slot has been initialized.

Trait Implementations§

Source§

impl Clone for Globals

Source§

fn clone(&self) -> Globals

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Globals

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Globals

Source§

fn default() -> Globals

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

impl TryClone for Globals

Source§

fn try_clone(&self) -> Result<Self>

Try to clone the current value, raising an allocation error if it’s unsuccessful.
Source§

fn try_clone_from(&mut self, source: &Self) -> Result<(), Error>

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> TryToOwned for T
where T: TryClone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn try_to_owned(&self) -> Result<T, Error>

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

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more