pub trait EncodePacked<M> {
// Required method
fn encode_packed<E>(&self, encoder: E) -> Result<(), <E as Encoder>::Error>
where E: Encoder<Mode = M>;
// Provided method
fn size_hint(&self) -> Option<usize> { ... }
}Expand description
Trait governing how a type is encoded as a packed value.
Packed encodings are ones where data follow one after another, with no “metadata” indicating when one value starts and another stops.
This is typically used automatically through the #[musli(packed)]
attribute through the Decode derive.
§Examples
use musli::Encode;
#[derive(Encode)]
struct PackedType {
#[musli(packed)]
data: (u32, u32),
}Implementing manually:
use musli::{Encode, Encoder};
use musli::en::SequenceEncoder;
struct PackedType {
data: (u32, u32),
}
impl<M> Encode<M> for PackedType {
type Encode = Self;
#[inline]
fn encode<E>(&self, encoder: E) -> Result<(), E::Error>
where
E: Encoder,
{
let mut pack = encoder.encode_pack()?;
pack.push(&self.data.0);
pack.push(&self.data.1);
pack.finish_sequence()
}
#[inline]
fn as_encode(&self) -> &Self::Encode {
self
}
}Required Methods§
Provided Methods§
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.