Module glib::translate

source ·
Expand description

Translation between GLib/GLib-based FFI types and their Rust counterparts.

This module allows library bindings authors to decouple type translation logic and use unified idioms at FFI boundaries. It also implements translation of GLib core data types.

FromGlib, from_glib and ToGlib translate simple types like bool.

    pub fn set_accept_focus(&self, accept_focus: bool) {
        unsafe { glib_ffi::gdk_window_set_accept_focus(self.pointer, accept_focus.to_glib()) }
    }

    pub fn get_accept_focus(&self) -> bool {
        unsafe { from_glib(glib_ffi::gdk_window_get_accept_focus(self.pointer)) }
    }

ToGlibPtr, FromGlibPtrNone, FromGlibPtrFull and FromGlibPtrBorrow work on gpointers and support different modes of ownership transfer.

    fn get_title(&self) -> Option<String> {
        unsafe {
            let title = glib_ffi::gtk_window_get_title(self.pointer);
            from_glib_none(title)
        }
    }

Letting the foreign library borrow pointers from the Rust side often requires having a temporary variable of an intermediate type (e.g. CString). A Stash contains the temporary storage and a pointer into it that is valid for the lifetime of the Stash. As the lifetime of the Stash returned from to_glib_none is at least the enclosing statement, you can avoid explicitly binding the stash in most cases and just take the pointer out of it:

    pub fn set_icon_name(&self, name: &str) {
        unsafe {
            glib_ffi::gdk_window_set_icon_name(self.pointer, name.to_glib_none().0)
        }
    }

Structs

Helper type that stores temporary values used for translation.

Traits

Translate a simple type.
Translate from a container.
Translate from a pointer type by borrowing. Don’t increase the refcount
Translate from a container of pointers.
Translate from a pointer type taking ownership, transfer: full.
Translate from a pointer type without taking ownership, transfer: none.
Provides the default pointer type to be used in some container conversions.
A pointer
Translate a simple type.
Translate to a pointer.
Translate to a pointer with a mutable borrow.
A trait for creating an uninitialized value. Handy for receiving outparams.

Functions

Overrides pointer constness.
Translate a simple type.
Translate from a pointer type, borrowing the pointer.
Translate from a pointer type, transfer: full (assume ownership).
Translate from a pointer type, transfer: none.
Overrides pointer mutability.
Returns Some(val) if the condition is true and None otherwise.
Returns an uninitialized value.