Crate lanyard

Source
Expand description

UTF-8 equivalents of std’s C string types.

lanyard is an extension of the std::ffi module that adds new Utf8CStr and Utf8CString types. They are like the standard library’s [CStr] and [CString] types, except they are guaranteed to be valid UTF-8.

Therefore they allow the ability to losslessly convert into strings, they implement Display, etc, etc. The std::ffi types are not guaranteed to be valid UTF-8, which is the right decision for the standard library.

However, in FFI, it’s not uncommon to encounter APIs which expect all strings to be UTF-8. This generally requires crate authors to convert between &str and CStr via CString between the Rust-C boundary (which allocates every time).

With UTF-8 C strings, the conversion between a &str and Utf8CStr are made explicit, and allocations can be minimized.

Macros§

c
Create a const &’static Utf8CStr from a string literal.

Structs§

Utf8CStr
Representation of a borrowed UTF-8 C string. This type is #[repr(transparent)] and can be transmuted to a &CStr safely.
Utf8CString
A type representing an owned, C-compatible, UTF-8, nul-terminated string with no nul bytes in the middle. This type is #[repr(transparent)] and can be transmuted to a &CString safely.

Enums§

FromOwnedUtf8WithNul
An error indicating that a nul byte was not in the expected position, or that there was invalid UTF-8.
FromUtf8UntilNul
An error indicating that no nul byte was present, or that there was invalid UTF-8.
FromUtf8WithNul
An error indicating that a nul byte was not in the expected position, or that there was invalid UTF-8.