89 releases

0.20.6 Nov 12, 2024
0.20.4 Sep 26, 2024
0.20.0 Jul 10, 2024
0.19.3 Mar 19, 2024
0.0.3 Jul 18, 2015

#80 in Unix APIs

Download history 99091/week @ 2024-08-25 111738/week @ 2024-09-01 94765/week @ 2024-09-08 87145/week @ 2024-09-15 102456/week @ 2024-09-22 104124/week @ 2024-09-29 103964/week @ 2024-10-06 109464/week @ 2024-10-13 102047/week @ 2024-10-20 113767/week @ 2024-10-27 114801/week @ 2024-11-03 103062/week @ 2024-11-10 107880/week @ 2024-11-17 108588/week @ 2024-11-24 117050/week @ 2024-12-01 108170/week @ 2024-12-08

454,865 downloads per month
Used in 1,003 crates (324 directly)

MIT license

2.5MB
54K SLoC

Rust GLib and GObject bindings

Rust bindings and wrappers for GLib, part of gtk-rs-core.

GLib 2.56 is the lowest supported version for the underlying library.

This library contains bindings to GLib and GObject types and APIs as well as common building blocks used in both handmade and machine generated bindings to GTK and other GLib-based libraries.

It is the foundation for higher level libraries with uniform Rusty (safe and strongly typed) APIs. It avoids exposing GLib-specific data types where possible and is not meant to provide comprehensive GLib bindings, which would often amount to duplicating the Rust Standard Library or other utility crates.

Minimum supported Rust version

Currently, the minimum supported Rust version is 1.70.0.

Dynamic typing

Most types in the GLib family have Type identifiers. Their corresponding Rust types implement the StaticType trait.

A dynamically typed Value can carry values of any StaticType. Variants can carry values of StaticVariantType.

Errors

Errors are represented by Error, which can carry values from various error domains such as FileError.

Objects

Each class and interface has a corresponding smart pointer struct representing an instance of that type (e.g. Object for GObject or gtk4::Widget for GtkWidget). They are reference counted and feature interior mutability similarly to Rust's Rc<RefCell<T>> idiom. Consequently, cloning objects is cheap and their methods never require mutable borrows. Two smart pointers are equal if they point to the same object.

The root of the object hierarchy is Object. Inheritance and subtyping is denoted with the IsA marker trait. The Cast trait enables upcasting and downcasting.

Interfaces and non-leaf classes also have corresponding traits (e.g. ObjectExt or WidgetExt), which are blanketly implemented for all their subtypes.

You can create new subclasses of Object or other object types. Look at the module's documentation for further details and a code example.

Under the hood

GLib-based libraries largely operate on pointers to various boxed or reference counted structures so the bindings have to implement corresponding smart pointers (wrappers), which encapsulate resource management and safety checks. Such wrappers are defined via the wrapper! macro, which uses abstractions defined in the wrapper, boxed, shared and object modules. The translate module defines and partly implements conversions between high level Rust types (including the aforementioned wrappers) and their FFI counterparts.

Documentation

Using

We recommend using crates from crates.io, as demonstrated here.

If you want to track the bleeding edge, use the git dependency instead:

[dependencies]
glib = { git = "https://github.com/gtk-rs/gtk-rs-core.git", package = "glib" }

Avoid mixing versioned and git crates like this:

# This will not compile
[dependencies]
glib = "0.13"
glib = { git = "https://github.com/gtk-rs/gtk-rs-core.git", package = "glib" }

License

glib is available under the MIT License, please refer to it.

Dependencies

~2.2–10MB
~103K SLoC