Skip to main content

Encodable

Trait Encodable 

Source
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§

Source

const FORMAT: Format

This thing’s Format.

Required Methods§

Source

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§

Source

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", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Encodable for bool

Source§

const FORMAT: Format = u8::FORMAT

Encoded as a u8, with a value of 1 for true and 0 for false.

Source§

fn encode( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>

Source§

impl Encodable for f32

Source§

const FORMAT: Format

Encoded as a Format::Blob(4) containing the result of f32::to_le_bytes.

Source§

fn encode( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>

Source§

impl Encodable for f64

Source§

const FORMAT: Format

Encoded as a Format::Blob(8) containing the result of f64::to_le_bytes.

Source§

fn encode( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>

Source§

impl Encodable for i8

Source§

const FORMAT: Format

Encoded as a Format::Blob(1) containing the result of i8::to_le_bytes.

Source§

fn encode( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>

Source§

impl Encodable for i16

Source§

const FORMAT: Format

Encoded as a Format::Blob(2) containing the result of i16::to_le_bytes.

Source§

fn encode( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>

Source§

impl Encodable for i32

Source§

const FORMAT: Format

Encoded as a Format::Blob(4) containing the result of i32::to_le_bytes.

Source§

fn encode( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>

Source§

impl Encodable for i64

Source§

const FORMAT: Format

Encoded as a Format::Blob(8) containing the result of i64::to_le_bytes.

Source§

fn encode( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>

Source§

impl Encodable for u8

Source§

const FORMAT: Format

Encoded as a Format::Blob(1) containing the result of u8::to_le_bytes.

Source§

fn encode( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>

Source§

impl Encodable for u16

Source§

const FORMAT: Format

Encoded as a Format::Blob(2) containing the result of u16::to_le_bytes.

Source§

fn encode( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>

Source§

impl Encodable for u32

Source§

const FORMAT: Format

Encoded as a Format::Blob(4) containing the result of u32::to_le_bytes.

Source§

fn encode( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>

Source§

impl Encodable for u64

Source§

const FORMAT: Format

Encoded as a Format::Blob(8) containing the result of u64::to_le_bytes.

Source§

fn encode( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>

Source§

impl Encodable for [u8]

Source§

const FORMAT: Format

Encoded as a sequence of Format::Data, each containing a single u8 from the slice.

Source§

fn encode( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>

Source§

fn encode_header( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>

Source§

impl<K, V> Encodable for BTreeMap<K, V>
where K: Encodable + Ord + Clone + 'static, V: Encodable + Clone + 'static,

Source§

const FORMAT: Format

Maps are encoded as a sorted vector of keys followed by a sorted vector of corresponding values.

Source§

fn encode( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>

Source§

impl<T> Encodable for Option<T>
where T: Default + Encodable + 'static,

Source§

const FORMAT: Format

Source§

fn encode( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>

Source§

fn encode_header( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>

Source§

impl<T> Encodable for Vec<T>
where T: Encodable + 'static,

Source§

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].

Source§

fn encode( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>

Source§

fn encode_header( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>

Source§

impl<const SIZE: usize> Encodable for [u8; SIZE]

Source§

const FORMAT: Format

Encoded as a Format::Data containing a Format::Blob(SIZE).

Source§

fn encode( &self, writer: &mut (impl WritesEncodable + ?Sized), ) -> Result<(), CodecError>

Implementors§

Source§

impl Encodable for Unspecified

Source§

const FORMAT: Format = Format::Fluid

Source§

impl Encodable for Text

Source§

const FORMAT: Format = <[u8]>::FORMAT

Source§

impl Encodable for Type

Source§

const FORMAT: Format = Format::Fluid

Source§

impl Encodable for Format

Source§

const FORMAT: Format = Format::Fluid

Source§

impl Encodable for CryptoCert

Source§

impl Encodable for EncryptedData

Source§

impl Encodable for HashBytes

Source§

const FORMAT: Format = <[core::primitive::u8; 32]>::FORMAT

Source§

impl Encodable for PrivateKeyBytes

Source§

const FORMAT: Format = <[core::primitive::u8; 32]>::FORMAT

Source§

impl Encodable for PublicKeyBytes

Source§

const FORMAT: Format = <[core::primitive::u8; 32]>::FORMAT

Source§

impl Encodable for SignatureBytes

Source§

const FORMAT: Format = <[core::primitive::u8; 64]>::FORMAT

Source§

impl Encodable for Coda

Source§

impl Encodable for DataField

Source§

impl Encodable for DataType

Source§

impl Encodable for DataHeader