#string #ffi #null #converting #arguments #cstr #terminated

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

#644 in Rust patterns

Download history 5275/week @ 2023-12-04 6534/week @ 2023-12-11 4995/week @ 2023-12-18 1982/week @ 2023-12-25 3786/week @ 2024-01-01 5407/week @ 2024-01-08 5634/week @ 2024-01-15 5807/week @ 2024-01-22 6117/week @ 2024-01-29 5595/week @ 2024-02-05 7088/week @ 2024-02-12 5316/week @ 2024-02-19 5494/week @ 2024-02-26 6903/week @ 2024-03-04 7754/week @ 2024-03-11 7429/week @ 2024-03-18

27,766 downloads per month
Used in 45 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

~175–320KB