stack-cstr

Crates.iostack-cstr
lib.rsstack-cstr
version0.2.0
created_at2025-08-21 07:15:09.851856+00
updated_at2025-12-24 10:56:52.570456+00
descriptionHigh-performance stack-to-heap C string creation for Rust with FFI support
homepage
repositoryhttps://github.com/fxdmhtt/stack-cstr
max_upload_size
id1804398
size39,075
(fxdmhtt)

documentation

README

stack_cstr

stack_cstr is a high-performance Rust library for creating C-compatible strings (&CStr) efficiently. It uses a stack buffer for short strings to avoid heap allocation, and automatically falls back to heap allocation for longer strings. The resulting strings are safe to pass to FFI functions.


Features

  • Stack buffer allocation for short strings (default 128 bytes)
  • Automatic heap fallback for longer strings
  • Supports format_args! style formatting
  • Returns CArrayString<128> for easy FFI usage
  • Simple macro interface: cstr!()
  • Ergonomic and safe for passing to C APIs

Usage Example

use std::ffi::CStr;

use stack_cstr::cstr;

// Create a C-compatible string
let s = cstr!("Pi = {:.2}", 3.14159);
assert_eq!(s.as_c_str().to_str().unwrap(), "Pi = 3.14");

unsafe {
    // Pass to FFI as *const c_char
    let ptr = s.as_ptr();
    assert_eq!(CStr::from_ptr(ptr).to_str().unwrap(), "Pi = 3.14");
}
Commit count: 7

cargo fmt