FabricMem

Struct FabricMem 

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

Safe handle to fabric memory via FBMU host functions.

Created by FabricMem::hello, which performs the FBMU HELLO handshake to establish a memory data-plane session. Once created, the handle can be used for byte-addressable reads and writes within the arena.

§Examples

use grafos_std::mem::FabricMem;

let mem = FabricMem::hello()?;

// Write and read back
mem.write(0, b"test data")?;
let data = mem.read(0, 9)?;
assert_eq!(&data, b"test data");

// Check arena capacity
let size = mem.arena_size()?;
assert_eq!(size, 4096);

Implementations§

Source§

impl FabricMem

Source

pub fn hello() -> Result<FabricMem>

Perform the FBMU HELLO handshake and return a memory handle.

This establishes the memory data-plane session with the host. On WASM targets, this calls the real fbmu_hello host import. On native targets, it initializes the mock state.

§Errors
Source

pub fn write(&self, offset: u64, data: &[u8]) -> Result<()>

Write data at the given byte offset in the arena.

§Errors

Returns a FabricError if the host reports a write failure.

Source

pub fn read(&self, offset: u64, len: u32) -> Result<Vec<u8>>

Read len bytes starting at offset from the arena.

Returns a Vec<u8> containing the data read. The returned vector may be shorter than len if the host returned fewer bytes.

§Errors

Returns a FabricError if the host reports a read failure.

Source

pub fn arena_size(&self) -> Result<u64>

Query the total arena size in bytes.

§Errors

Returns FabricError::IoError if the host returns a negative value.

Source§

impl FabricMem

Source

pub fn write_struct<T: Serialize>(&self, offset: u64, val: &T) -> Result<usize>

Serialize val and write it to fabric memory at offset.

Uses postcard encoding, which is compact and no_std-compatible. Returns the number of bytes written.

§Errors
Source

pub fn read_struct<T: DeserializeOwned>( &self, offset: u64, max_len: u32, ) -> Result<T>

Read and deserialize a T from fabric memory at offset.

Reads up to max_len bytes from the arena and attempts to deserialize them as T using postcard.

§Errors

Trait Implementations§

Source§

impl Debug for FabricMem

Source§

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

Formats the value using the given formatter. 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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, 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.