Enum ffizz_string::FzString
source · Expand description
A FzString carries a single string between Rust and C code, represented from the C side as an opaque struct.
The two environments carry some different requirements: C generally requires that strings be NUL-terminated, while Rust requires that strings be valid UTF-8. Rust also permits NUL characters in the middle of a string.
This type accepts whatever kind of data it receives without error, and converts – potentially with an error – when output of a different kind is required.
FzStrings also have a special “Null” state, similar to the None variant of Option. Rust code
should use .unwrap()
where the Null variant is not allowed. For user convenience, a NULL
pointer is treated as a pointer to the Null variant wherever a pointer is accepted. Note that
the Null variant is not necessarily represented with an all-zero byte pattern.
A FzString points to allocated memory, and must be freed to avoid memory leaks.
Variants§
Null
An un-set FzString.
String(String)
An owned Rust string (not NUL-terminated, valid UTF-8).
CString(CString)
An owned C String (NUL-terminated, may contain invalid UTF-8).
CStr(&'a CStr)
A borrowed C string.
Bytes(Vec<u8>)
An owned bunch of bytes (not NUL-terminated, may contain invalid UTF-8).
Implementations§
source§impl<'a> FzString<'a>
impl<'a> FzString<'a>
sourcepub fn as_str(&mut self) -> Result<Option<&str>, InvalidUTF8Error>
pub fn as_str(&mut self) -> Result<Option<&str>, InvalidUTF8Error>
Convert this value to &str
.
If required, the FzString is converted in-place to a String variant. If this conversion fails because the content is not valid UTF-8, an error is returned.
The Null FzString is represented as None.
sourcepub fn as_cstr(&mut self) -> Result<Option<&CStr>, EmbeddedNulError>
pub fn as_cstr(&mut self) -> Result<Option<&CStr>, EmbeddedNulError>
Convert this value to a CStr: a slice of bytes containing a valid, NUL-terminated C string.
If required, the FzString is converted in-place to a CString variant. If this conversion fails because the content contains embedded NUL characters, an error is returned.
The Null FzString is represented as None.
sourcepub fn into_string(self) -> Result<Option<String>, InvalidUTF8Error>
pub fn into_string(self) -> Result<Option<String>, InvalidUTF8Error>
Consume this FzString and return an equivalent String.
As with as_str
, the FzString is converted in-place, and this conversion can fail. In the
failure case, the original data is lost.
The Null varaiant is represented as None.