Struct Buffer

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

A fixed-size buffer for encoding or decoding Crockford’s Base32.

Buffer manages a fixed-size array of bytes and tracks the number of bytes written during encoding and decoding operations, and is primarily designed for use in a constant context.

§Examples

const INPUT: [u8; 3] = [42, 42, 42];
const ENC: Buffer<5> = Buffer::encode(&INPUT);
assert_eq!(ENC.as_str(), "2MAHA");
assert_eq!(ENC.pos(), 5);
const INPUT: [u8; 5] = *b"2MAHA";
const DEC: Buffer<5> = Buffer::decode(&INPUT);
assert_eq!(DEC.as_bytes(), [42, 42, 42]);
assert_eq!(DEC.pos(), 3);

Implementations§

Source§

impl<const N: usize> Buffer<N>

Source

pub const EMPTY: Self

An empty Buffer with no bytes written.

§Examples
const BUFFER: Buffer<1> = Buffer::<1>::EMPTY;
assert_eq!(BUFFER.as_bytes(), &[]);
assert_eq!(BUFFER.pos(), 0);
Source

pub const fn encode<const M: usize>(src: &[u8; M]) -> Buffer<N>

Encodes a byte array into a Buffer.

§Panics

This method panics if:

  • The buffer size N is N >= encoded_len(M).
§Examples
const INPUT: [u8; 3] = [42, 42, 42];
const ENC: Buffer<5> = Buffer::encode(&INPUT);
assert_eq!(ENC.as_str(), "2MAHA");
Source

pub const fn decode(src: &[u8]) -> Buffer<N>

Decodes a slice of encoded bytes into a Buffer.

§Panics

This method panics if:

  • The buffer size N is N >= decoded_len(src.len()).
  • The input contains invalid Crockford Base32 characters.
  • The input contains non-ASCII bytes.
§Examples
const INPUT: [u8; 5] = *b"2MAHA";
const DEC: Buffer<5> = Buffer::decode(&INPUT);
assert_eq!(DEC.as_bytes(), [42, 42, 42]);
Source

pub const fn encode_prefixed<const M: usize>( src: &[u8; M], prefix: char, ) -> Buffer<N>

Encodes a byte array with a prefix into a Buffer.

§Panics

This method panics if:

  • The buffer size N is N >= encoded_len(M).
  • The prefix is a non-ASCII character.
§Examples
const INPUT: [u8; 3] = [42, 42, 42];
const ENC: Buffer<6> = Buffer::encode_prefixed(&INPUT, 'S');
assert_eq!(ENC.as_str(), "S2MAHA");
Source

pub const fn decode_prefixed(src: &[u8], prefix: char) -> Buffer<N>

Decodes a slice of prefixed encoded bytes into a Buffer.

§Panics

This method panics if:

  • The buffer size N is N >= decoded_len(src.len() - 1).
  • The input contains invalid Crockford Base32 characters.
  • The input does not start with the expected prefix.
  • The input contains non-ASCII bytes.
  • The prefix is a non-ASCII character.
§Examples
const INPUT: [u8; 6] = *b"S2MAHA";
const DEC: Buffer<6> = Buffer::decode_prefixed(&INPUT, 'S');
assert_eq!(DEC.as_bytes(), [42, 42, 42]);
Source

pub const fn encode_check<const M: usize>( src: &[u8; M], version: u8, ) -> Buffer<N>

Available on crate feature check only.

Encodes a byte array with a checksum into a Buffer.

§Panics

This method panics if:

  • The buffer size N is N >= encoded_check_len(M).
  • The version is greater than or equal to 32.
§Examples
const INPUT: [u8; 3] = [42, 42, 42];
const ENC: Buffer<13> = Buffer::encode_check(&INPUT, 0);
assert_eq!(ENC.as_str(), "0AHA59B9201Z");
Source

pub const fn decode_check(src: &[u8]) -> Buffer<N>

Available on crate feature check only.

Decodes a slice of check-encoded bytes into a Buffer.

§Panics

This method panics if:

  • The buffer size N is insufficient for the decoded output.
  • The input is shorter than 2 bytes.
  • The input contains invalid Crockford Base32 characters.
  • The input contains non-ASCII bytes.
  • The version is greater than or equal to 32.
  • The computed checksum does not match the embedded checksum.
§Examples
const INPUT: [u8; 12] = *b"0AHA59B9201Z";
const DEC: Buffer<12> = Buffer::decode_check(&INPUT);
assert_eq!(DEC.as_bytes(), [42, 42, 42]);
Source

pub const fn encode_check_prefixed<const M: usize>( src: &[u8; M], prefix: char, version: u8, ) -> Buffer<N>

Available on crate feature check only.

Encodes a byte array with a checksum and prefix into a Buffer.

§Panics

This method panics if:

  • The buffer size N is insufficient for the encoded output and prefix.
  • The prefix is not an ASCII character.
  • The version is greater than or equal to 32.
§Examples
const INPUT: [u8; 3] = [42, 42, 42];
const ENC: Buffer<14> = Buffer::encode_check_prefixed(&INPUT, 'S', 0);
assert_eq!(ENC.as_str(), "S0AHA59B9201Z");
Source

pub const fn decode_check_prefixed(src: &[u8], prefix: char) -> Buffer<N>

Available on crate feature check only.

Decodes a slice of prefixed check-encoded bytes into a Buffer.

§Panics

This method panics if:

  • The buffer size N is insufficient for the decoded output.
  • The input is shorter than 3 bytes.
  • The prefix is not an ASCII character.
  • The input does not start with the expected prefix.
  • The input contains invalid Crockford Base32 characters.
  • The input contains non-ASCII bytes.
  • The version version is greater than or equal to 32.
  • The computed checksum does not match the embedded checksum.
§Examples
const INPUT: [u8; 13] = *b"S0AHA59B9201Z";
const DEC: Buffer<13> = Buffer::decode_check_prefixed(&INPUT, 'S');
assert_eq!(DEC.as_bytes(), [42, 42, 42]);
Source

pub const fn pos(&self) -> usize

Returns the number of bytes written to the buffer.

§Examples
const INPUT: [u8; 3] = [42, 42, 42];
const ENC: Buffer<5> = Buffer::encode(&INPUT);
assert_eq!(ENC.pos(), 5);
Source

pub const fn as_str(&self) -> &str

Returns a string slice of the written bytes.

§Examples
const INPUT: [u8; 3] = [42, 42, 42];
const ENC: Buffer<5> = Buffer::encode(&INPUT);
assert_eq!(ENC.as_str(), "2MAHA");
Source

pub const fn as_bytes(&self) -> &[u8]

Returns a slice of the written bytes.

§Examples
const INPUT: [u8; 3] = [42, 42, 42];
const ENC: Buffer<5> = Buffer::encode(&INPUT);
assert_eq!(ENC.as_bytes(), b"2MAHA");
Source

pub const fn as_ptr(&self) -> *const u8

Returns a raw pointer to the buffer’s data.

§Examples
let buffer = Buffer::<10>::EMPTY;
let ptr = buffer.as_ptr();

Trait Implementations§

Source§

impl<const N: usize> Clone for Buffer<N>

Source§

fn clone(&self) -> Buffer<N>

Returns a copy 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<const N: usize> Debug for Buffer<N>

Source§

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

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

impl<const N: usize> Hash for Buffer<N>

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 N: usize> Ord for Buffer<N>

Source§

fn cmp(&self, other: &Buffer<N>) -> 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<const N: usize> PartialEq for Buffer<N>

Source§

fn eq(&self, other: &Buffer<N>) -> 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<const N: usize> PartialOrd for Buffer<N>

Source§

fn partial_cmp(&self, other: &Buffer<N>) -> 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<const N: usize> Copy for Buffer<N>

Source§

impl<const N: usize> Eq for Buffer<N>

Source§

impl<const N: usize> StructuralPartialEq for Buffer<N>

Auto Trait Implementations§

§

impl<const N: usize> Freeze for Buffer<N>

§

impl<const N: usize> RefUnwindSafe for Buffer<N>

§

impl<const N: usize> Send for Buffer<N>

§

impl<const N: usize> Sync for Buffer<N>

§

impl<const N: usize> Unpin for Buffer<N>

§

impl<const N: usize> UnwindSafe for Buffer<N>

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.