pub trait Serializer: Sized {
// Required method
fn serialize<W: Write>(&self, writer: &mut W) -> Result<()>;
// Provided method
fn try_to_vec(&self) -> Result<Vec<u8>> { ... }
}
Expand description
Serializer
is a trait that allows for data serialization and deserialization
similar to Borsh, but via a separate trait. This allows for serialization
of additional metadata while using underlying Borsh primitives. For example:
a struct can implement both Borsh and Serializer traits where Serializer
can store custom metadata (e.g. struct version) and then store the struct
using Borsh. Both Serializer
and Borsh are almost identical, where
Serializer
is meant to signal intent for custom serialization.
Serializer
is a complimentary trait for Serializable
struct
and can be used to prevent direct Borsh serialization of a struct.
Required Methods§
Provided Methods§
fn try_to_vec(&self) -> Result<Vec<u8>>
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.