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>
impl<const N: usize> Encoding<N>
Sourcepub const BITS_PER_CHAR: u32
pub const BITS_PER_CHAR: u32
The number of bits encoded per character.
Sourcepub const fn new(alphabet: &[u8; N], pad: Option<u8>) -> Self
pub const fn new(alphabet: &[u8; N], pad: Option<u8>) -> Self
Creates a new encoding from an alphabet.
Sourcepub const fn with_lowercase(self) -> Self
pub const fn with_lowercase(self) -> Self
Add lowercase aliases for [A-Z] if present in alphabet.
Sourcepub const fn group_size_out(&self) -> usize
pub const fn group_size_out(&self) -> usize
Returns the output group size (number of characters per encoding group).
Sourcepub const fn group_size_in(&self) -> usize
pub const fn group_size_in(&self) -> usize
Returns the input group size (number of bytes per encoding group).
Sourcepub const fn bits_per_char(&self) -> u32
pub const fn bits_per_char(&self) -> u32
Returns the bits per character.
Sourcepub const fn encode_len(&self, src_len: usize) -> usize
pub const fn encode_len(&self, src_len: usize) -> usize
Returns the exact encoded length for a given source byte length.
Sourcepub const fn decode_len(&self, chars_len_upto_pad: usize) -> usize
pub const fn decode_len(&self, chars_len_upto_pad: usize) -> usize
Returns the decoded length for a given number of characters (excluding padding).
Sourcepub const fn encode(&self, value: u8) -> u8
pub const fn encode(&self, value: u8) -> u8
Encodes a single value (masked to valid range) to its character.
Sourcepub const fn decode(&self, ch: u8) -> Option<u8>
pub const fn decode(&self, ch: u8) -> Option<u8>
Decodes a single character to its value, returning None for invalid
characters.
Sourcepub const fn encode_mut(&self, src: &[u8], dst: &mut [u8]) -> usize
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.
Sourcepub const fn decode_mut(&self, src: &[u8], dst: &mut [u8]) -> Result<usize>
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.
Sourcepub const fn encode_n<const L: usize>(&self, src: &[u8; L]) -> ArrayStr<L>
pub const fn encode_n<const L: usize>(&self, src: &[u8; L]) -> ArrayStr<L>
Encodes a fixed-size byte array, returning an ArrayStr wrapper.
Sourcepub const fn decode_n<const L: usize>(&self, src: &[u8; L]) -> Option<Array<L>>
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.
Sourcepub const fn encode_writer<W: Write>(&self, inner: W) -> EncodeWriter<W, N> ⓘ
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();Sourcepub const fn decode_writer<W: Write>(&self, inner: W) -> DecodeWriter<W, N> ⓘ
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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