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§
sourcetype Serializable: StoreIpld + LoadIpld + CondSync
type Serializable: StoreIpld + LoadIpld + CondSync
The at-rest representation of this storable type.
Required Methods§
sourcefn to_serializable(
&self,
store: &impl BlockStore
) -> impl Future<Output = Result<Self::Serializable>> + CondSend
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.
sourcefn from_serializable(
cid: Option<&Cid>,
serializable: Self::Serializable
) -> impl Future<Output = Result<Self>> + CondSend
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§
sourcefn persisted_as(&self) -> Option<&OnceCell<Cid>>
fn persisted_as(&self) -> Option<&OnceCell<Cid>>
Return a serialization cache, if it exists.
By default, this always returns None
.