#ffi

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

#224 in #ffi

Download history 3268/week @ 2022-06-04 3053/week @ 2022-06-11 4053/week @ 2022-06-18 3479/week @ 2022-06-25 2420/week @ 2022-07-02 3718/week @ 2022-07-09 3907/week @ 2022-07-16 3849/week @ 2022-07-23 4476/week @ 2022-07-30 3929/week @ 2022-08-06 3826/week @ 2022-08-13 2999/week @ 2022-08-20 2627/week @ 2022-08-27 3133/week @ 2022-09-03 3205/week @ 2022-09-10 2845/week @ 2022-09-17

12,298 downloads per month
Used in 37 crates (9 directly)

Unlicense

13KB
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

~200KB