pub struct TinyStr<T: TinyStrInt>(/* private fields */);Expand description
A short string packed into a big-endian NonZero integer.
TinyStr stores up to N bytes in a single value that fits in a register, enabling
single-instruction equality and ordering comparisons.
The big-endian layout means normal integer comparisons result in lexicographic order.
Strings are NUL-padded. Trailing NUL bytes are indistinguishable from padding, so two inputs only differing in trailing NULs will be equal.
Empty strings or strings containing all NUL bytes are not representable.
§Examples
let s4 = TinyStr4::new(b"abc").unwrap();
assert_eq!(s4.len(), 3);
assert_eq!(format!("{s4}"), "abc");
assert!(s4 < TinyStr4::from_const(b"abd"));
assert!(s4 > TinyStr4::from_const(b"abb"));
let s8 = TinyStr8::new(b"abcdefg").unwrap();
assert_eq!(s8.len(), 7);
assert_eq!(format!("{s8}"), "abcdefg");
assert_eq!(s8, const { TinyStr8::from_const(b"abcdefg") });Implementations§
Source§impl<T: TinyStrInt> TinyStr<T>
impl<T: TinyStrInt> TinyStr<T>
Sourcepub fn new(s: &[u8]) -> Option<Self>
pub fn new(s: &[u8]) -> Option<Self>
Creates a new TinyStr from a byte slice.
Returns None if the slice is empty, only contains NUL bytes, or is too long.
§Examples
let s = TinyStr4::new(b"abc").unwrap();
assert_eq!(s.len(), 3);
assert_eq!(format!("{s}"), "abc");
assert!(TinyStr4::new(b"").is_none());
assert!(TinyStr4::new(b"abcde").is_none());Source§impl TinyStr<NonZero<u16>>
impl TinyStr<NonZero<u16>>
Sourcepub const fn from_const(s: &[u8]) -> Self
pub const fn from_const(s: &[u8]) -> Self
Creates a TinyStr from a byte slice at compile time, panicking if the string is invalid.
§Examples
const S: TinyStr<NonZero<u16>> = TinyStr::<NonZero<u16>>::from_const(b"ab");
assert_eq!(S, TinyStr::new(b"ab").unwrap());
assert_eq!(S.to_string(), "ab");
assert_eq!(S.len(), 2);Source§impl TinyStr<NonZero<u32>>
impl TinyStr<NonZero<u32>>
Sourcepub const fn from_const(s: &[u8]) -> Self
pub const fn from_const(s: &[u8]) -> Self
Creates a TinyStr from a byte slice at compile time, panicking if the string is invalid.
§Examples
const S: TinyStr<NonZero<u32>> = TinyStr::<NonZero<u32>>::from_const(b"ab");
assert_eq!(S, TinyStr::new(b"ab").unwrap());
assert_eq!(S.to_string(), "ab");
assert_eq!(S.len(), 2);Source§impl TinyStr<NonZero<u64>>
impl TinyStr<NonZero<u64>>
Sourcepub const fn from_const(s: &[u8]) -> Self
pub const fn from_const(s: &[u8]) -> Self
Creates a TinyStr from a byte slice at compile time, panicking if the string is invalid.
§Examples
const S: TinyStr<NonZero<u64>> = TinyStr::<NonZero<u64>>::from_const(b"ab");
assert_eq!(S, TinyStr::new(b"ab").unwrap());
assert_eq!(S.to_string(), "ab");
assert_eq!(S.len(), 2);Trait Implementations§
Source§impl<T: TinyStrInt> Debug for TinyStr<T>
Writes a debug representation of the string.
impl<T: TinyStrInt> Debug for TinyStr<T>
Writes a debug representation of the string.
§Examples
let s = TinyStr4::new(b"abc").unwrap();
assert_eq!(format!("{s:?}"), "TinyStr(\"abc\")");
let invalid = TinyStr4::from_raw(std::num::NonZero::new(0x61FF6200).unwrap());
assert_eq!(format!("{invalid:?}"), "TinyStr([97, 255, 98])");Source§impl<T: TinyStrInt> Display for TinyStr<T>
Writes the string, replacing any invalid UTF-8 sequences with the replacement character.
impl<T: TinyStrInt> Display for TinyStr<T>
Writes the string, replacing any invalid UTF-8 sequences with the replacement character.
This is a comparatively expensive operation, requiring the value to be copied onto the stack and UTF-8 validation.
§Examples
let s = TinyStr4::new(b"abc").unwrap();
assert_eq!(format!("{s}"), "abc");
let invalid = TinyStr4::from_raw(std::num::NonZero::new(0x61FF6200).unwrap());
assert_eq!(format!("{invalid}"), "a\u{FFFD}b");