dtolnay/cxx

Handle &CStr <-> const char*

Open

#681 opened on Jan 16, 2021

View on GitHub
 (2 comments) (2 reactions) (0 assignees)Rust (253 forks)batch import
help wanted

Repository metrics

Stars
 (4,472 stars)
PR merge metrics
 (Avg merge 8m) (3 merged PRs in 30d)

Description

For a C++ function like this:

void example(const char *s);

it should work to call it as:

use std::ffi::CStr;

#[cxx::bridge]
mod ffi {
    unsafe extern "C++" {
        fn example(s: &CStr);
    }
}

fn main() {
    let cstr = CStr::from_bytes_with_nul(b"test\0").unwrap();
    ffi::example(cstr);
}

Contributor guide