fuel_core_storage/codec/postcard.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
//! The module contains the implementation of the `Postcard` codec.
//! Any type that implements `serde::Serialize` and `serde::Deserialize`
//! can use the `Postcard` codec to be encoded/decoded into/from bytes.
//! The `serde` serialization and deserialization add their own overhead,
//! so this codec shouldn't be used for simple types.
use crate::codec::{
Decode,
Encode,
};
#[cfg(feature = "std")]
use std::borrow::Cow;
#[cfg(not(feature = "std"))]
use alloc::borrow::Cow;
/// The codec is used to serialized/deserialized types that supports `serde::Serialize` and `serde::Deserialize`.
pub struct Postcard;
impl<T> Encode<T> for Postcard
where
T: ?Sized + serde::Serialize,
{
type Encoder<'a> = Cow<'a, [u8]> where T: 'a;
fn encode(value: &T) -> Self::Encoder<'_> {
Cow::Owned(postcard::to_allocvec(value).expect(
"It should be impossible to fail unless serialization is not implemented, which is not true for our types.",
))
}
}
impl<T> Decode<T> for Postcard
where
T: serde::de::DeserializeOwned,
{
fn decode(bytes: &[u8]) -> anyhow::Result<T> {
Ok(postcard::from_bytes(bytes)?)
}
}