rune::runtime

Trait Memory

Source
pub trait Memory {
    // Required methods
    fn slice_at(
        &self,
        addr: InstAddress,
        len: usize,
    ) -> Result<&[Value], SliceError>;
    fn slice_at_mut(
        &mut self,
        addr: InstAddress,
        len: usize,
    ) -> Result<&mut [Value], SliceError>;
    fn at_mut(&mut self, addr: InstAddress) -> Result<&mut Value, StackError>;

    // Provided method
    fn array_at<const N: usize>(
        &self,
        addr: InstAddress,
    ) -> Result<[&Value; N], SliceError>
       where Self: Sized { ... }
}
Expand description

Memory access.

Required Methods§

Source

fn slice_at( &self, addr: InstAddress, len: usize, ) -> Result<&[Value], SliceError>

Get the slice at the given address with the given length.

§Examples
use rune::vm_try;
use rune::runtime::{Output, Memory, ToValue, VmResult, InstAddress};

fn sum(stack: &mut dyn Memory, addr: InstAddress, args: usize, out: Output) -> VmResult<()> {
    let mut number = 0;

    for value in vm_try!(stack.slice_at(addr, args)) {
        number += vm_try!(value.as_integer::<i64>());
    }

    out.store(stack, number);
    VmResult::Ok(())
}
Source

fn slice_at_mut( &mut self, addr: InstAddress, len: usize, ) -> Result<&mut [Value], SliceError>

Access the given slice mutably.

§Examples
use rune::vm_try;
use rune::runtime::{Output, Memory, InstAddress, Value, VmResult};

fn drop_values(stack: &mut dyn Memory, addr: InstAddress, args: usize, out: Output) -> VmResult<()> {
    for value in vm_try!(stack.slice_at_mut(addr, args)) {
        *value = Value::empty();
    }

    out.store(stack, ());
    VmResult::Ok(())
}
Source

fn at_mut(&mut self, addr: InstAddress) -> Result<&mut Value, StackError>

Get a value mutable at the given index from the stack bottom.

§Examples
use rune::vm_try;
use rune::Module;
use rune::runtime::{Output, Memory, VmResult, InstAddress};

fn add_one(stack: &mut dyn Memory, addr: InstAddress, args: usize, out: Output) -> VmResult<()> {
    let mut value = vm_try!(stack.at_mut(addr));
    let number = vm_try!(value.as_integer::<i64>());
    *value = vm_try!(rune::to_value(number + 1));
    out.store(stack, ());
    VmResult::Ok(())
}

Provided Methods§

Source

fn array_at<const N: usize>( &self, addr: InstAddress, ) -> Result<[&Value; N], SliceError>
where Self: Sized,

Get the slice at the given address with the given static length.

Implementations on Foreign Types§

Source§

impl<M> Memory for &mut M
where M: Memory + ?Sized,

Source§

fn slice_at( &self, addr: InstAddress, len: usize, ) -> Result<&[Value], SliceError>

Source§

fn slice_at_mut( &mut self, addr: InstAddress, len: usize, ) -> Result<&mut [Value], SliceError>

Source§

fn at_mut(&mut self, addr: InstAddress) -> Result<&mut Value, StackError>

Source§

impl<const N: usize> Memory for [Value; N]

Source§

fn slice_at( &self, addr: InstAddress, len: usize, ) -> Result<&[Value], SliceError>

Source§

fn slice_at_mut( &mut self, addr: InstAddress, len: usize, ) -> Result<&mut [Value], SliceError>

Source§

fn at_mut(&mut self, addr: InstAddress) -> Result<&mut Value, StackError>

Implementors§