pub struct LiteStr<const N: usize> {
pub bytes: [u8; N],
}Expand description
A fixed-capacity, stack-allocated buffer that can hold a UTF-8 string.
LiteStr<N> stores its content in a [u8; N] array using C-style nul
termination. The logical length is determined by the position of the first
b'\0' byte (or N if the buffer is completely filled without a nul).
This type performs no validation during construction. UTF-8 validity is
only checked when the content is accessed via [as_str], Debug, or
serialization.
Both [new] and [from_bytes] silently truncate input that exceeds the
capacity N. This type is intentionally minimal because each LiteStr<N>
is monomorphized independently.
§.len()
- Byte length: Use
as_bytes().len() - Unicode character count: Use
as_str().unwrap().len()
Fields§
§bytes: [u8; N]Implementations§
Source§impl<const N: usize> LiteStr<N>
impl<const N: usize> LiteStr<N>
pub const SIZE: usize = N
Sourcepub fn new(s: &str) -> Self
pub fn new(s: &str) -> Self
Creates a new LiteStr from a &str.
If the input is longer than N bytes, it is truncated at the nearest
valid UTF-8 boundary.
Sourcepub fn as_str(&self) -> Result<&str, LiteStrErr>
pub fn as_str(&self) -> Result<&str, LiteStrErr>
Returns the content as a &str, validating that it is well-formed UTF-8.
Finds the first nul byte and uses that as the end of the str, or if
there isn’t a nul byte then uses the whole len N.
Sourcepub fn from_bytes(bytes: &[u8]) -> Self
pub fn from_bytes(bytes: &[u8]) -> Self
Creates a LiteStr<N> from a byte slice.
Copies up to N bytes from the input and zero-fills the remainder.
If bytes.len() > N, the input is silently truncated.
No UTF-8 validation is performed.
Trait Implementations§
impl<const N: usize> Copy for LiteStr<N>
Source§impl<'de, const N: usize> Deserialize<'de> for LiteStr<N>
Available on crate feature serde only.
impl<'de, const N: usize> Deserialize<'de> for LiteStr<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>,
impl<const N: usize> Eq for LiteStr<N>
Source§impl<const N: usize> PartialEq for LiteStr<N>
impl<const N: usize> PartialEq for LiteStr<N>
impl<const N: usize> StructuralPartialEq for LiteStr<N>
Auto Trait Implementations§
impl<const N: usize> Freeze for LiteStr<N>
impl<const N: usize> RefUnwindSafe for LiteStr<N>
impl<const N: usize> Send for LiteStr<N>
impl<const N: usize> Sync for LiteStr<N>
impl<const N: usize> Unpin for LiteStr<N>
impl<const N: usize> UnsafeUnpin for LiteStr<N>
impl<const N: usize> UnwindSafe for LiteStr<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,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
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.