Struct glib::char::Char

source ·
pub struct Char(pub c_char);
Expand description

Wrapper for values where C functions expect a plain C char

Consider the following C function prototype from glib:

void g_key_file_set_list_separator (GKeyFile *key_file, gchar separator);

This function plainly expects a byte as the separator argument. However, having this function exposed to Rust as the following would be inconvenient:

impl KeyFile {
    pub fn set_list_separator(&self, separator: libc:c_char) { }
}

This would be inconvenient because users would have to do the conversion from a Rust char to an libc::c_char by hand, which is just a type alias for i8 on most system.

This Char type is a wrapper over an libc::c_char, so that we can pass it to Glib or C functions. The check for whether a Rust char (a Unicode scalar value) actually fits in a libc::c_char is done in the new function; see its documentation for details.

The inner libc::c_char (which is equivalent to i8) can be extracted with .0, or by calling my_char.into_glib().

Examples

use glib::Char;
use std::convert::TryFrom;

Char::from(b'a');
Char::try_from('a').unwrap();
assert!(Char::try_from('☔').is_err());
extern "C" fn have_a_byte(b: libc::c_char);

have_a_byte(Char::from(b'a').into_glib());

Tuple Fields§

§0: c_char

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
The type returned in the event of a conversion error.
Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.