pub struct CStrStack<const N: usize> { /* private fields */ }Expand description
A stack-allocated, null-terminated C string with fixed capacity.
CStrStack<N> stores a formatted string directly on the stack, avoiding heap allocation.
The internal buffer has N bytes and is always null-terminated (\0),
making it safe to pass to C functions via as_ptr().
§Examples
use std::ffi::CStr;
use stack_cstr::{CStrStack, CStrLike};
// Create a stack-allocated C string with capacity for 32 bytes
let s = CStrStack::<32>::new(format_args!("Hello {}", 123)).unwrap();
let cstr: &CStr = s.as_cstr();
assert_eq!(cstr.to_str().unwrap(), "Hello 123");
unsafe {
// FFI-safe pointer
assert_eq!(CStr::from_ptr(s.as_ptr()).to_str().unwrap(), "Hello 123");
}§Errors
- Returns
Err("buffer overflow")if the formatted string (plus NUL) does not fit in the buffer. - Returns
Err("format failed")if formatting fails (rare case; ArrayString rarely errors).
Implementations§
Source§impl<const N: usize> CStrStack<N>
impl<const N: usize> CStrStack<N>
Sourcepub fn new(fmt: Arguments<'_>) -> Result<CStrStack<N>, CStrError>
pub fn new(fmt: Arguments<'_>) -> Result<CStrStack<N>, CStrError>
Creates a new stack-allocated C string using a format_args! expression.
The string is written into an internal buffer of size N.
If the string does not fit, returns an error.
Trait Implementations§
Auto Trait Implementations§
impl<const N: usize> Freeze for CStrStack<N>
impl<const N: usize> RefUnwindSafe for CStrStack<N>
impl<const N: usize> Send for CStrStack<N>
impl<const N: usize> Sync for CStrStack<N>
impl<const N: usize> Unpin for CStrStack<N>
impl<const N: usize> UnwindSafe for CStrStack<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
Mutably borrows from an owned value. Read more