pub struct StorageBlob { /* private fields */ }Expand description
A persistent variable-length byte storage.
StorageBlob stores arbitrary-length byte arrays by chunking them into
32-byte segments. Useful for storing strings, serialized structs, or large data.
§Storage Layout
derive_slot(namespace, ["blob:len"])- Length in bytes as u64derive_slot(namespace, ["blob:chunk", i])- Chunk i (32 bytes)
§Example
const NS_METADATA: Namespace = Namespace([1u8; 32]);
let metadata = StorageBlob::new(NS_METADATA);
// Write raw bytes
metadata.write(b"Hello, TruthLinked!")?;
// Read raw bytes
let data = metadata.read()?;
// Write typed value
let config = Config { version: 1, enabled: true };
metadata.put(&config)?;
// Read typed value
let config: Config = metadata.get()?;Implementations§
Source§impl StorageBlob
impl StorageBlob
Sourcepub const fn new(namespace: Namespace) -> Self
pub const fn new(namespace: Namespace) -> Self
Creates a new blob with the specified namespace.
Sourcepub fn slot_for_chunk(&self, index: u64) -> [u8; 32]
pub fn slot_for_chunk(&self, index: u64) -> [u8; 32]
Returns the storage slot for a chunk at the given index.
Sourcepub fn write_in<B: StorageBackend>(
&self,
backend: &mut B,
data: &[u8],
) -> Result<()>
pub fn write_in<B: StorageBackend>( &self, backend: &mut B, data: &[u8], ) -> Result<()>
Writes raw bytes to storage (with explicit backend).
Data is chunked into 32-byte segments. The length is stored separately.
Sourcepub fn read_in<B: StorageBackend>(&self, backend: &B) -> Result<Vec<u8>>
pub fn read_in<B: StorageBackend>(&self, backend: &B) -> Result<Vec<u8>>
Reads raw bytes from storage (with explicit backend).
Reconstructs the original byte array from stored chunks.
Sourcepub fn clear_in<B: StorageBackend>(&self, backend: &mut B) -> Result<()>
pub fn clear_in<B: StorageBackend>(&self, backend: &mut B) -> Result<()>
Clears the blob by setting length to 0 (with explicit backend).
Sourcepub fn put_in<B: StorageBackend, T: BytesCodec>(
&self,
backend: &mut B,
value: &T,
) -> Result<()>
pub fn put_in<B: StorageBackend, T: BytesCodec>( &self, backend: &mut B, value: &T, ) -> Result<()>
Writes a typed value to storage (with explicit backend).
The value is encoded using BytesCodec before storage.
Sourcepub fn get_in<B: StorageBackend, T: BytesCodec>(&self, backend: &B) -> Result<T>
pub fn get_in<B: StorageBackend, T: BytesCodec>(&self, backend: &B) -> Result<T>
Reads a typed value from storage (with explicit backend).
The bytes are decoded using BytesCodec.
Sourcepub fn write(&self, data: &[u8]) -> Result<()>
pub fn write(&self, data: &[u8]) -> Result<()>
Writes raw bytes to storage (production, uses HostStorage).
Sourcepub fn read(&self) -> Result<Vec<u8>>
pub fn read(&self) -> Result<Vec<u8>>
Reads raw bytes from storage (production, uses HostStorage).
Sourcepub fn put<T: BytesCodec>(&self, value: &T) -> Result<()>
pub fn put<T: BytesCodec>(&self, value: &T) -> Result<()>
Writes a typed value to storage (production, uses HostStorage).
Sourcepub fn get<T: BytesCodec>(&self) -> Result<T>
pub fn get<T: BytesCodec>(&self) -> Result<T>
Reads a typed value from storage (production, uses HostStorage).