#cstr #unsafe #error #s #allocation #interior #argument

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

#529 in Rust patterns

Download history 6236/week @ 2023-08-01 6601/week @ 2023-08-08 6931/week @ 2023-08-15 8226/week @ 2023-08-22 6454/week @ 2023-08-29 7516/week @ 2023-09-05 7513/week @ 2023-09-12 7008/week @ 2023-09-19 7211/week @ 2023-09-26 7277/week @ 2023-10-03 5773/week @ 2023-10-10 5452/week @ 2023-10-17 6415/week @ 2023-10-24 5552/week @ 2023-10-31 5413/week @ 2023-11-07 4430/week @ 2023-11-14

22,960 downloads per month
Used in 46 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