pub trait StorageItem {
    type Key: Eq;
    type Error;

    // Required methods
    fn serialize_into(&self, buffer: &mut [u8]) -> Result<usize, Self::Error>;
    fn deserialize_from(buffer: &[u8]) -> Result<Self, Self::Error>
       where Self: Sized;
    fn key(&self) -> Self::Key;

    // Provided method
    fn deserialize_key_only(buffer: &[u8]) -> Result<Self::Key, Self::Error>
       where Self: Sized { ... }
}
Expand description

A way of serializing and deserializing items in the storage.

Serialized items must not be 0 bytes and may not be longer than u16::MAX. Items must also fit within a page (together with the bits of overhead added in the storage process).

Required Associated Types§

source

type Key: Eq

The key type of the key-value pair

source

type Error

The error type for serialization and deserialization

Required Methods§

source

fn serialize_into(&self, buffer: &mut [u8]) -> Result<usize, Self::Error>

Serialize the key-value item into the given buffer. Returns the number of bytes the buffer was filled with or an error.

The serialized data does not have to self-describe its length or do framing. This crate already stores the length of any item in flash.

source

fn deserialize_from(buffer: &[u8]) -> Result<Self, Self::Error>
where Self: Sized,

Deserialize the key-value item from the given buffer. This buffer should be as long as what was serialized before.

source

fn key(&self) -> Self::Key

The key of the key-value item. It is used by the storage to know what the key of this item is.

Provided Methods§

source

fn deserialize_key_only(buffer: &[u8]) -> Result<Self::Key, Self::Error>
where Self: Sized,

Optimization for deserializing the key only. Can give a small performance boost if your key is easily extractable from the buffer.

Implementors§