Skip to main content

AsciiStr

Struct AsciiStr 

Source
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>

Source

pub const WIRE_SIZE: usize = N

Size of the wire representation in bytes (always equal to the capacity N).

Source

pub const DEFAULT: Self

Source

pub const fn new() -> Self

Creates a new empty AsciiStr (all bytes zero).

Source

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
Source

pub fn try_from_str(s: &str) -> Result<Self, AsciiStrError>

Attempts to create an AsciiStr<N> from a string slice.

§Errors
Source

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
Source

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).

Source

pub fn as_bytes(&self) -> &[u8]

Returns the raw bytes of the stored string (excluding the trailing nul).

Source

pub fn len(&self) -> usize

Returns the current logical length of the string.

Source

pub const fn is_empty(&self) -> bool

Returns true if the string is empty.

Source

pub const fn capacity(&self) -> usize

Returns the fixed maximum capacity of this type (always N).

Source

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.

Source

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.

Source

pub fn from_fmt(args: Arguments<'_>) -> Self

Convenience: create from a format string (most ergonomic for errors)

Trait Implementations§

Source§

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

Source§

fn clone(&self) -> AsciiStr<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> Debug for AsciiStr<N>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<const N: usize> Default for AsciiStr<N>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<const N: usize> PartialEq for AsciiStr<N>

Source§

fn eq(&self, other: &AsciiStr<N>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<const N: usize> TryFrom<&str> for AsciiStr<N>

Source§

type Error = AsciiStrError

The type returned in the event of a conversion error.
Source§

fn try_from(s: &str) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<const N: usize> TryFrom<[u8; N]> for AsciiStr<N>

Source§

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

The type returned in the event of a conversion error.
Source§

impl<const N: usize> Write for AsciiStr<N>

Source§

fn write_str(&mut self, s: &str) -> Result

Writes a string slice into this writer, returning whether the write succeeded. Read more
1.1.0 · Source§

fn write_char(&mut self, c: char) -> Result<(), Error>

Writes a char into this writer, returning whether the write succeeded. Read more
1.0.0 · Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Glue for usage of the write! macro with implementors of this trait. Read more
Source§

impl<const N: usize> Copy for AsciiStr<N>

Source§

impl<const N: usize> Eq for AsciiStr<N>

Source§

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> 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, 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.