https://github.com/johnschug/cstr-argument
https://github.com/johnschug/cstr-argument
ffi rust
Last synced: 7 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/johnschug/cstr-argument
- Owner: johnschug
- License: unlicense
- Created: 2017-09-08T06:08:15.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2025-05-11T01:02:08.000Z (about 1 year ago)
- Last Synced: 2025-10-04T01:54:01.901Z (9 months ago)
- Topics: ffi, rust
- Language: Rust
- Size: 11.7 KB
- Stars: 4
- Watchers: 1
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
cstr-argument
=============
A trait for converting function arguments to null terminated strings
[](https://travis-ci.org/johnschug/cstr-argument)
[](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: 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
}
```