Skip to main content

Encoding

Struct Encoding 

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

A compile-time dictionary for base-N encoding/decoding.

§Type Parameters

  • N: The base (must be a power of 2 and <= 256)
  • PAD: Optional padding character (0 means no padding)

Implementations§

Source§

impl<const N: usize> Encoding<N>

Source

pub const BITS_PER_CHAR: u32

The number of bits encoded per character.

Source

pub const GROUP_IN: usize

The input group size (number of bytes per encoding group).

Source

pub const GROUP_OUT: usize

The output group size (number of characters per encoding group).

Source

pub const MASK: u8

The bit mask for extracting character values.

Source

pub const fn new(alphabet: &[u8; N], pad: Option<u8>) -> Self

Creates a new encoding from an alphabet.

Source

pub const fn with_lowercase(self) -> Self

Add lowercase aliases for [A-Z] if present in alphabet.

Source

pub const fn mask(&self) -> u8

Returns the bit mask for extracting character values.

Source

pub const fn group_size_out(&self) -> usize

Returns the output group size (number of characters per encoding group).

Source

pub const fn group_size_in(&self) -> usize

Returns the input group size (number of bytes per encoding group).

Source

pub const fn padding(&self) -> Option<u8>

Returns the padding character if padding is enabled.

Source

pub const fn bits_per_char(&self) -> u32

Returns the bits per character.

Source

pub const fn encode_len(&self, src_len: usize) -> usize

Returns the exact encoded length for a given source byte length.

Source

pub const fn decode_len(&self, chars_len_upto_pad: usize) -> usize

Returns the decoded length for a given number of characters (excluding padding).

Source

pub const fn encode(&self, value: u8) -> u8

Encodes a single value (masked to valid range) to its character.

Source

pub const fn decode(&self, ch: u8) -> Option<u8>

Decodes a single character to its value, returning None for invalid characters.

Source

pub const fn encode_mut(&self, src: &[u8], dst: &mut [u8]) -> usize

Encodes bytes into a mutable buffer, returning the number of characters written. Routes to optimized implementation at runtime or const fallback at compile time.

Source

pub const fn decode_mut(&self, src: &[u8], dst: &mut [u8]) -> Result<usize>

Decodes characters into a mutable buffer, returning the number of bytes written. Routes to optimized implementation at runtime or const fallback at compile time.

Source

pub const fn encode_n<const L: usize>(&self, src: &[u8; L]) -> ArrayStr<L>

Encodes a fixed-size byte array, returning an ArrayStr wrapper.

Source

pub const fn decode_n<const L: usize>(&self, src: &[u8; L]) -> Option<Array<L>>

Decodes a fixed-size character array, returning an Array wrapper.

Source

pub const fn encode_writer<W: Write>(&self, inner: W) -> EncodeWriter<W, N>

Creates an encoding writer that wraps an io::Write and encodes bytes before writing them.

The returned writer buffers input, encodes it using this encoding, and writes the encoded output to the inner writer.

§Examples
use std::io::Write;

use omp_core::base64;

let mut output = Vec::new();
let mut writer = base64::STD.encode_writer(&mut output);
writer.write_all(b"Hello").unwrap();
writer.flush().unwrap();
Source

pub const fn decode_writer<W: Write>(&self, inner: W) -> DecodeWriter<W, N>

Creates a decoding writer that wraps an io::Write and decodes bytes before writing them.

The returned writer buffers encoded input, decodes it using this encoding, and writes the decoded output to the inner writer.

§Examples
use std::io::Write;

use omp_core::base64;

let mut output = Vec::new();
{
	let mut writer = base64::STD.decode_writer(&mut output);
	writer.write_all(b"SGVsbG8=").unwrap();
	writer.flush().unwrap();
}
assert_eq!(output, b"Hello");

Trait Implementations§

Source§

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

Source§

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

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 N: usize> Copy for Encoding<N>

Source§

impl<const N: usize> Debug for Encoding<N>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<const N: usize> UnsafeUnpin for Encoding<N>

§

impl<const N: usize> UnwindSafe for Encoding<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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. 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.