Skip to main content

BufEncoder

Struct BufEncoder 

Source
pub struct BufEncoder<const CAP: usize> { /* private fields */ }
Expand description

Hex-encodes bytes into the provided buffer.

This is an important building block for fast hex-encoding. Because string writing tools provided by core::fmt involve dynamic dispatch and don’t allow reserving capacity in strings buffering the hex and then formatting it is significantly faster.

The buffer has a fixed capacity specified when created. The capacity must be an even number since each byte is encoded as two hex characters.

§Examples

let mut encoder = BufEncoder::<4>::new(Case::Lower);
encoder.put_byte(0xab);
assert_eq!(encoder.as_str(), "ab");

The following code doesn’t compile because of odd capacity:

let mut encoder = BufEncoder::<3>::new(Case::Lower);

Implementations§

Source§

impl<const CAP: usize> BufEncoder<CAP>

Source

pub fn new(case: Case) -> Self

Creates an empty BufEncoder that will encode bytes to hex characters in the given case.

Source

pub fn put_byte(&mut self, byte: u8)

Encodes byte as hex and appends it to the buffer.

§Panics

The method panics if the buffer is full.

Source

pub fn put_bytes<I>(&mut self, bytes: I)
where I: IntoIterator, I::Item: Borrow<u8>,

Encodes bytes as hex and appends them to the buffer.

§Panics

The method panics if the bytes wouldn’t fit the buffer.

Source

pub fn put_bytes_min<'a>(&mut self, bytes: &'a [u8]) -> &'a [u8]

Encodes as many bytes as fit into the buffer as hex and return the remainder.

This method works just like put_bytes but instead of panicking it returns the unwritten bytes. The method returns an empty slice if all bytes were written

Source

pub fn is_full(&self) -> bool

Returns true if no more bytes can be written into the buffer.

Source

pub fn as_str(&self) -> &str

Returns the written bytes as a hex str.

Source

pub fn clear(&mut self)

Resets the buffer to become empty.

Source

pub fn space_remaining(&self) -> usize

How many bytes can be written to this buffer.

Note that this returns the number of bytes before encoding, not number of hex digits.

Trait Implementations§

Source§

impl<const CAP: usize> Clone for BufEncoder<CAP>

Source§

fn clone(&self) -> BufEncoder<CAP>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<const CAP: usize> Debug for BufEncoder<CAP>

Source§

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

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

impl<const CAP: usize> Default for BufEncoder<CAP>

Source§

fn default() -> Self

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

impl<const CAP: usize, A: Borrow<u8>> Extend<A> for BufEncoder<CAP>

Source§

fn extend<T: IntoIterator<Item = A>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<const CAP: usize, A: Borrow<u8>> FromIterator<A> for BufEncoder<CAP>

Source§

fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl<const CAP: usize> Hash for BufEncoder<CAP>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<const CAP: usize> PartialEq for BufEncoder<CAP>

Source§

fn eq(&self, other: &BufEncoder<CAP>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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<const CAP: usize> Eq for BufEncoder<CAP>

Source§

impl<const CAP: usize> StructuralPartialEq for BufEncoder<CAP>

Auto Trait Implementations§

§

impl<const CAP: usize> Freeze for BufEncoder<CAP>

§

impl<const CAP: usize> RefUnwindSafe for BufEncoder<CAP>

§

impl<const CAP: usize> Send for BufEncoder<CAP>

§

impl<const CAP: usize> Sync for BufEncoder<CAP>

§

impl<const CAP: usize> Unpin for BufEncoder<CAP>

§

impl<const CAP: usize> UnsafeUnpin for BufEncoder<CAP>

§

impl<const CAP: usize> UnwindSafe for BufEncoder<CAP>

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