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
impl FabricMem
Sourcepub fn hello() -> Result<FabricMem>
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
FabricError::Disconnectedif the host connection is unavailable.- Other
FabricErrorvariants based on the host status code.
Sourcepub fn write(&self, offset: u64, data: &[u8]) -> Result<()>
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.
Sourcepub fn read(&self, offset: u64, len: u32) -> Result<Vec<u8>>
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.
Sourcepub fn arena_size(&self) -> Result<u64>
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
impl FabricMem
Sourcepub fn write_struct<T: Serialize>(&self, offset: u64, val: &T) -> Result<usize>
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
FabricError::IoErrorif serialization fails (e.g. the type cannot be serialized by postcard).- Any
FabricErrorfrom the underlyingFabricMem::writecall.
Sourcepub fn read_struct<T: DeserializeOwned>(
&self,
offset: u64,
max_len: u32,
) -> Result<T>
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
FabricError::IoErrorif deserialization fails (e.g. the data does not match the expected type layout).- Any
FabricErrorfrom the underlyingFabricMem::readcall.