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 to_wire_bytes(&self) -> [u8; N]
pub fn to_wire_bytes(&self) -> [u8; N]
Serializes this AsciiStr into a fixed-size byte array.
The entire internal buffer is written (including trailing zeros after the logical string content). This preserves the exact representation.
Sourcepub fn from_wire_bytes(bytes: &[u8]) -> Option<Self>
pub fn from_wire_bytes(bytes: &[u8]) -> Option<Self>
Deserializes an AsciiStr<N> from exactly N bytes.
The input must be valid ASCII. Any bytes after the first nul byte must be zero (as required by the type invariant).
Returns None if the input is not valid ASCII or violates the
internal representation rules.
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<'de, const N: usize> Deserialize<'de> for AsciiStr<N>
Available on crate feature serde only.
impl<'de, const N: usize> Deserialize<'de> for AsciiStr<N>
serde only.Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
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().
Source§type Error = AsciiStrError
type Error = AsciiStrError
Source§impl<const N: usize> Write for AsciiStr<N>
impl<const N: usize> Write for AsciiStr<N>
impl<const N: usize> Copy for AsciiStr<N>
impl<const N: usize> Eq for AsciiStr<N>
impl<const N: usize> StructuralPartialEq for AsciiStr<N>
Auto Trait Implementations§
impl<const N: usize> Freeze for AsciiStr<N>
impl<const N: usize> RefUnwindSafe for AsciiStr<N>
impl<const N: usize> Send for AsciiStr<N>
impl<const N: usize> Sync for AsciiStr<N>
impl<const N: usize> Unpin for AsciiStr<N>
impl<const N: usize> UnsafeUnpin for AsciiStr<N>
impl<const N: usize> UnwindSafe for AsciiStr<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<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.