c_str

Crates.ioc_str
lib.rsc_str
version1.0.8
sourcesrc
created_at2015-01-07 22:11:04.305585
updated_at2015-12-11 23:57:49.028558
descriptionThe old c_str functions
homepage
repositoryhttps://github.com/GuillaumeGomez/c_str-rs.git
max_upload_size
id735
size33,545
Guillaume Gomez (GuillaumeGomez)

documentation

README

c_str Build Status

Old rust c_str module. It provides the ToCStr and FromCStr traits. It works just like the old one:

extern crate libc;
extern crate c_str;

use c_str::{FromCStr, ToCStr};

fn some_func(cstr: *const libc::c_char) {
    let s : String = FromCStr::from_c_str(cstr);

    println!("converted from c string: {}", s);
}

fn some_other_func(rstr: &str) {
    unsafe {
        rstr.with_c_str(|s| {
            some_c_func(s)
        })
    }
}

Equivalent in Rust

Here is the equivalent in pure Rust:

fn from_c_func(cstr: *const libc::c_char) -> String {
    FromCStr::from_c_str(tmp)
    // equivalent in rust:
    String::from_utf8_lossy(::std::ffi::c_str_to_bytes(&tmp).to_string())
}

fn main() {
    let s = "hello";

    s.with_c_str(|cstr| {
        from_c_func(cstr)
    });
    // equivalent in rust:
    let cstring = CString::from_slice(s.as_bytes());

    from_c_func(cstring.as_ptr());
}

Usage

You can use it directly by adding this line to your Cargo.toml file:

[dependencies]
c_str = "^1.0.0"

Here's is the crates.io page for c_str.

License

This project is under the MIT and Apache 2.0 licenses. Please look at the license files for more information.

Commit count: 23

cargo fmt