const-cstr
Create static C-compatible strings from Rust string literals.
This is forked from abonander/const-cstr to implement an API that conforms
to the Rust API Guidelines and takes advantage of [const
functions][] to
hide more implementation details.
Cargo.toml:
toml
[dependencies]
zombiezen-const-cstr = "1.0"
```rust use zombiezenconstcstr::{const_cstr, ConstCStr};
use std::os::raw::c_char; use std::ffi::CStr;
/// Declare a constant: const HELLOCSTR: ConstCStr = constcstr!("Hello, world!");
// Imagine this is an extern "C"
function linked from some other lib.
unsafe fn printcstring(cstr: *const cchar) {
println!("{}", CStr::fromptr(cstr).to_str().unwrap());
}
fn main() { let goodnightcstr = constcstr!("Goodnight, sun!");
unsafe {
print_c_string(HELLO_CSTR.as_ptr());
print_c_string(goodnight_cstr.as_ptr());
}
} ```
Prints:
notest
Hello, world!
Goodnight, sun!
Licensed under either of * Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0) * MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT) at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you shall be dual licensed as above, without any additional terms or conditions.