Expand description

unixstring codecov Crates.io Docs

UnixString is an FFI-friendly null-terminated byte string that may be constructed from a String, a CString, a PathBuf, an OsString or a collection of bytes.

An UnixString can then be converted into a slice of CStr, Path or OsStr in infallible and zero-cost operations.

Why?

UnixString aims to be useful in any scenario where you’d like to use FFI (specially with C) on Unix systems. If you have a PathBuf, for example, you can send that data to a libc function, such as stat, but you’d have to first allocate a CString (or something analogous) to do so.

The same is true with OsString and String because these three types are allowed to have internal zero bytes and are not null-terminated.

A UnixString is very close to what a CString is but with increased flexibility and usability. A CString cannot be changed or increased after instantited, while UnixString is growable through its push and push_bytes methods, somewhat similar to OsString.

A CString also does not have direct reference conversions to anything but &[u8] or &CStr, while UnixString has those and more (described below).

Obtaining references from an UnixString

IntoFunctionNotes
&CStrUnixString::as_c_strAvailable through AsRef as well
&PathUnixString::as_pathAvailable through AsRef as well
&strUnixString::as_strFails if the bytes of the UnixString aren’t valid UTF-8
&[u8]UnixString::as_bytesReturns the bytes of the UnixString without the null terminator
&[u8]UnixString::as_bytes_with_nulReturns the bytes of the UnixString with the null terminator
&OsStrUnixString::as_os_strAvailable through AsRef as well
* const c_charUnixString::as_ptr

Creating an UnixString

FromPotential failureTrait implFunction
CStringInfallibleFromUnixString::from_cstring
PathBufFails if contains an interior zero byteTryFromUnixString::from_pathbuf
StringFails if contains an interior zero byteTryFromUnixString::from_string
Vec<u8>Fails if contains an interior zero byteTryFromUnixString::from_bytes
OsStringFails if contains an interior zero byteTryFromUnixString::from_os_string
* const c_charUnsafe, see the docs for more infoNoneUnixString::from_ptr

Converting from an UnixString

IntoFunctionNotes
CStringUnixString::into_cstring
PathBufUnixString::into_pathbuf
OsStringUnixString::into_os_string
StringUnixString::into_stringFails if the UnixString’s bytes are not valid UTF-8
StringUnixString::into_string_lossy
StringUnixString::to_string_lossyNon-moving version of UnixString::into_string_lossy
StringUnixString::into_string_uncheckedUnsafe: creates a String without checking if the bytes are valid UTF-8
Vec<u8>UnixString::into_bytesReturns the bytes of the UnixString without the null terminator
Vec<u8>UnixString::into_bytes_with_nulReturns the bytes of the UnixString with the null terminator

All of the above are also available through .into().

Structs

An FFI-friendly null-terminated byte string.

Enums

An error enum that encapsulates all possible errors in this crate.

Type Definitions

A Result type alias for this crate’s Error type.