pub trait EncodePacked<M> {
// Required method
fn encode_packed<E>(
&self,
cx: &E::Cx,
encoder: E,
) -> Result<E::Ok, E::Error>
where E: Encoder<Mode = M>;
}
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 {
fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
where
E: Encoder,
{
let mut pack = encoder.encode_pack()?;
pack.push(&self.data.0);
pack.push(&self.data.1);
pack.finish_sequence()
}
}
Required 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.