pub trait Memory {
// Required methods
fn slice_at(
&self,
addr: Address,
len: usize,
) -> Result<&[Value], SliceError>;
fn slice_at_mut(
&mut self,
addr: Address,
len: usize,
) -> Result<&mut [Value], SliceError>;
fn at_mut(&mut self, addr: Address) -> Result<&mut Value, StackError>;
// Provided method
fn array_at<const N: usize>(
&self,
addr: Address,
) -> Result<[&Value; N], SliceError>
where Self: Sized { ... }
}Expand description
Memory access.
Required Methods§
Sourcefn slice_at(&self, addr: Address, len: usize) -> Result<&[Value], SliceError>
fn slice_at(&self, addr: Address, len: usize) -> Result<&[Value], SliceError>
Get the slice at the given address with the given length.
§Examples
use rune::runtime::{Address, Memory, Output, VmError};
fn sum(memory: &mut dyn Memory, addr: Address, args: usize, out: Output) -> Result<(), VmError> {
let mut number = 0;
for value in memory.slice_at(addr, args)? {
number += value.as_integer::<i64>()?;
}
memory.store(out, number)?;
Ok(())
}Sourcefn slice_at_mut(
&mut self,
addr: Address,
len: usize,
) -> Result<&mut [Value], SliceError>
fn slice_at_mut( &mut self, addr: Address, len: usize, ) -> Result<&mut [Value], SliceError>
Access the given slice mutably.
§Examples
use rune::runtime::{Address, Memory, Output, Value, VmError};
fn drop_values(memory: &mut dyn Memory, addr: Address, args: usize, out: Output) -> Result<(), VmError> {
for value in memory.slice_at_mut(addr, args)? {
*value = Value::empty();
}
memory.store(out, ())?;
Ok(())
}Sourcefn at_mut(&mut self, addr: Address) -> Result<&mut Value, StackError>
fn at_mut(&mut self, addr: Address) -> Result<&mut Value, StackError>
Get a value mutable at the given index from the stack bottom.
§Examples
use rune::runtime::{Address, Memory, Output, VmError};
fn add_one(memory: &mut dyn Memory, addr: Address, args: usize, out: Output) -> Result<(), VmError> {
let mut value = memory.at_mut(addr)?;
let number = value.as_integer::<i64>()?;
*value = rune::to_value(number + 1)?;
memory.store(out, ())?;
Ok(())
}Provided Methods§
Implementations§
Source§impl dyn Memory + '_
impl dyn Memory + '_
Sourcepub fn store<O>(
&mut self,
out: Output,
o: O,
) -> Result<(), StoreError<O::Error>>where
O: IntoOutput,
pub fn store<O>(
&mut self,
out: Output,
o: O,
) -> Result<(), StoreError<O::Error>>where
O: IntoOutput,
Write output using the provided IntoOutput implementation onto the
stack.
The IntoOutput trait primarily allows for deferring a computation
since it’s implemented by FnOnce. However, you must take care that
any side effects calling a function may have are executed outside of the
call to store. Like if the function would error.
§Examples
use rune::runtime::{Output, Memory, ToValue, VmError, Address};
use rune::vm_try;
fn sum(memory: &mut dyn Memory, addr: Address, args: usize, out: Output) -> Result<(), VmError> {
let mut number = 0;
for value in memory.slice_at(addr, args)? {
number += value.as_integer::<i64>()?;
}
memory.store(out, number)?;
Ok(())
}