Encode

Trait Encode 

Source
pub trait Encode {
    // Required methods
    fn encode_into<W: Write>(&self, dest: &mut W) -> Result<()>;
    fn encoding_length(&self) -> Result<usize>;

    // Provided method
    fn encode(&self) -> Result<Vec<u8>> { ... }
}
Expand description

A trait for values that can be encoded into bytes deterministically.

Required Methods§

Source

fn encode_into<W: Write>(&self, dest: &mut W) -> Result<()>

Writes the encoded representation of the value to the destination writer. Can error due to either a write error from dest, or an encoding error for types where invalid values are possible.

It may be more convenient to call encode which returns bytes, however encode_into will often be more efficient since it can write the encoding without necessarily allocating a new Vec<u8>.

Source

fn encoding_length(&self) -> Result<usize>

Calculates the length of the encoding for this value. Can error for types where invalid values are possible.

Provided Methods§

Source

fn encode(&self) -> Result<Vec<u8>>

Returns the encoded representation of the value as a Vec<u8>.

While this method is convenient, it will often be more efficient to call encode_into since encode usually involves allocating a new Vec<u8>.

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 Encode for bool

Source§

fn encode_into<W: Write>(&self, dest: &mut W) -> Result<()>

Encodes the boolean as a single byte: 0 for false or 1 for true.

Source§

fn encoding_length(&self) -> Result<usize>

Always returns Ok(1).

Source§

impl Encode for i8

Source§

fn encode_into<W: Write>(&self, dest: &mut W) -> Result<()>

Encodes the integer as fixed-size big-endian bytes.

Source§

fn encoding_length(&self) -> Result<usize>

Returns the size of the integer in bytes. Will always return an Ok result.

Source§

impl Encode for i16

Source§

fn encode_into<W: Write>(&self, dest: &mut W) -> Result<()>

Encodes the integer as fixed-size big-endian bytes.

Source§

fn encoding_length(&self) -> Result<usize>

Returns the size of the integer in bytes. Will always return an Ok result.

Source§

impl Encode for i32

Source§

fn encode_into<W: Write>(&self, dest: &mut W) -> Result<()>

Encodes the integer as fixed-size big-endian bytes.

Source§

fn encoding_length(&self) -> Result<usize>

Returns the size of the integer in bytes. Will always return an Ok result.

Source§

impl Encode for i64

Source§

fn encode_into<W: Write>(&self, dest: &mut W) -> Result<()>

Encodes the integer as fixed-size big-endian bytes.

Source§

fn encoding_length(&self) -> Result<usize>

Returns the size of the integer in bytes. Will always return an Ok result.

Source§

impl Encode for i128

Source§

fn encode_into<W: Write>(&self, dest: &mut W) -> Result<()>

Encodes the integer as fixed-size big-endian bytes.

Source§

fn encoding_length(&self) -> Result<usize>

Returns the size of the integer in bytes. Will always return an Ok result.

Source§

impl Encode for u8

Source§

fn encode_into<W: Write>(&self, dest: &mut W) -> Result<()>

Encodes the integer as fixed-size big-endian bytes.

Source§

fn encoding_length(&self) -> Result<usize>

Returns the size of the integer in bytes. Will always return an Ok result.

Source§

impl Encode for u16

Source§

fn encode_into<W: Write>(&self, dest: &mut W) -> Result<()>

Encodes the integer as fixed-size big-endian bytes.

Source§

fn encoding_length(&self) -> Result<usize>

Returns the size of the integer in bytes. Will always return an Ok result.

Source§

impl Encode for u32

Source§

fn encode_into<W: Write>(&self, dest: &mut W) -> Result<()>

Encodes the integer as fixed-size big-endian bytes.

Source§

fn encoding_length(&self) -> Result<usize>

Returns the size of the integer in bytes. Will always return an Ok result.

Source§

impl Encode for u64

Source§

fn encode_into<W: Write>(&self, dest: &mut W) -> Result<()>

Encodes the integer as fixed-size big-endian bytes.

Source§

fn encoding_length(&self) -> Result<usize>

Returns the size of the integer in bytes. Will always return an Ok result.

Source§

impl Encode for u128

Source§

fn encode_into<W: Write>(&self, dest: &mut W) -> Result<()>

Encodes the integer as fixed-size big-endian bytes.

Source§

fn encoding_length(&self) -> Result<usize>

Returns the size of the integer in bytes. Will always return an Ok result.

Source§

impl Encode for ()

Source§

fn encode_into<W: Write>(&self, _: &mut W) -> Result<()>

Encoding a unit tuple is a no-op.

Source§

fn encoding_length(&self) -> Result<usize>

Always returns Ok(0).

Source§

impl<A: Encode + Terminated, B: Encode + Terminated, C: Encode + Terminated, D: Encode + Terminated, E: Encode + Terminated, F: Encode + Terminated, G: Encode + Terminated, H: Encode + Terminated, I: Encode + Terminated, J: Encode + Terminated, K: Encode + Terminated, L: Encode> Encode for (A, B, C, D, E, F, G, H, I, J, K, L)

Source§

fn encode_into<W: Write>(&self, dest: &mut W) -> Result<()>

Encodes the fields of the tuple one after another, in order.

Source§

fn encoding_length(&self) -> Result<usize>

Returns the sum of the encoding lengths of the fields of the tuple.

Source§

impl<A: Encode + Terminated, B: Encode + Terminated, C: Encode + Terminated, D: Encode + Terminated, E: Encode + Terminated, F: Encode + Terminated, G: Encode + Terminated, H: Encode + Terminated, I: Encode + Terminated, J: Encode + Terminated, K: Encode> Encode for (A, B, C, D, E, F, G, H, I, J, K)

Source§

fn encode_into<W: Write>(&self, dest: &mut W) -> Result<()>

Encodes the fields of the tuple one after another, in order.

Source§

fn encoding_length(&self) -> Result<usize>

Returns the sum of the encoding lengths of the fields of the tuple.

Source§

impl<A: Encode + Terminated, B: Encode + Terminated, C: Encode + Terminated, D: Encode + Terminated, E: Encode + Terminated, F: Encode + Terminated, G: Encode + Terminated, H: Encode + Terminated, I: Encode + Terminated, J: Encode> Encode for (A, B, C, D, E, F, G, H, I, J)

Source§

fn encode_into<W: Write>(&self, dest: &mut W) -> Result<()>

Encodes the fields of the tuple one after another, in order.

Source§

fn encoding_length(&self) -> Result<usize>

Returns the sum of the encoding lengths of the fields of the tuple.

Source§

impl<A: Encode + Terminated, B: Encode + Terminated, C: Encode + Terminated, D: Encode + Terminated, E: Encode + Terminated, F: Encode + Terminated, G: Encode + Terminated, H: Encode + Terminated, I: Encode> Encode for (A, B, C, D, E, F, G, H, I)

Source§

fn encode_into<W: Write>(&self, dest: &mut W) -> Result<()>

Encodes the fields of the tuple one after another, in order.

Source§

fn encoding_length(&self) -> Result<usize>

Returns the sum of the encoding lengths of the fields of the tuple.

Source§

impl<A: Encode + Terminated, B: Encode + Terminated, C: Encode + Terminated, D: Encode + Terminated, E: Encode + Terminated, F: Encode + Terminated, G: Encode + Terminated, H: Encode> Encode for (A, B, C, D, E, F, G, H)

Source§

fn encode_into<W: Write>(&self, dest: &mut W) -> Result<()>

Encodes the fields of the tuple one after another, in order.

Source§

fn encoding_length(&self) -> Result<usize>

Returns the sum of the encoding lengths of the fields of the tuple.

Source§

impl<A: Encode + Terminated, B: Encode + Terminated, C: Encode + Terminated, D: Encode + Terminated, E: Encode + Terminated, F: Encode + Terminated, G: Encode> Encode for (A, B, C, D, E, F, G)

Source§

fn encode_into<W: Write>(&self, dest: &mut W) -> Result<()>

Encodes the fields of the tuple one after another, in order.

Source§

fn encoding_length(&self) -> Result<usize>

Returns the sum of the encoding lengths of the fields of the tuple.

Source§

impl<A: Encode + Terminated, B: Encode + Terminated, C: Encode + Terminated, D: Encode + Terminated, E: Encode + Terminated, F: Encode> Encode for (A, B, C, D, E, F)

Source§

fn encode_into<W: Write>(&self, dest: &mut W) -> Result<()>

Encodes the fields of the tuple one after another, in order.

Source§

fn encoding_length(&self) -> Result<usize>

Returns the sum of the encoding lengths of the fields of the tuple.

Source§

impl<A: Encode + Terminated, B: Encode + Terminated, C: Encode + Terminated, D: Encode + Terminated, E: Encode> Encode for (A, B, C, D, E)

Source§

fn encode_into<W: Write>(&self, dest: &mut W) -> Result<()>

Encodes the fields of the tuple one after another, in order.

Source§

fn encoding_length(&self) -> Result<usize>

Returns the sum of the encoding lengths of the fields of the tuple.

Source§

impl<A: Encode + Terminated, B: Encode + Terminated, C: Encode + Terminated, D: Encode> Encode for (A, B, C, D)

Source§

fn encode_into<W: Write>(&self, dest: &mut W) -> Result<()>

Encodes the fields of the tuple one after another, in order.

Source§

fn encoding_length(&self) -> Result<usize>

Returns the sum of the encoding lengths of the fields of the tuple.

Source§

impl<A: Encode + Terminated, B: Encode + Terminated, C: Encode> Encode for (A, B, C)

Source§

fn encode_into<W: Write>(&self, dest: &mut W) -> Result<()>

Encodes the fields of the tuple one after another, in order.

Source§

fn encoding_length(&self) -> Result<usize>

Returns the sum of the encoding lengths of the fields of the tuple.

Source§

impl<A: Encode + Terminated, B: Encode> Encode for (A, B)

Source§

fn encode_into<W: Write>(&self, dest: &mut W) -> Result<()>

Encodes the fields of the tuple one after another, in order.

Source§

fn encoding_length(&self) -> Result<usize>

Returns the sum of the encoding lengths of the fields of the tuple.

Source§

impl<A: Encode> Encode for (A,)

Source§

fn encode_into<W: Write>(&self, dest: &mut W) -> Result<()>

Encodes the fields of the tuple one after another, in order.

Source§

fn encoding_length(&self) -> Result<usize>

Returns the sum of the encoding lengths of the fields of the tuple.

Source§

impl<T> Encode for PhantomData<T>

Source§

fn encode_into<W: Write>(&self, _: &mut W) -> Result<()>

Encoding PhantomData is a no-op.

Source§

fn encoding_length(&self) -> Result<usize>

Always returns Ok(0).

Source§

impl<T: Encode + Terminated> Encode for [T]

Source§

fn encode_into<W: Write>(&self, dest: &mut W) -> Result<()>

Encodes the elements of the slice one after another, in order.

Source§

fn encoding_length(&self) -> Result<usize>

Returns the sum of the encoding lengths of all elements.

Source§

impl<T: Encode + Terminated> Encode for Vec<T>

Source§

fn encode_into<W: Write>(&self, dest: &mut W) -> Result<()>

Encodes the elements of the vector one after another, in order.

Source§

fn encoding_length(&self) -> Result<usize>

Returns the sum of the encoding lengths of all elements.

Source§

impl<T: Encode + Terminated, const N: usize> Encode for [T; N]

Source§

impl<T: Encode> Encode for Option<T>

Source§

fn encode_into<W: Write>(&self, dest: &mut W) -> Result<()>

Encodes as a 0 byte for None, or as a 1 byte followed by the encoding of the inner value for Some.

Source§

fn encoding_length(&self) -> Result<usize>

Length will be 1 for None, or 1 plus the encoding length of the inner value for Some.

Source§

impl<T: Encode> Encode for Box<T>

Source§

fn encode_into<W: Write>(&self, dest: &mut W) -> Result<()>

Encodes the inner value.

Source§

fn encoding_length(&self) -> Result<usize>

Returns the encoding length of the inner value.

Implementors§