Skip to content

fxdmhtt/stack-cstr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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");
}

About

High-performance stack-to-heap C string creation for Rust with FFI support

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages