Crate c_utf8

Source
Expand description

Operations on UTF-8-encoded C strings.

The CUtf8 and CUtf8Buf types are guaranteed to be:

  • Nul (Ø) terminated C strings in order to more safely ensure that C APIs only access memory that is properly owned.

  • Encoded as valid UTF-8, allowing for passing around native Rust str strings with ease.

§Usage

This crate is available on crates.io and can be used by adding the following to your project’s Cargo.toml:

[dependencies]
c_utf8 = "0.1"

and this to your crate root (lib.rs or main.rs):

#[macro_use] // enables c_utf8! macro
extern crate c_utf8;

§Examples

A CUtf8 slice can be created via the c_utf8! macro, which ensures it will always end with a trailing 0 byte:

use c_utf8::CUtf8;

static MESSAGE: &CUtf8 = c_utf8!("Heyo!");

fn main() {
    let bytes = [72, 101, 121, 111, 33, 0];
    assert_eq!(MESSAGE.as_bytes_with_nul(), &bytes);
}

Macros§

c_utf8
Creates a &'static CUtf8 from a native Rust str string literal, making it much easier to work with C APIs that are strict about encoding input as UTF-8.

Structs§

CUtf8
Like CStr, except with the guarantee of being encoded as valid UTF-8.
CUtf8Buf
An owned, mutable UTF-8 encoded C string (akin to String or PathBuf).

Enums§

Error
The error for converting types to CUtf8.

Type Aliases§

c_char
Equivalent to C’s char type.