Skip to main content

Base

Struct Base 

Source
pub struct Base<const RADIX: usize, const LUT: bool, const PAD: bool, const CASE: bool, CODE> { /* private fields */ }
Expand description

๐Ÿฅก A compile-time configurable radix-based encoding scheme.


๐Ÿ“data/codec


This struct defines the core behavior for various base encodings, including Base16, Base32 and Base64.

It converts binary โ†” text for data transmission/storage.

It is configurable via const generics to support different encoding behaviors, such as lookup tables, padding, and case insensitivity.

ยงType Parameters

  • RADIX: The numeric radix of the encoding (e.g., 16, 32, 64).
  • LUT: Whether to use a lookup table (true) for fast decoding or a linear search (false) using less memory.
  • PAD: Whether to use padding (=) for encoding output (used in Base32/Base64).
  • CASE: Whether decoding should be case-insensitive (e.g., true for Crockford Base32).
  • CODE: A marker type defining the specific encoding scheme (e.g., Rfc4648, Crockford).

Implementationsยง

Sourceยง

impl<const PAD: bool, const CASE: bool> Base<16, false, PAD, CASE, Rfc4648>

Source

pub const fn encoded_len(input_len: usize) -> usize

Returns the required output buffer size for encoding input_len bytes.

Source

pub const fn encoded_len_padded(input_len: usize) -> usize

Returns the required output buffer size for encoding input_len bytes.

Source

pub const fn decoded_len(input_len: usize) -> usize

Returns the required output buffer size for decoding input_len Base32 characters.

Source

pub const fn decoded_len_stripped(input: &[u8]) -> usize

Returns the required output buffer size for decoding input_len Base32 characters.

Strips the padding. Otherwise decoded_len will calculate for the worst case.

Source

pub const fn decode_from_slice(input: &[u8], output: &mut [u8]) -> Option<usize> โ“˜

Decodes input into output, returning the number of bytes written. Uses a LUT when DEC_TABLE = true, otherwise falls back to linear search.

Returns None if it finds an invalid byte.

ยงPanics

Panics if output is too small.

Source

pub const fn encode_to_slice(input: &[u8], output: &mut [u8]) -> usize

Encodes input into output, returning the number of bytes written.

ยงPanics

Panics if output is too small.

Sourceยง

impl<const PAD: bool, const CASE: bool> Base<16, true, PAD, CASE, Rfc4648>

Source

pub const fn encoded_len(input_len: usize) -> usize

Returns the required output buffer size for encoding input_len bytes.

Source

pub const fn encoded_len_padded(input_len: usize) -> usize

Returns the required output buffer size for encoding input_len bytes.

Source

pub const fn decoded_len(input_len: usize) -> usize

Returns the required output buffer size for decoding input_len Base32 characters.

Source

pub const fn decoded_len_stripped(input: &[u8]) -> usize

Returns the required output buffer size for decoding input_len Base32 characters.

Strips the padding. Otherwise decoded_len will calculate for the worst case.

Source

pub const fn decode_from_slice(input: &[u8], output: &mut [u8]) -> Option<usize> โ“˜

Decodes input into output, returning the number of bytes written. Uses a LUT when DEC_TABLE = true, otherwise falls back to linear search.

Returns None if it finds an invalid byte.

ยงPanics

Panics if output is too small.

Source

pub const fn encode_to_slice(input: &[u8], output: &mut [u8]) -> usize

Encodes input into output, returning the number of bytes written.

ยงPanics

Panics if output is too small.

Sourceยง

impl<const PAD: bool, const CASE: bool> Base<32, false, PAD, CASE, Rfc4648>

Source

pub const fn encoded_len(input_len: usize) -> usize

Returns the required output buffer size for encoding input_len bytes.

Source

pub const fn encoded_len_padded(input_len: usize) -> usize

Returns the required output buffer size for encoding input_len bytes.

Source

pub const fn decoded_len(input_len: usize) -> usize

Returns the required output buffer size for decoding input_len Base32 characters.

Source

pub const fn decoded_len_stripped(input: &[u8]) -> usize

Returns the required output buffer size for decoding input_len Base32 characters.

Strips the padding. Otherwise decoded_len will calculate for the worst case.

Source

pub const fn decode_from_slice(input: &[u8], output: &mut [u8]) -> Option<usize> โ“˜

Decodes input into output, returning the number of bytes written. Uses a LUT when DEC_TABLE = true, otherwise falls back to linear search.

Returns None if it finds an invalid byte.

ยงPanics

Panics if output is too small.

Source

pub const fn encode_to_slice(input: &[u8], output: &mut [u8]) -> usize

Encodes input into output, returning the number of bytes written.

ยงPanics

Panics if output is too small.

Sourceยง

impl<const PAD: bool, const CASE: bool> Base<32, true, PAD, CASE, Rfc4648>

Source

pub const fn encoded_len(input_len: usize) -> usize

Returns the required output buffer size for encoding input_len bytes.

Source

pub const fn encoded_len_padded(input_len: usize) -> usize

Returns the required output buffer size for encoding input_len bytes.

Source

pub const fn decoded_len(input_len: usize) -> usize

Returns the required output buffer size for decoding input_len Base32 characters.

Source

pub const fn decoded_len_stripped(input: &[u8]) -> usize

Returns the required output buffer size for decoding input_len Base32 characters.

Strips the padding. Otherwise decoded_len will calculate for the worst case.

Source

pub const fn decode_from_slice(input: &[u8], output: &mut [u8]) -> Option<usize> โ“˜

Decodes input into output, returning the number of bytes written. Uses a LUT when DEC_TABLE = true, otherwise falls back to linear search.

Returns None if it finds an invalid byte.

ยงPanics

Panics if output is too small.

Source

pub const fn encode_to_slice(input: &[u8], output: &mut [u8]) -> usize

Encodes input into output, returning the number of bytes written.

ยงPanics

Panics if output is too small.

Sourceยง

impl<const PAD: bool, const CASE: bool> Base<32, false, PAD, CASE, Crockford>

Source

pub const fn encoded_len(input_len: usize) -> usize

Returns the required output buffer size for encoding input_len bytes.

Source

pub const fn encoded_len_padded(input_len: usize) -> usize

Returns the required output buffer size for encoding input_len bytes.

Source

pub const fn decoded_len(input_len: usize) -> usize

Returns the required output buffer size for decoding input_len Base32 characters.

Source

pub const fn decoded_len_stripped(input: &[u8]) -> usize

Returns the required output buffer size for decoding input_len Base32 characters.

Strips the padding. Otherwise decoded_len will calculate for the worst case.

Source

pub const fn decode_from_slice(input: &[u8], output: &mut [u8]) -> Option<usize> โ“˜

Decodes input into output, returning the number of bytes written. Uses a LUT when DEC_TABLE = true, otherwise falls back to linear search.

Returns None if it finds an invalid byte.

ยงPanics

Panics if output is too small.

Source

pub const fn encode_to_slice(input: &[u8], output: &mut [u8]) -> usize

Encodes input into output, returning the number of bytes written.

ยงPanics

Panics if output is too small.

Sourceยง

impl<const PAD: bool, const CASE: bool> Base<32, true, PAD, CASE, Crockford>

Source

pub const fn encoded_len(input_len: usize) -> usize

Returns the required output buffer size for encoding input_len bytes.

Source

pub const fn encoded_len_padded(input_len: usize) -> usize

Returns the required output buffer size for encoding input_len bytes.

Source

pub const fn decoded_len(input_len: usize) -> usize

Returns the required output buffer size for decoding input_len Base32 characters.

Source

pub const fn decoded_len_stripped(input: &[u8]) -> usize

Returns the required output buffer size for decoding input_len Base32 characters.

Strips the padding. Otherwise decoded_len will calculate for the worst case.

Source

pub const fn decode_from_slice(input: &[u8], output: &mut [u8]) -> Option<usize> โ“˜

Decodes input into output, returning the number of bytes written. Uses a LUT when DEC_TABLE = true, otherwise falls back to linear search.

Returns None if it finds an invalid byte.

ยงPanics

Panics if output is too small.

Source

pub const fn encode_to_slice(input: &[u8], output: &mut [u8]) -> usize

Encodes input into output, returning the number of bytes written.

ยงPanics

Panics if output is too small.

Sourceยง

impl<const PAD: bool, const CASE: bool> Base<32, false, PAD, CASE, Rfc4648Hex>

Source

pub const fn encoded_len(input_len: usize) -> usize

Returns the required output buffer size for encoding input_len bytes.

Source

pub const fn encoded_len_padded(input_len: usize) -> usize

Returns the required output buffer size for encoding input_len bytes.

Source

pub const fn decoded_len(input_len: usize) -> usize

Returns the required output buffer size for decoding input_len Base32 characters.

Source

pub const fn decoded_len_stripped(input: &[u8]) -> usize

Returns the required output buffer size for decoding input_len Base32 characters.

Strips the padding. Otherwise decoded_len will calculate for the worst case.

Source

pub const fn decode_from_slice(input: &[u8], output: &mut [u8]) -> Option<usize> โ“˜

Decodes input into output, returning the number of bytes written. Uses a LUT when DEC_TABLE = true, otherwise falls back to linear search.

Returns None if it finds an invalid byte.

ยงPanics

Panics if output is too small.

Source

pub const fn encode_to_slice(input: &[u8], output: &mut [u8]) -> usize

Encodes input into output, returning the number of bytes written.

ยงPanics

Panics if output is too small.

Sourceยง

impl<const PAD: bool, const CASE: bool> Base<32, true, PAD, CASE, Rfc4648Hex>

Source

pub const fn encoded_len(input_len: usize) -> usize

Returns the required output buffer size for encoding input_len bytes.

Source

pub const fn encoded_len_padded(input_len: usize) -> usize

Returns the required output buffer size for encoding input_len bytes.

Source

pub const fn decoded_len(input_len: usize) -> usize

Returns the required output buffer size for decoding input_len Base32 characters.

Source

pub const fn decoded_len_stripped(input: &[u8]) -> usize

Returns the required output buffer size for decoding input_len Base32 characters.

Strips the padding. Otherwise decoded_len will calculate for the worst case.

Source

pub const fn decode_from_slice(input: &[u8], output: &mut [u8]) -> Option<usize> โ“˜

Decodes input into output, returning the number of bytes written. Uses a LUT when DEC_TABLE = true, otherwise falls back to linear search.

Returns None if it finds an invalid byte.

ยงPanics

Panics if output is too small.

Source

pub const fn encode_to_slice(input: &[u8], output: &mut [u8]) -> usize

Encodes input into output, returning the number of bytes written.

ยงPanics

Panics if output is too small.

Sourceยง

impl<const PAD: bool, const CASE: bool> Base<64, false, PAD, CASE, Rfc4648>

Source

pub const fn encoded_len(input_len: usize) -> usize

Returns the required output buffer size for encoding input_len bytes.

Source

pub const fn encoded_len_padded(input_len: usize) -> usize

Returns the required output buffer size for encoding input_len bytes.

Source

pub const fn decoded_len(input_len: usize) -> usize

Returns the required output buffer size for decoding input_len Base32 characters.

Source

pub const fn decoded_len_stripped(input: &[u8]) -> usize

Returns the required output buffer size for decoding input_len Base32 characters.

Strips the padding. Otherwise decoded_len will calculate for the worst case.

Source

pub const fn decode_from_slice(input: &[u8], output: &mut [u8]) -> Option<usize> โ“˜

Decodes input into output, returning the number of bytes written. Uses a LUT when DEC_TABLE = true, otherwise falls back to linear search.

Returns None if it finds an invalid byte.

ยงPanics

Panics if output is too small.

Source

pub const fn encode_to_slice(input: &[u8], output: &mut [u8]) -> usize

Encodes input into output, returning the number of bytes written.

ยงPanics

Panics if output is too small.

Sourceยง

impl<const PAD: bool, const CASE: bool> Base<64, true, PAD, CASE, Rfc4648>

Source

pub const fn encoded_len(input_len: usize) -> usize

Returns the required output buffer size for encoding input_len bytes.

Source

pub const fn encoded_len_padded(input_len: usize) -> usize

Returns the required output buffer size for encoding input_len bytes.

Source

pub const fn decoded_len(input_len: usize) -> usize

Returns the required output buffer size for decoding input_len Base32 characters.

Source

pub const fn decoded_len_stripped(input: &[u8]) -> usize

Returns the required output buffer size for decoding input_len Base32 characters.

Strips the padding. Otherwise decoded_len will calculate for the worst case.

Source

pub const fn decode_from_slice(input: &[u8], output: &mut [u8]) -> Option<usize> โ“˜

Decodes input into output, returning the number of bytes written. Uses a LUT when DEC_TABLE = true, otherwise falls back to linear search.

Returns None if it finds an invalid byte.

ยงPanics

Panics if output is too small.

Source

pub const fn encode_to_slice(input: &[u8], output: &mut [u8]) -> usize

Encodes input into output, returning the number of bytes written.

ยงPanics

Panics if output is too small.

Sourceยง

impl<const RADIX: usize, const LUT: bool, const PAD: bool, const CASE: bool, CODE> Base<RADIX, LUT, PAD, CASE, CODE>

ยงmisc. methods

Source

pub const fn new() -> Self

Returns a new Base with the chosen generics.

Source

pub const fn with_radix<const NEW_RADIX: usize>( self, ) -> Base<NEW_RADIX, LUT, PAD, CASE, CODE>

Change the radix while keeping everything else the same.

Source

pub const fn with_lut<const NEW_LUT: bool>( self, ) -> Base<RADIX, NEW_LUT, PAD, CASE, CODE>

Toggle LUT usage while keeping everything else the same.

Source

pub const fn with_pad<const NEW_PAD: bool>( self, ) -> Base<RADIX, LUT, NEW_PAD, CASE, CODE>

Toggle padding while keeping everything else the same.

Source

pub const fn with_case<const NEW_CASE: bool>( self, ) -> Base<RADIX, LUT, PAD, NEW_CASE, CODE>

Change the case sensitivity while keeping everything else the same.

Source

pub const fn with_encoding<NewCode>( self, ) -> Base<RADIX, LUT, PAD, CASE, NewCode>

Change the encoding scheme while keeping everything else the same.

Trait Implementationsยง

Sourceยง

impl<const RADIX: usize, const LUT: bool, const PAD: bool, const CASE: bool, CODE: Clone> Clone for Base<RADIX, LUT, PAD, CASE, CODE>

Sourceยง

fn clone(&self) -> Self

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 RADIX: usize, const LUT: bool, const PAD: bool, const CASE: bool, CODE> ConstInitCore for Base<RADIX, LUT, PAD, CASE, CODE>

Sourceยง

const INIT: Self

Returns the compile-time โ€œinitial valueโ€ for a type.
Sourceยง

impl<const RADIX: usize, const LUT: bool, const PAD: bool, const CASE: bool, CODE: Copy> Copy for Base<RADIX, LUT, PAD, CASE, CODE>

Sourceยง

impl<const RADIX: usize, const LUT: bool, const PAD: bool, const CASE: bool, CODE: Debug> Debug for Base<RADIX, LUT, PAD, CASE, CODE>

Sourceยง

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

Formats the value using the given formatter. Read more
Sourceยง

impl<const RADIX: usize, const LUT: bool, const PAD: bool, const CASE: bool, CODE> Default for Base<RADIX, LUT, PAD, CASE, CODE>

Sourceยง

fn default() -> Self

Returns the โ€œdefault valueโ€ for a type. Read more

Auto Trait Implementationsยง

ยง

impl<const RADIX: usize, const LUT: bool, const PAD: bool, const CASE: bool, CODE> Freeze for Base<RADIX, LUT, PAD, CASE, CODE>
where PhantomData<CODE>: Freeze,

ยง

impl<const RADIX: usize, const LUT: bool, const PAD: bool, const CASE: bool, CODE> RefUnwindSafe for Base<RADIX, LUT, PAD, CASE, CODE>

ยง

impl<const RADIX: usize, const LUT: bool, const PAD: bool, const CASE: bool, CODE> Send for Base<RADIX, LUT, PAD, CASE, CODE>
where PhantomData<CODE>: Send,

ยง

impl<const RADIX: usize, const LUT: bool, const PAD: bool, const CASE: bool, CODE> Sync for Base<RADIX, LUT, PAD, CASE, CODE>
where PhantomData<CODE>: Sync,

ยง

impl<const RADIX: usize, const LUT: bool, const PAD: bool, const CASE: bool, CODE> Unpin for Base<RADIX, LUT, PAD, CASE, CODE>
where PhantomData<CODE>: Unpin,

ยง

impl<const RADIX: usize, const LUT: bool, const PAD: bool, const CASE: bool, CODE> UnsafeUnpin for Base<RADIX, LUT, PAD, CASE, CODE>
where PhantomData<CODE>: UnsafeUnpin,

ยง

impl<const RADIX: usize, const LUT: bool, const PAD: bool, const CASE: bool, CODE> UnwindSafe for Base<RADIX, LUT, PAD, CASE, CODE>
where PhantomData<CODE>: UnwindSafe,

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> ByteSized for T

Sourceยง

const BYTE_ALIGN: usize = _

The alignment of this type in bytes.
Sourceยง

const BYTE_SIZE: usize = _

The size of this type in bytes.
Sourceยง

fn byte_align(&self) -> usize

Returns the alignment of this type in bytes.
Sourceยง

fn byte_size(&self) -> usize

Returns the size of this type in bytes. Read more
Sourceยง

fn ptr_size_ratio(&self) -> [usize; 2]

Returns the size ratio between Ptr::BYTES and BYTE_SIZE. 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> Hook for T

Sourceยง

fn hook<F>(self, f: F) -> Self
where F: FnOnce(&mut Self),

Hooks a mutation step into the value and returns it. Read more
Sourceยง

fn tap<F>(self, f: F) -> Self
where F: FnOnce(&Self),

Taps into the value for observation and returns it unchanged. Read more
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, R> Morph<R> for T
where T: ?Sized,

Sourceยง

fn morph<F>(self, f: F) -> R
where F: FnOnce(Self) -> R, Self: Sized,

Morphs the value into a new one and returns it. Read more
Sourceยง

fn morph_ref<F>(&self, f: F) -> R
where F: FnOnce(&Self) -> R,

Morphs the value by shared reference and returns the result. Read more
Sourceยง

fn morph_mut<F>(&mut self, f: F) -> R
where F: FnOnce(&mut Self) -> R,

Morphs the value by exclusive reference and returns the result. Read more
Sourceยง

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

Sourceยง

type Error = !

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

fn try_from(value: U) -> Result<T, !> โ“˜

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.