pub trait Encode<M> {
type Encode: Encode<M> + ?Sized;
const IS_BITWISE_ENCODE: bool = false;
// Required methods
fn encode<E>(&self, encoder: E) -> Result<(), <E as Encoder>::Error>
where E: Encoder<Mode = M>;
fn as_encode(&self) -> &Self::Encode;
// Provided method
fn size_hint(&self) -> Option<usize> { ... }
}Expand description
Trait governing how types are encoded.
This is typically implemented automatically using the Encode derive.
§Examples
use musli::Encode;
#[derive(Encode)]
struct MyType {
data: [u32; 8],
}Implementing manually:
use musli::{Encode, Encoder};
struct MyType {
data: [u32; 8],
}
impl<M> Encode<M> for MyType {
type Encode = Self;
#[inline]
fn encode<E>(&self, encoder: E) -> Result<(), E::Error>
where
E: Encoder<Mode = M>,
{
encoder.encode(&self.data)
}
#[inline]
fn as_encode(&self) -> &Self::Encode {
self
}
}Provided Associated Constants§
Sourceconst IS_BITWISE_ENCODE: bool = false
const IS_BITWISE_ENCODE: bool = false
Whether the type is packed. Packed types can be bitwise copied if the representation of the serialization format is identical to the memory layout of the type.
Note that setting this to true has safety implications, since it
implies that assuming the type is correctly aligned it can be validly
bitwise copied when encoded. Setting it to false is always safe.
This being set to true also implies that the type is Copy, and must
not have a Drop implementation.
Required Associated Types§
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.