CStrStack

Struct CStrStack 

Source
pub struct CStrStack<const N: usize> { /* private fields */ }
Expand description

A stack-allocated C string with a fixed buffer size.

CStrStack<N> stores a formatted string directly on the stack with a buffer of N bytes, avoiding heap allocation.
It always appends a trailing \0 (null terminator) so it can be safely passed to C FFI functions.

If the formatted string (plus the null terminator) does not fit into the buffer, [new] will return an error.

§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("stack buffer overflow") if the formatted string is too long to fit in the buffer.

Returns Err("format failed") if formatting the string fails (rare case, usually only if the formatter writes an error).

Implementations§

Source§

impl<const N: usize> CStrStack<N>

Source

pub fn new(fmt: Arguments<'_>) -> Result<CStrStack<N>, &'static str>

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.

Source

pub fn as_ptr(&self) -> *const c_char

Returns a raw pointer to the null-terminated C string.

This pointer is valid for as long as self is alive. Suitable for passing to FFI.

Source

pub fn as_cstr(&self) -> &CStr

Returns a reference to the underlying CStr.

§Safety

The buffer is guaranteed to be null-terminated by construction, so this is always safe.

Trait Implementations§

Source§

impl<const N: usize> CStrLike for CStrStack<N>

Source§

fn as_ptr(&self) -> *const c_char

Returns a raw pointer to the null-terminated C string. Read more
Source§

fn as_cstr(&self) -> &CStr

Returns a reference to the underlying CStr.

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.