Skip to main content

Encodable

Trait Encodable 

Source
pub trait Encodable {
    // Required methods
    fn encoded_size(&self) -> usize;
    fn encode<B: BufMut>(&self, encoder: &mut Encoder<B>);

    // Provided method
    fn encode_to_bytes(&self) -> Bytes { ... }
}
Expand description

Trait for types that can be encoded into a binary format.

Required Methods§

Source

fn encoded_size(&self) -> usize

Returns the number of bytes required to encode this value.

This should return the exact number of bytes that encode will write, allowing for efficient buffer pre-allocation.

Note: This function traverses the entire datastructure which scales with cardinality.

Source

fn encode<B: BufMut>(&self, encoder: &mut Encoder<B>)

Encodes this value into the provided encoder.

Provided Methods§

Source

fn encode_to_bytes(&self) -> Bytes

Convenience method that encodes this value to a Bytes buffer.

This is the easiest way to serialize splinter data. It allocates a buffer of the exact required size and encodes the value into it.

§Examples
use splinter_rs::{Splinter, Encodable, PartitionWrite};

let splinter = Splinter::from_iter([8, 42, 16]);
let bytes = splinter.encode_to_bytes();
assert!(!bytes.is_empty());
assert_eq!(bytes.len(), splinter.encoded_size());

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§

Source§

impl Encodable for Splinter

Source§

impl<B: Deref<Target = [u8]>> Encodable for CowSplinter<B>

Source§

impl<B: Deref<Target = [u8]>> Encodable for SplinterRef<B>