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§
Sourcefn encoded_size(&self) -> usize
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.
Provided Methods§
Sourcefn encode_to_bytes(&self) -> Bytes
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", so this trait is not object safe.