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

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

#860 in Rust patterns

Download history 7839/week @ 2024-07-24 9650/week @ 2024-07-31 9284/week @ 2024-08-07 8910/week @ 2024-08-14 9732/week @ 2024-08-21 9285/week @ 2024-08-28 8521/week @ 2024-09-04 8974/week @ 2024-09-11 9368/week @ 2024-09-18 11555/week @ 2024-09-25 9327/week @ 2024-10-02 10622/week @ 2024-10-09 9935/week @ 2024-10-16 9175/week @ 2024-10-23 10026/week @ 2024-10-30 7497/week @ 2024-11-06

38,684 downloads per month
Used in 47 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

~110–250KB