pub trait EncodeBytes<M> {
type EncodeBytes: ?Sized + EncodeBytes<M>;
const ENCODE_BYTES_PACKED: bool = false;
// Required methods
fn encode_bytes<E>(&self, encoder: E) -> Result<(), E::Error>
where E: Encoder<Mode = M>;
fn as_encode_bytes(&self) -> &Self::EncodeBytes;
// Provided method
fn size_hint(&self) -> Option<usize> { ... }
}Expand description
Trait governing how a type is encoded as bytes.
This is typically used automatically through the #[musli(bytes)] attribute
through the Encode derive.
§Examples
use musli::Encode;
#[derive(Encode)]
struct MyType {
#[musli(bytes)]
data: [u8; 128],
}Implementing manually:
use musli::{Encode, Encoder};
use musli::en::EncodeBytes;
struct MyType {
data: [u8; 128],
}
impl<M> Encode<M> for MyType {
type Encode = Self;
#[inline]
fn encode<E>(&self, encoder: E) -> Result<(), E::Error>
where
E: Encoder,
{
self.data.encode_bytes(encoder)
}
#[inline]
fn as_encode(&self) -> &Self::Encode {
self
}
}Provided Associated Constants§
Sourceconst ENCODE_BYTES_PACKED: bool = false
const ENCODE_BYTES_PACKED: 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.
Required Associated Types§
Sourcetype EncodeBytes: ?Sized + EncodeBytes<M>
type EncodeBytes: ?Sized + EncodeBytes<M>
The underlying type being encoded.
This is used to “peek through” types like references being encoded.
Required Methods§
Sourcefn encode_bytes<E>(&self, encoder: E) -> Result<(), E::Error>where
E: Encoder<Mode = M>,
fn encode_bytes<E>(&self, encoder: E) -> Result<(), E::Error>where
E: Encoder<Mode = M>,
Encode the given output as bytes.
Sourcefn as_encode_bytes(&self) -> &Self::EncodeBytes
fn as_encode_bytes(&self) -> &Self::EncodeBytes
Coerce into the underlying value being encoded.
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.