#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

#850 in Rust patterns

Download history 11060/week @ 2024-11-15 10400/week @ 2024-11-22 9797/week @ 2024-11-29 11654/week @ 2024-12-06 13688/week @ 2024-12-13 5730/week @ 2024-12-20 5669/week @ 2024-12-27 11612/week @ 2025-01-03 13982/week @ 2025-01-10 12000/week @ 2025-01-17 13063/week @ 2025-01-24 14242/week @ 2025-01-31 76547/week @ 2025-02-07 99080/week @ 2025-02-14 125450/week @ 2025-02-21 146591/week @ 2025-02-28

453,030 downloads per month
Used in 54 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

~250KB