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>
impl<const CAP: usize> BufEncoder<CAP>
Sourcepub fn new(case: Case) -> Self
pub fn new(case: Case) -> Self
Creates an empty BufEncoder that will encode bytes to hex characters in the given case.
Sourcepub fn put_bytes<I>(&mut self, bytes: I)
pub fn put_bytes<I>(&mut self, bytes: I)
Encodes bytes as hex and appends them to the buffer.
§Panics
The method panics if the bytes wouldn’t fit the buffer.
Sourcepub fn put_bytes_min<'a>(&mut self, bytes: &'a [u8]) -> &'a [u8] ⓘ
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
Sourcepub fn space_remaining(&self) -> usize
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>
impl<const CAP: usize> Clone for BufEncoder<CAP>
Source§fn clone(&self) -> BufEncoder<CAP>
fn clone(&self) -> BufEncoder<CAP>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<const CAP: usize> Debug for BufEncoder<CAP>
impl<const CAP: usize> Debug for BufEncoder<CAP>
Source§impl<const CAP: usize> Default for BufEncoder<CAP>
impl<const CAP: usize> Default for BufEncoder<CAP>
Source§impl<const CAP: usize, A: Borrow<u8>> Extend<A> for BufEncoder<CAP>
impl<const CAP: usize, A: Borrow<u8>> Extend<A> for BufEncoder<CAP>
Source§fn extend<T: IntoIterator<Item = A>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = A>>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<const CAP: usize, A: Borrow<u8>> FromIterator<A> for BufEncoder<CAP>
impl<const CAP: usize, A: Borrow<u8>> FromIterator<A> for BufEncoder<CAP>
Source§fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self
fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self
Source§impl<const CAP: usize> Hash for BufEncoder<CAP>
impl<const CAP: usize> Hash for BufEncoder<CAP>
Source§impl<const CAP: usize> PartialEq for BufEncoder<CAP>
impl<const CAP: usize> PartialEq for BufEncoder<CAP>
Source§fn eq(&self, other: &BufEncoder<CAP>) -> bool
fn eq(&self, other: &BufEncoder<CAP>) -> bool
self and other values to be equal, and is used by ==.