#arguments #ffi #null-terminated-string

cstr-argument

A trait for converting function arguments to null terminated strings

5 releases

Uses old Rust 2015

0.1.2 Oct 14, 2021
0.1.1 Jan 28, 2019
0.1.0 Apr 15, 2018
0.0.2 Sep 10, 2017
0.0.1 Sep 10, 2017

#113 in Value formatting

Download history 67480/week @ 2025-09-15 59224/week @ 2025-09-22 69174/week @ 2025-09-29 97186/week @ 2025-10-06 80148/week @ 2025-10-13 71282/week @ 2025-10-20 66105/week @ 2025-10-27 56287/week @ 2025-11-03 55079/week @ 2025-11-10 76630/week @ 2025-11-17 44887/week @ 2025-11-24 64540/week @ 2025-12-01 53120/week @ 2025-12-08 61337/week @ 2025-12-15 28860/week @ 2025-12-22 25151/week @ 2025-12-29

171,395 downloads per month
Used in 62 crates (9 directly)

Unlicense

14KB
243 lines

cstr-argument

A trait for converting function arguments to null terminated strings

Build Status Version

Documentation

Usage

Add this to your Cargo.toml:

[dependencies]
cstr-argument = "0.0.2"

and this to your crate root:

extern crate cstr_argument;

Example

use std::os::raw::c_char;
use cstr_argument::CStrArgument;

extern "C" {
  fn foo(s: *const c_char);
}

fn bar<S: CStrArgument>(s: S) {
  let s = s.into_cstr();
  unsafe {
    foo(s.as_ref().as_ptr())
  }
}

fn baz() {
  bar("hello "); // Argument will be converted to a CString requiring an allocation
  bar("world\0"); // Argument will be converted to a CStr without allocation
  bar("!".to_owned()); // Argument will be converted to a CString possibly requiring an allocation
}

Dependencies

~245KB