Encode

Trait Encode 

Source
pub unsafe trait Encode {
    // Required methods
    fn size(&self) -> Size;
    unsafe fn encode_unchecked(&self, w: &mut Writer<'_>);
}
Expand description

A trait for encoding values into a buffer.

§Note

This trait is intended for low-level implementation, which is also unsafe to call. Instead, use the EncodeExt trait, which allows safely constructing encodable sequences.

§Safety

This trait is unsafe because unsafe code relies on the safety invariants upheld by implementations. To implement this trait correctly, the following invariants must be maintained:

  • size must always return the exact number of bytes required to encode the value. In particular, size must consistently return the same value if self has not changed.

  • encode_unchecked must initialize exactly size bytes in the passed buffer.

Required Methods§

Source

fn size(&self) -> Size

Returns byte size of encodable value.

§Note

Although the Size type contains a usize and checks for overflow in its expand method, the Encode trait cannot encode a value larger than isize::MAX bytes sequentially. However, it is impossible to create a Writer with a buffer that exceeds this limit, so calling encode_unchecked with such a size is also impossible.

Source

unsafe fn encode_unchecked(&self, w: &mut Writer<'_>)

Encodes the value to the buffer.

§Safety

The caller must ensure that w.remaining() >= self.size().

Implementations on Foreign Types§

Source§

impl Encode for u8

Source§

fn size(&self) -> Size

Source§

unsafe fn encode_unchecked(&self, w: &mut Writer<'_>)

Source§

impl Encode for ()

Source§

fn size(&self) -> Size

Source§

unsafe fn encode_unchecked(&self, _: &mut Writer<'_>)

Source§

impl Encode for [u8]

Source§

fn size(&self) -> Size

Source§

unsafe fn encode_unchecked(&self, w: &mut Writer<'_>)

Source§

impl<E> Encode for &E
where E: Encode + ?Sized,

Source§

fn size(&self) -> Size

Source§

unsafe fn encode_unchecked(&self, w: &mut Writer<'_>)

Source§

impl<const N: usize> Encode for [u8; N]

Source§

fn size(&self) -> Size

Source§

unsafe fn encode_unchecked(&self, w: &mut Writer<'_>)

Implementors§