Skip to main content

Serialize

Trait Serialize 

Source
pub trait Serialize: SchemaWrite<DefaultConfig> {
    // Provided methods
    fn serialize(src: &Self::Src) -> WriteResult<Vec<u8>> { ... }
    fn serialize_into(dst: impl Writer, src: &Self::Src) -> WriteResult<()> { ... }
    fn serialized_size(src: &Self::Src) -> WriteResult<u64> { ... }
}
Expand description

Helper over SchemaWrite that automatically constructs a writer and serializes a source.

§Examples

Using containers (indirect serialization):

let vec: Vec<u8> = vec![1, 2, 3];
type Src = containers::Vec<u8, BincodeLen>;
let bytes = Src::serialize(&vec).unwrap();
let deserialized: Vec<u8> = wincode::deserialize(&bytes).unwrap();
assert_eq!(vec, deserialized);

Using direct serialization (T::Src = T):

let vec: Vec<u8> = vec![1, 2, 3];
let bytes = wincode::serialize(&vec).unwrap();
let deserialized: Vec<u8> = wincode::deserialize(&bytes).unwrap();
assert_eq!(vec, deserialized);

Provided Methods§

Source

fn serialize(src: &Self::Src) -> WriteResult<Vec<u8>>

Serialize a serializable type into a Vec of bytes.

Source

fn serialize_into(dst: impl Writer, src: &Self::Src) -> WriteResult<()>

Serialize a serializable type into the given byte buffer.

§Partial writes

This operation is not transactional. If it returns an error, the writer may already contain a prefix of the serialized value. Dynamically sized values in particular may discover insufficient capacity only after preceding fields have been written.

If the destination must remain unchanged on failure, serialize into a temporary buffer and copy the result only after serialization succeeds. For a fixed-size destination, callers can instead use Self::serialized_size to check that enough space is available first.

Source

fn serialized_size(src: &Self::Src) -> WriteResult<u64>

Get the size in bytes of the type when serialized.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§