pub enum FzString<'a> {
    Null,
    String(String),
    CString(CString),
    CStr(&'a CStr),
    Bytes(Vec<u8>),
}
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.

A number of From<T> implementations are provided to convert from common Rust types. The fz_string_.. utility functions provide conversions from various string formats.

FzStrings also have a special “Null” state, similar to the None variant of Option. For user convenience, a NULL pointer is treated as a pointer to the Null variant wherever a pointer is accepted. Rust code should use the _nonnull methods where the Null variant is not allowed. 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>

source

pub fn is_null(&self) -> bool

Check if this is a Null FzString.

source

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.

source

pub fn as_str_nonnull(&mut self) -> Result<&str, InvalidUTF8Error>

Convert this FzString, assuming it is not Null, into &str.

This is a simple wrapper that will panic on the Null variant. This is useful when the C API prohibits NULL.

source

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.

source

pub fn as_cstr_nonnull(&mut self) -> Result<&CStr, EmbeddedNulError>

Convert this FzString, assuming it is not Null, into a CStr.

This is a simple wrapper that will panic on the Null variant. This is useful when the C API prohibits NULL.

source

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.

source

pub fn into_string_nonnull(self) -> Result<String, InvalidUTF8Error>

Consume this FzString, assuming it is not Null, and return an equivalent String.

This is a simple wrapper that will panic on the Null variant. This is useful when the C API prohibits NULL.

source

pub fn into_path_buf(self) -> Result<Option<PathBuf>, Utf8Error>

Consume this FzString and return an equivalent PathBuf.

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.

source

pub fn into_path_buf_nonnull(self) -> Result<PathBuf, Utf8Error>

Consume this FzString, assuming it is not Null, and return an equivalent PathBuf.

This is a simple wrapper that will panic on the Null variant. This is useful when the C API prohibits NULL.

source

pub fn as_bytes(&self) -> Option<&[u8]>

Get the slice of bytes representing the content of this value, not including any NUL terminator.

Any variant can be represented as a byte slice, so this method does not mutate the FzString and cannot fail.

The Null variant is represented as None.

source

pub fn as_bytes_nonnull(&self) -> &[u8]

Get the slice of bytes representing the content of this value, not including any NUL terminator, panicing if this is the Null Variant.

This is a simple wrapper that will panic on the Null variant. This is useful when the C API prohibits NULL.

source

pub unsafe fn with_ref<T, F: Fn(&FzString<'_>) -> T>(
    fzstr: *const fz_string_t,
    f: F
) -> T

Call the contained function with a shared reference to the FzString.

This is a wrapper around ffizz_passby::OpaqueStruct::with_ref.

Safety
  • fzstr must be NULL or point to a valid fz_string_t value
  • no other thread may mutate the value pointed to by fzstr until with_ref returns.
source

pub unsafe fn with_ref_mut<T, F: Fn(&mut FzString<'_>) -> T>(
    fzstr: *mut fz_string_t,
    f: F
) -> T

Call the contained function with an exclusive reference to the FzString.

This is a wrapper around ffizz_passby::OpaqueStruct::with_ref_mut.

Safety
  • fzstr must be NULL or point to a valid fz_string_t value
  • no other thread may access the value pointed to by fzstr until with_ref_mut returns.
source

pub unsafe fn to_out_param(self, fzstr: *mut fz_string_t)

Initialize the value pointed to fzstr with, “moving” it into the pointer.

This is a wrapper around ffizz_passby::OpaqueStruct::to_out_param.

If the pointer is NULL, the value is dropped.

Safety
  • if fzstr is not NULl, then it must be aligned for fz_string_t, and must have enough space for fz_string_t.
  • ownership of the string is transfered to *fzstr or dropped.
source

pub unsafe fn to_out_param_nonnull(self, fzstr: *mut fz_string_t)

Initialize the value pointed to fzstr with, “moving” it into the pointer.

This is a wrapper around ffizz_passby::OpaqueStruct::to_out_param_nonnull.

If the pointer is NULL, this method will panic. Use this when the C API requires that the pointer be non-NULL.

Safety
  • fzstr must not be NULL, must be aligned for fz_string_t, and must have enough space for fz_string_t.
  • ownership of the string is transfered to *fzstr.
source

pub unsafe fn return_val(self) -> fz_string_t

Return a fz_string_t transferring ownership out of the function.

This is a wrapper around ffizz_passby::OpaqueStruct::return_val.

Safety
  • to avoid a leak, ownership of the value must eventually be returned to Rust.
source

pub unsafe fn take(fzstr: fz_string_t) -> Self

Take a fz_string_t by value and return an owned FzString.

This is a wrapper around ffizz_passby::OpaqueStruct::take.

This method is intended for C API functions that take a string by value and are documented as taking ownership of the value. However, this means that C retains an expired “copy” of the value and could lead to use-after-free errors.

Where compatible with the API design, prefer to use pointers in the C API and use FzString::take_ptr to ensure the old value is invalidated.

Safety
  • fzstr must be a valid fz_string_t value
source

pub unsafe fn take_ptr(fzstr: *mut fz_string_t) -> Self

Take a pointer to a CType and return an owned value.

This is a wrapper around ffizz_passby::OpaqueStruct::take_ptr.

This is intended for C API functions that take a value by reference (pointer), but still “take ownership” of the value. It leaves behind an invalid value, where any non-padding bytes of the Rust type are zeroed. This makes use-after-free errors in the C code more likely to crash instead of silently working. Which is about as good as it gets in C.

Do not pass a pointer to a Rust value to this function:

let rust_value = RustType::take_ptr(&mut c_value); // BAD!

This creates undefined behavior as Rust will assume c_value is still initialized. Use take in this situation.

Safety
  • fzstr must be NULL or point to a valid fz_string_t value.
  • the memory pointed to by fzstr is uninitialized when this function returns.

Trait Implementations§

source§

impl<'a> Debug for FzString<'a>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for FzString<'_>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl From<&[u8]> for FzString<'static>

source§

fn from(bytes: &[u8]) -> FzString<'static>

Converts to this type from the input type.
source§

impl From<&str> for FzString<'static>

source§

fn from(string: &str) -> FzString<'static>

Converts to this type from the input type.
source§

impl From<Option<&[u8]>> for FzString<'static>

source§

fn from(bytes: Option<&[u8]>) -> FzString<'static>

Converts to this type from the input type.
source§

impl From<Option<&str>> for FzString<'static>

source§

fn from(string: Option<&str>) -> FzString<'static>

Converts to this type from the input type.
source§

impl From<Option<String>> for FzString<'static>

source§

fn from(string: Option<String>) -> FzString<'static>

Converts to this type from the input type.
source§

impl From<Option<Vec<u8, Global>>> for FzString<'static>

source§

fn from(bytes: Option<Vec<u8>>) -> FzString<'static>

Converts to this type from the input type.
source§

impl From<String> for FzString<'static>

source§

fn from(string: String) -> FzString<'static>

Converts to this type from the input type.
source§

impl From<Vec<u8, Global>> for FzString<'static>

source§

fn from(bytes: Vec<u8>) -> FzString<'static>

Converts to this type from the input type.
source§

impl OpaqueStruct for FzString<'_>

§

type CType = fz_string_t

The C representation of this type. This must have the same alignment as Self and its size must not be less than that of Self.
source§

fn null_value() -> Self

Get the value of this type used to represent a NULL pointer. Read more
source§

unsafe fn with_ref<T, F>(cptr: *const Self::CType, f: F) -> Twhere
    F: Fn(&Self) -> T,

Call the contained function with a shared reference to the data type. Read more
source§

unsafe fn with_ref_mut<T, F>(cptr: *mut Self::CType, f: F) -> Twhere
    F: Fn(&mut Self) -> T,

Call the contained function with an exclusive reference to the data type. Read more
source§

unsafe fn to_out_param(self, cptr: *mut Self::CType)

Initialize the value pointed to cptr with rval, “moving” rval into the pointer. Read more
source§

unsafe fn to_out_param_nonnull(self, cptr: *mut Self::CType)

Initialize the value pointed to cptr with rval, “moving” rval into the pointer. Read more
source§

unsafe fn return_val(self) -> Self::CType

Return a CType containing self, moving self in the process. Read more
source§

unsafe fn take(cval: Self::CType) -> Self

Take a CType and return an owned value. Read more
source§

unsafe fn take_ptr(cptr: *mut Self::CType) -> Self

Take a pointer to a CType and return an owned value. Read more
source§

impl<'a> PartialEq<FzString<'a>> for FzString<'a>

source§

fn eq(&self, other: &FzString<'a>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> Eq for FzString<'a>

source§

impl<'a> StructuralEq for FzString<'a>

source§

impl<'a> StructuralPartialEq for FzString<'a>

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for FzString<'a>

§

impl<'a> Send for FzString<'a>

§

impl<'a> Sync for FzString<'a>

§

impl<'a> Unpin for FzString<'a>

§

impl<'a> UnwindSafe for FzString<'a>

Blanket Implementations§

source§

impl<T> Any for Twhere
    T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere
    T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere
    T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere
    U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere
    U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere
    U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.