dtolnay/cxx

Handle &CStr <-> const char*

Open

#681 创建于 2021年1月16日

在 GitHub 查看
 (2 评论) (2 反应) (0 负责人)Rust (4,472 star) (253 fork)batch import
help wanted

描述

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);
}

贡献者指南