SizeEncoder

Struct SizeEncoder 

Source
pub struct SizeEncoder { /* private fields */ }
Expand description

An encoder that counts the size of the encoded data.

This encoder is useful for calculating the size of the encoded data without actually encoding it, allowing you to pre-allocate a buffer of the correct size before encoding the data.

Note that this encoder runs all the same encoding logic as any other encoder, so it will trigger the same side effects that other encoders would trigger (e.g Allocations). See the Encodable trait for more information on idempotent encodes.

§Example

use encode::Encodable;
use encode::encoders::SizeEncoder;

let encodable = c"hello, world!";
let mut encoder = SizeEncoder::new();
encodable.encode(&mut encoder).unwrap();
assert_eq!(encoder.size(), 14, "13 bytes from the ASCII string and 1 byte for the null terminator");

Implementations§

Source§

impl SizeEncoder

Source

pub fn new() -> Self

Creates a new SizeEncoder.

Source

pub fn size(&self) -> usize

Returns the size of the encoded data.

Trait Implementations§

Source§

impl BaseEncoder for SizeEncoder

Source§

type Error = Infallible

The error type returned by all encoding operations. Read more
Source§

impl ByteEncoder for SizeEncoder

Source§

fn put_slice(&mut self, slice: &[u8]) -> Result<(), Self::Error>

Writes a slice of bytes into the encoder. Read more
Source§

fn put_byte(&mut self, _byte: u8) -> Result<(), Self::Error>

Writes a single byte into the encoder. Read more
Source§

impl Clone for SizeEncoder

Source§

fn clone(&self) -> SizeEncoder

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SizeEncoder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SizeEncoder

Source§

fn default() -> SizeEncoder

Returns the “default value” for a type. Read more
Source§

impl From<SizeEncoder> for usize

Source§

fn from(encoder: SizeEncoder) -> usize

Converts to this type from the input type.
Source§

impl Ord for SizeEncoder

Source§

fn cmp(&self, other: &SizeEncoder) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for SizeEncoder

Source§

fn eq(&self, other: &SizeEncoder) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for SizeEncoder

Source§

fn partial_cmp(&self, other: &SizeEncoder) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Write for SizeEncoder

Source§

fn write_str(&mut self, s: &str) -> Result

Writes a string slice into this writer, returning whether the write succeeded. Read more
1.1.0 · Source§

fn write_char(&mut self, c: char) -> Result<(), Error>

Writes a char into this writer, returning whether the write succeeded. Read more
1.0.0 · Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Glue for usage of the write! macro with implementors of this trait. Read more
Source§

impl Copy for SizeEncoder

Source§

impl Eq for SizeEncoder

Source§

impl StructuralPartialEq for SizeEncoder

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> StrEncoder for T
where T: ByteEncoder,

Source§

fn put_str(&mut self, string: &str) -> Result<(), <T as BaseEncoder>::Error>

Writes an str into the encoder. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.