cstr-argument

Crates.iocstr-argument
lib.rscstr-argument
version0.1.2
sourcesrc
created_at2017-09-10 07:59:44.939373
updated_at2021-10-14 02:24:20.457961
descriptionA trait for converting function arguments to null terminated strings
homepage
repositoryhttps://github.com/johnschug/cstr-argument
max_upload_size
id31265
size14,717
John Schug (johnschug)

documentation

https://docs.rs/cstr-argument

README

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
}
Commit count: 14

cargo fmt