Skip to main content

StorageBlob

Struct StorageBlob 

Source
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 u64
  • derive_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

Source

pub const fn new(namespace: Namespace) -> Self

Creates a new blob with the specified namespace.

Source

pub fn len_slot(&self) -> [u8; 32]

Returns the storage slot for the length counter.

Source

pub fn slot_for_chunk(&self, index: u64) -> [u8; 32]

Returns the storage slot for a chunk at the given index.

Source

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.

Source

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.

Source

pub fn clear_in<B: StorageBackend>(&self, backend: &mut B) -> Result<()>

Clears the blob by setting length to 0 (with explicit backend).

Source

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.

Source

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.

Source

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

Writes raw bytes to storage (production, uses HostStorage).

Source

pub fn read(&self) -> Result<Vec<u8>>

Reads raw bytes from storage (production, uses HostStorage).

Source

pub fn put<T: BytesCodec>(&self, value: &T) -> Result<()>

Writes a typed value to storage (production, uses HostStorage).

Source

pub fn get<T: BytesCodec>(&self) -> Result<T>

Reads a typed value from storage (production, uses HostStorage).

Source

pub fn clear(&self) -> Result<()>

Clears the blob (production, uses HostStorage).

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> Same for T

Source§

type Output = T

Should always be Self
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.