Trait wnfs_common::Storable

source ·
pub trait Storable: Sized {
    type Serializable: StoreIpld + LoadIpld + CondSync;

    // Required methods
    fn to_serializable(
        &self,
        store: &impl BlockStore
    ) -> impl Future<Output = Result<Self::Serializable>> + CondSend;
    fn from_serializable(
        cid: Option<&Cid>,
        serializable: Self::Serializable
    ) -> impl Future<Output = Result<Self>> + CondSend;

    // Provided methods
    fn persisted_as(&self) -> Option<&OnceCell<Cid>> { ... }
    fn store(
        &self,
        store: &impl BlockStore
    ) -> impl Future<Output = Result<Cid>> + CondSend
       where Self: CondSync { ... }
    fn load(
        cid: &Cid,
        store: &impl BlockStore
    ) -> impl Future<Output = Result<Self>> + CondSend { ... }
}
Expand description

The trait that defines how to store something in a blockstore.

This works via a two-tiered system, where the actual in-memory representation (the struct that implements this trait) is not the same as the at-rest representation of itself. The at-rest representation is given by the Serializable associated type.

Commonly, the Serializable type implements serde’s Serialize and Deserialize traits and thus can automatically be used without having to implement StoreIpld and LoadIpld yourself. In that case, the default implementation will use serde_ipld_dagcbor.

This trait also optionally supports memoizing serialization via the persisted_as function. You can add a field persisted_as: OnceCell<Cid> to your in-memory representation and return it in the persisted_as function and any store calls will automatically populate that cache. If you do so, remember to initialize the OnceCell if a Cid is passed in the from_serializable call, such that a store call right after a load call is practically free.

Required Associated Types§

source

type Serializable: StoreIpld + LoadIpld + CondSync

The at-rest representation of this storable type.

Required Methods§

source

fn to_serializable( &self, store: &impl BlockStore ) -> impl Future<Output = Result<Self::Serializable>> + CondSend

Turn the current type into the at-rest representation of this type.

source

fn from_serializable( cid: Option<&Cid>, serializable: Self::Serializable ) -> impl Future<Output = Result<Self>> + CondSend

Take an at-rest representation of this type and turn it into the in-memory representation. You can use the cid parameter to populate a cache.

Provided Methods§

source

fn persisted_as(&self) -> Option<&OnceCell<Cid>>

Return a serialization cache, if it exists. By default, this always returns None.

source

fn store( &self, store: &impl BlockStore ) -> impl Future<Output = Result<Cid>> + CondSend
where Self: CondSync,

Store this data type in a given BlockStore.

This will short-circuit by using the persisted_as once-cell, if available.

source

fn load( cid: &Cid, store: &impl BlockStore ) -> impl Future<Output = Result<Self>> + CondSend

Try to load a value of this type from a CID.

This will pass on the CID to the from_serializable function so it can populate a cache in some cases.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl Storable for i8

§

type Serializable = i8

source§

async fn to_serializable( &self, _store: &impl BlockStore ) -> Result<Self::Serializable>

source§

async fn from_serializable( _cid: Option<&Cid>, serializable: Self::Serializable ) -> Result<Self>

source§

impl Storable for i16

§

type Serializable = i16

source§

async fn to_serializable( &self, _store: &impl BlockStore ) -> Result<Self::Serializable>

source§

async fn from_serializable( _cid: Option<&Cid>, serializable: Self::Serializable ) -> Result<Self>

source§

impl Storable for i32

§

type Serializable = i32

source§

async fn to_serializable( &self, _store: &impl BlockStore ) -> Result<Self::Serializable>

source§

async fn from_serializable( _cid: Option<&Cid>, serializable: Self::Serializable ) -> Result<Self>

source§

impl Storable for i64

§

type Serializable = i64

source§

async fn to_serializable( &self, _store: &impl BlockStore ) -> Result<Self::Serializable>

source§

async fn from_serializable( _cid: Option<&Cid>, serializable: Self::Serializable ) -> Result<Self>

source§

impl Storable for i128

§

type Serializable = i128

source§

async fn to_serializable( &self, _store: &impl BlockStore ) -> Result<Self::Serializable>

source§

async fn from_serializable( _cid: Option<&Cid>, serializable: Self::Serializable ) -> Result<Self>

source§

impl Storable for isize

§

type Serializable = isize

source§

async fn to_serializable( &self, _store: &impl BlockStore ) -> Result<Self::Serializable>

source§

async fn from_serializable( _cid: Option<&Cid>, serializable: Self::Serializable ) -> Result<Self>

source§

impl Storable for u8

§

type Serializable = u8

source§

async fn to_serializable( &self, _store: &impl BlockStore ) -> Result<Self::Serializable>

source§

async fn from_serializable( _cid: Option<&Cid>, serializable: Self::Serializable ) -> Result<Self>

source§

impl Storable for u16

§

type Serializable = u16

source§

async fn to_serializable( &self, _store: &impl BlockStore ) -> Result<Self::Serializable>

source§

async fn from_serializable( _cid: Option<&Cid>, serializable: Self::Serializable ) -> Result<Self>

source§

impl Storable for u32

§

type Serializable = u32

source§

async fn to_serializable( &self, _store: &impl BlockStore ) -> Result<Self::Serializable>

source§

async fn from_serializable( _cid: Option<&Cid>, serializable: Self::Serializable ) -> Result<Self>

source§

impl Storable for u64

§

type Serializable = u64

source§

async fn to_serializable( &self, _store: &impl BlockStore ) -> Result<Self::Serializable>

source§

async fn from_serializable( _cid: Option<&Cid>, serializable: Self::Serializable ) -> Result<Self>

source§

impl Storable for u128

§

type Serializable = u128

source§

async fn to_serializable( &self, _store: &impl BlockStore ) -> Result<Self::Serializable>

source§

async fn from_serializable( _cid: Option<&Cid>, serializable: Self::Serializable ) -> Result<Self>

source§

impl Storable for usize

§

type Serializable = usize

source§

async fn to_serializable( &self, _store: &impl BlockStore ) -> Result<Self::Serializable>

source§

async fn from_serializable( _cid: Option<&Cid>, serializable: Self::Serializable ) -> Result<Self>

source§

impl Storable for String

§

type Serializable = String

source§

async fn to_serializable( &self, _store: &impl BlockStore ) -> Result<Self::Serializable>

source§

async fn from_serializable( _cid: Option<&Cid>, serializable: Self::Serializable ) -> Result<Self>

source§

impl Storable for [u8; 0]

§

type Serializable = [u8; 0]

source§

async fn to_serializable( &self, _store: &impl BlockStore ) -> Result<Self::Serializable>

source§

async fn from_serializable( _cid: Option<&Cid>, serializable: Self::Serializable ) -> Result<Self>

source§

impl Storable for [u8; 1]

§

type Serializable = [u8; 1]

source§

async fn to_serializable( &self, _store: &impl BlockStore ) -> Result<Self::Serializable>

source§

async fn from_serializable( _cid: Option<&Cid>, serializable: Self::Serializable ) -> Result<Self>

source§

impl Storable for [u8; 2]

§

type Serializable = [u8; 2]

source§

async fn to_serializable( &self, _store: &impl BlockStore ) -> Result<Self::Serializable>

source§

async fn from_serializable( _cid: Option<&Cid>, serializable: Self::Serializable ) -> Result<Self>

source§

impl Storable for [u8; 4]

§

type Serializable = [u8; 4]

source§

async fn to_serializable( &self, _store: &impl BlockStore ) -> Result<Self::Serializable>

source§

async fn from_serializable( _cid: Option<&Cid>, serializable: Self::Serializable ) -> Result<Self>

source§

impl Storable for [u8; 8]

§

type Serializable = [u8; 8]

source§

async fn to_serializable( &self, _store: &impl BlockStore ) -> Result<Self::Serializable>

source§

async fn from_serializable( _cid: Option<&Cid>, serializable: Self::Serializable ) -> Result<Self>

source§

impl Storable for [u8; 16]

§

type Serializable = [u8; 16]

source§

async fn to_serializable( &self, _store: &impl BlockStore ) -> Result<Self::Serializable>

source§

async fn from_serializable( _cid: Option<&Cid>, serializable: Self::Serializable ) -> Result<Self>

source§

impl Storable for [u8; 32]

§

type Serializable = [u8; 32]

source§

async fn to_serializable( &self, _store: &impl BlockStore ) -> Result<Self::Serializable>

source§

async fn from_serializable( _cid: Option<&Cid>, serializable: Self::Serializable ) -> Result<Self>

source§

impl<A> Storable for (A,)

§

type Serializable = (A,)

source§

async fn to_serializable( &self, _store: &impl BlockStore ) -> Result<Self::Serializable>

source§

async fn from_serializable( _cid: Option<&Cid>, serializable: Self::Serializable ) -> Result<Self>

source§

impl<A, B> Storable for (A, B)

§

type Serializable = (A, B)

source§

async fn to_serializable( &self, _store: &impl BlockStore ) -> Result<Self::Serializable>

source§

async fn from_serializable( _cid: Option<&Cid>, serializable: Self::Serializable ) -> Result<Self>

source§

impl<A, B, C> Storable for (A, B, C)

§

type Serializable = (A, B, C)

source§

async fn to_serializable( &self, _store: &impl BlockStore ) -> Result<Self::Serializable>

source§

async fn from_serializable( _cid: Option<&Cid>, serializable: Self::Serializable ) -> Result<Self>

source§

impl<A, B, C, D> Storable for (A, B, C, D)

§

type Serializable = (A, B, C, D)

source§

async fn to_serializable( &self, _store: &impl BlockStore ) -> Result<Self::Serializable>

source§

async fn from_serializable( _cid: Option<&Cid>, serializable: Self::Serializable ) -> Result<Self>

source§

impl<A, B, C, D, E> Storable for (A, B, C, D, E)

§

type Serializable = (A, B, C, D, E)

source§

async fn to_serializable( &self, _store: &impl BlockStore ) -> Result<Self::Serializable>

source§

async fn from_serializable( _cid: Option<&Cid>, serializable: Self::Serializable ) -> Result<Self>

source§

impl<A, B, C, D, E, F> Storable for (A, B, C, D, E, F)

§

type Serializable = (A, B, C, D, E, F)

source§

async fn to_serializable( &self, _store: &impl BlockStore ) -> Result<Self::Serializable>

source§

async fn from_serializable( _cid: Option<&Cid>, serializable: Self::Serializable ) -> Result<Self>

source§

impl<A, B, C, D, E, F, G> Storable for (A, B, C, D, E, F, G)

§

type Serializable = (A, B, C, D, E, F, G)

source§

async fn to_serializable( &self, _store: &impl BlockStore ) -> Result<Self::Serializable>

source§

async fn from_serializable( _cid: Option<&Cid>, serializable: Self::Serializable ) -> Result<Self>

source§

impl<A, B, C, D, E, F, G, H> Storable for (A, B, C, D, E, F, G, H)

§

type Serializable = (A, B, C, D, E, F, G, H)

source§

async fn to_serializable( &self, _store: &impl BlockStore ) -> Result<Self::Serializable>

source§

async fn from_serializable( _cid: Option<&Cid>, serializable: Self::Serializable ) -> Result<Self>

source§

impl<A, B, C, D, E, F, G, H, I> Storable for (A, B, C, D, E, F, G, H, I)

§

type Serializable = (A, B, C, D, E, F, G, H, I)

source§

async fn to_serializable( &self, _store: &impl BlockStore ) -> Result<Self::Serializable>

source§

async fn from_serializable( _cid: Option<&Cid>, serializable: Self::Serializable ) -> Result<Self>

source§

impl<A, B, C, D, E, F, G, H, I, J> Storable for (A, B, C, D, E, F, G, H, I, J)

§

type Serializable = (A, B, C, D, E, F, G, H, I, J)

source§

async fn to_serializable( &self, _store: &impl BlockStore ) -> Result<Self::Serializable>

source§

async fn from_serializable( _cid: Option<&Cid>, serializable: Self::Serializable ) -> Result<Self>

source§

impl<A, B, C, D, E, F, G, H, I, J, K> Storable for (A, B, C, D, E, F, G, H, I, J, K)

§

type Serializable = (A, B, C, D, E, F, G, H, I, J, K)

source§

async fn to_serializable( &self, _store: &impl BlockStore ) -> Result<Self::Serializable>

source§

async fn from_serializable( _cid: Option<&Cid>, serializable: Self::Serializable ) -> Result<Self>

Implementors§