pub trait Encodable {
const FORMAT: Format;
// Required method
fn encode(
&self,
writer: &mut (impl WritesEncodable + ?Sized),
) -> Result<(), CodecError>;
// Provided method
fn encode_header(
&self,
writer: &mut (impl WritesEncodable + ?Sized),
) -> Result<(), CodecError> { ... }
}Expand description
A thing that encodes into
codec-compliant data.
Required Associated Constants§
Required Methods§
Sourcefn encode(
&self,
writer: &mut (impl WritesEncodable + ?Sized),
) -> Result<(), CodecError>
fn encode( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>
Encodes this thing’s data into writer
without encoding a DataHeader.
In most cases, WritesEncodable::write_data should
be used instead of calling this function directly.
let data = Text::from("cupcakes!");
// Encode data into a vector.
let mut encoded = vec![];
data.encode_header(&mut encoded).unwrap();
data.encode(&mut encoded).unwrap();
let mut encoded_slice = encoded.as_slice();
// Decode the header.
let mut decoded_header = DataHeader::default();
decoded_header.decode(&mut encoded_slice, None).unwrap();
// Decode the data.
let mut decoded_data = Text::default();
decoded_data.decode(&mut encoded_slice, Some(decoded_header)).unwrap();
assert_eq!(data, decoded_data);Provided Methods§
Sourcefn encode_header(
&self,
writer: &mut (impl WritesEncodable + ?Sized),
) -> Result<(), CodecError>
fn encode_header( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>
Encodes this thing’s data header into writer.
If Self’s Encodable::FORMAT is not
structured,
this function should be a no-op.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl Encodable for [u8]
impl Encodable for [u8]
Source§const FORMAT: Format
const FORMAT: Format
Encoded as a sequence of Format::Data,
each containing a single u8 from the slice.
fn encode( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>
fn encode_header( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>
Source§impl Encodable for f32
impl Encodable for f32
Source§const FORMAT: Format
const FORMAT: Format
Encoded as a Format::Blob(4) containing the result of f32::to_le_bytes.
fn encode( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>
Source§impl Encodable for f64
impl Encodable for f64
Source§const FORMAT: Format
const FORMAT: Format
Encoded as a Format::Blob(8) containing the result of f64::to_le_bytes.
fn encode( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>
Source§impl Encodable for i8
impl Encodable for i8
Source§const FORMAT: Format
const FORMAT: Format
Encoded as a Format::Blob(1) containing the result of i8::to_le_bytes.
fn encode( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>
Source§impl Encodable for i16
impl Encodable for i16
Source§const FORMAT: Format
const FORMAT: Format
Encoded as a Format::Blob(2) containing the result of i16::to_le_bytes.
fn encode( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>
Source§impl Encodable for i32
impl Encodable for i32
Source§const FORMAT: Format
const FORMAT: Format
Encoded as a Format::Blob(4) containing the result of i32::to_le_bytes.
fn encode( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>
Source§impl Encodable for i64
impl Encodable for i64
Source§const FORMAT: Format
const FORMAT: Format
Encoded as a Format::Blob(8) containing the result of i64::to_le_bytes.
fn encode( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>
Source§impl Encodable for u8
impl Encodable for u8
Source§const FORMAT: Format
const FORMAT: Format
Encoded as a Format::Blob(1) containing the result of u8::to_le_bytes.
fn encode( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>
Source§impl Encodable for u16
impl Encodable for u16
Source§const FORMAT: Format
const FORMAT: Format
Encoded as a Format::Blob(2) containing the result of u16::to_le_bytes.
fn encode( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>
Source§impl Encodable for u32
impl Encodable for u32
Source§const FORMAT: Format
const FORMAT: Format
Encoded as a Format::Blob(4) containing the result of u32::to_le_bytes.
fn encode( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>
Source§impl Encodable for u64
impl Encodable for u64
Source§const FORMAT: Format
const FORMAT: Format
Encoded as a Format::Blob(8) containing the result of u64::to_le_bytes.
fn encode( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>
Source§impl<T> Encodable for Option<T>
impl<T> Encodable for Option<T>
const FORMAT: Format
fn encode( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>
fn encode_header( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>
Source§impl<T> Encodable for Vec<T>where
T: Encodable + 'static,
impl<T> Encodable for Vec<T>where
T: Encodable + 'static,
Source§const FORMAT: Format
const FORMAT: Format
Encoded as a sequence of Format::Data, each
containing a single T from the vector.
A Vec<u8> has the same encoding as a [u8].
fn encode( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>
fn encode_header( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>
Source§impl<const SIZE: usize> Encodable for [u8; SIZE]
impl<const SIZE: usize> Encodable for [u8; SIZE]
Source§const FORMAT: Format
const FORMAT: Format
Encoded as a Format::Data containing a
Format::Blob(SIZE).