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>
impl<const N: usize> Buffer<N>
Sourcepub const fn decode(src: &[u8]) -> Buffer<N>
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
NisN >= 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]);Sourcepub const fn encode_prefixed<const M: usize>(
src: &[u8; M],
prefix: char,
) -> Buffer<N>
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
NisN >= 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");Sourcepub const fn decode_prefixed(src: &[u8], prefix: char) -> Buffer<N>
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
NisN >= 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]);Sourcepub const fn encode_check<const M: usize>(
src: &[u8; M],
version: u8,
) -> Buffer<N>
Available on crate feature check only.
pub const fn encode_check<const M: usize>( src: &[u8; M], version: u8, ) -> Buffer<N>
check only.Encodes a byte array with a checksum into a Buffer.
§Panics
This method panics if:
- The buffer size
NisN >= 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");Sourcepub const fn decode_check(src: &[u8]) -> Buffer<N>
Available on crate feature check only.
pub const fn decode_check(src: &[u8]) -> Buffer<N>
check only.Decodes a slice of check-encoded bytes into a Buffer.
§Panics
This method panics if:
- The buffer size
Nis 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]);Sourcepub const fn encode_check_prefixed<const M: usize>(
src: &[u8; M],
prefix: char,
version: u8,
) -> Buffer<N>
Available on crate feature check only.
pub const fn encode_check_prefixed<const M: usize>( src: &[u8; M], prefix: char, version: u8, ) -> Buffer<N>
check only.Encodes a byte array with a checksum and prefix into a Buffer.
§Panics
This method panics if:
- The buffer size
Nis 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");Sourcepub const fn decode_check_prefixed(src: &[u8], prefix: char) -> Buffer<N>
Available on crate feature check only.
pub const fn decode_check_prefixed(src: &[u8], prefix: char) -> Buffer<N>
check only.Decodes a slice of prefixed check-encoded bytes into a Buffer.
§Panics
This method panics if:
- The buffer size
Nis 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]);Sourcepub const fn pos(&self) -> usize
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);Sourcepub const fn as_str(&self) -> &str
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");