File: README.md

package info (click to toggle)
rust-cstr-argument 0.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 100 kB
  • sloc: makefile: 4
file content (47 lines) | stat: -rw-r--r-- 1,051 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
cstr-argument
=============
A trait for converting function arguments to null terminated strings

[![Build Status](https://travis-ci.org/johnschug/cstr-argument.svg?branch=master)](https://travis-ci.org/johnschug/cstr-argument)
[![Version](https://img.shields.io/crates/v/cstr-argument.svg)](https://crates.io/crates/cstr-argument)

[Documentation](https://docs.rs/cstr-argument)

## Usage

Add this to your `Cargo.toml`:

```toml
[dependencies]
cstr-argument = "0.0.2"
```

and this to your crate root:

```rust
extern crate cstr_argument;
```

## Example

```rust
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
}
```