pub struct AsciiStr<const N: usize> { /* private fields */ }Expand description
Fixed-capacity, stack-only ASCII string stored in a single [u8; N] array.
The string is stored as raw bytes. Its logical length is determined at
runtime by the position of the first nul byte (b'\0'). All bytes after
the string content are guaranteed to be zero.
Implementations§
Source§impl<const N: usize> AsciiStr<N>
impl<const N: usize> AsciiStr<N>
Sourcepub const WIRE_SIZE: usize = N
pub const WIRE_SIZE: usize = N
Size of the wire representation in bytes (always equal to the capacity N).
pub const DEFAULT: Self
Sourcepub fn try_from_filled_buffer(buffer: [u8; N]) -> Result<Self, AsciiStrError>
pub fn try_from_filled_buffer(buffer: [u8; N]) -> Result<Self, AsciiStrError>
Attempts to create an AsciiStr<N> from a raw byte buffer safely.
This is the public, validated counterpart to the internal
from_filled_buffer.
It performs full validation:
- All bytes must be valid ASCII.
- Every byte after the first
b'\0'must be zero (preserves the nul-terminated + trailing-zeros invariant).
Use this when you have untrusted or externally-supplied bytes
(network packets, C strftime output, user input, etc.).
This method (and the entire public API) is completely panic-free.
All fallible operations return Result or Option.
§Errors
AsciiStrError::InvalidAsciiif the buffer contains non-ASCII bytes.AsciiStrError::CorruptedDataif bytes after the first nul are not all zero (violates the representation invariant).
Sourcepub fn try_from_str(s: &str) -> Result<Self, AsciiStrError>
pub fn try_from_str(s: &str) -> Result<Self, AsciiStrError>
Attempts to create an AsciiStr<N> from a string slice.
§Errors
AsciiStrError::InvalidAsciiif the input is not ASCII.AsciiStrError::TooLongif the input exceeds capacityN.
Sourcepub fn try_from_str_upper(s: &str) -> Result<Self, AsciiStrError>
pub fn try_from_str_upper(s: &str) -> Result<Self, AsciiStrError>
Attempts to create an AsciiStr<N> from a string slice, uppercasing the input.
This is a convenience wrapper around try_from_str
that converts the input to ASCII uppercase before storing it.
§Errors
AsciiStrError::InvalidAsciiif the input is not ASCII.AsciiStrError::TooLongif the input exceeds capacityN.
Sourcepub fn as_str(&self) -> Result<&str, AsciiStrError>
pub fn as_str(&self) -> Result<&str, AsciiStrError>
Returns the stored string as &str.
The length is computed by locating the first nul byte.
§Errors
Returns AsciiStrError::CorruptedData only if the internal data
has become invalid UTF-8 (unreachable via safe constructors).
Sourcepub fn as_bytes(&self) -> &[u8]
pub fn as_bytes(&self) -> &[u8]
Returns the raw bytes of the stored string (excluding the trailing nul).
Sourcepub const fn capacity(&self) -> usize
pub const fn capacity(&self) -> usize
Returns the fixed maximum capacity of this type (always N).
Sourcepub fn from_str_truncate(s: &str) -> Self
pub fn from_str_truncate(s: &str) -> Self
Creates an AsciiStr from a &str, truncating if it exceeds capacity N.
Non-ASCII characters are allowed.
Sourcepub fn from_display<T: Display>(value: T) -> Self
pub fn from_display<T: Display>(value: T) -> Self
Creates an AsciiStr from any type that implements Display.
The output is truncated if it exceeds capacity N.
Very useful for embedding numbers, paths, etc. into errors.
Trait Implementations§
Source§impl<const N: usize> PartialEq for AsciiStr<N>
impl<const N: usize> PartialEq for AsciiStr<N>
Source§impl<const N: usize> TryFrom<[u8; N]> for AsciiStr<N>
impl<const N: usize> TryFrom<[u8; N]> for AsciiStr<N>
Source§fn try_from(buffer: [u8; N]) -> Result<Self, Self::Error>
fn try_from(buffer: [u8; N]) -> Result<Self, Self::Error>
Attempts to create an AsciiStr<N> from a filled buffer.
This is the idiomatic, completely panic-free way to construct
from a byte array using the ? operator or .unwrap_or_else().