FStr: a stack-allocated fixed-length string type
This crate provides a thin wrapper for [u8; N]
to handle a stack-allocated byte array as a
fixed-length, String
-like type through common traits such as Display
, PartialEq
, and
Deref<Target = str>
.
use FStr;
let x = try_from?;
println!; // "foo"
assert_eq!;
assert_eq!;
assert_eq!;
assert!;
assert!;
let mut y = try_from?;
assert_eq!;
y.make_ascii_uppercase;
assert_eq!;
const K: = from_str_unwrap;
assert_eq!;
Unlike String
and arrayvec::ArrayString
, this type has the same binary representation
as the underlying [u8; N]
and manages fixed-length strings only. The type parameter takes the
exact length (in bytes) of a concrete type, and the concrete type only holds the string values
of that size.
let s = "Lorem Ipsum ✨";
assert_eq!;
assert!; // just right
assert!; // too small
assert!; // too large
let x: = from_str_unwrap;
let y: = from_str_unwrap;
// This code does not compile because `FStr` of different lengths cannot mix.
if x != y
Variable-length string operations are partially supported by utilizing a C-style NUL-terminated buffer and some helper methods.
let mut buffer = from_str_lossy;
assert_eq!;
let c_str = buffer.slice_to_terminator;
assert_eq!;
use Write as _;
write!?;
assert_eq!;
Crate features
std
(optional; enabled by default) enables the integration withstd
. Disable default features to operate this crate underno_std
environments.serde
(optional) enables the serialization and deserialization ofFStr
throughserde
.
License
Licensed under the Apache License, Version 2.0.