| Crates.io | fixed_str |
| lib.rs | fixed_str |
| version | 0.9.1 |
| created_at | 2025-03-25 04:50:07.246774+00 |
| updated_at | 2025-03-25 17:49:39.879885+00 |
| description | Fixed-size, null-padded UTF-8 string type with const-safe construction and binary serialization support. |
| homepage | |
| repository | https://github.com/crabcode/fixed_str |
| max_upload_size | |
| id | 1604760 |
| size | 108,451 |
fixed_str is a Rust crate that provides fixed–capacity, null–padded UTF‑8 string types designed for performance–critical, memory–sensitive, or FFI contexts. With predictable layout and safe UTF‑8 handling, it’s ideal for embedded applications, binary serialization, and other environments where precise control over memory is paramount.
fixed_str introduces a primary type, FixedStr<N>, which uses a [u8; N] array as its internal storage. Unused bytes are zero–padded and the first null byte (\0) serves as the string terminator.
The null byte terminator (\0) also forms the basis for all conversion and comparison, considering the effective string rather than the underlying fixed-size byte array.
\0, while preserving the underlying data.FixedStr::new_const to create fixed strings in constant contexts (with the caveat that UTF‑8 isn’t revalidated).FixedStrBuf builder allows you to construct fixed strings piece–by–piece with proper boundary checks.Clone, Copy, Debug, Display, PartialEq, and more.Add the following to your Cargo.toml:
[dependencies]
fixed_str = "0.9.1"
Or run:
cargo add fixed_str [--feature serde]
Optional feature flags include:
const_mut_refs (enabled by default, disable for compatibility with rustc versions <1.83).use fixed_str::FixedStr;
fn main() {
// Create a FixedStr with 10 bytes of storage.
let s = FixedStr::<10>::new("Hello, world!");
// Output will be "Hello, wor" due to safe UTF‑8 truncation.
println!("{}", s);
}
FixedStrBufuse fixed_str::{FixedStrBuf, FixedStr};
fn main() {
let mut buf = FixedStrBuf::<12>::new();
buf.try_push_str("Hello").unwrap();
buf.try_push_char(' ').unwrap();
buf.push_str_lossy("world! 👋");
let fixed: FixedStr<12> = buf.finalize();
println!("{}", fixed); // Likely prints "Hello world!"
}
String (requires the std feature).FixedStr::new(&str) -> Self: Create a fixed string with proper UTF‑8 truncation.FixedStr::new_const(&str) -> Self: Const–fn compile-time construction with safe truncation.set(&mut self, &str): Replace the content (truncated at the first \0).clear(): Zero out the internal buffer.truncate(len: usize): Truncate the visible portion to a specified length.as_str() -> &str: View the string up to the null terminator or last valid UTF-8 character.try_as_str() -> Result<&str, FixedStrError>: UTF‑8 tested view.as_bytes() -> &[u8]: Raw byte view of the entire buffer.effective_bytes() -> &[u8]: View of the bytes until the first \0.into_string() -> String: Convert into an owned String (requires std).to_string_lossy() -> String: Lossy conversion if needed.This project is dual–licensed under either the MIT license or the Apache License, Version 2.0. See LICENSE-MIT and LICENSE-APACHE for details.