| 1 | # Rust GLib and GObject bindings |
| 2 | |
| 3 | __Rust__ bindings and wrappers for [GLib](https://docs.gtk.org/glib/), part of [gtk-rs-core](https://github.com/gtk-rs/gtk-rs-core). |
| 4 | |
| 5 | GLib __2.56__ is the lowest supported version for the underlying library. |
| 6 | |
| 7 | This library contains bindings to GLib and GObject types and APIs as well as |
| 8 | common building blocks used in both handmade and machine generated |
| 9 | bindings to GTK and other GLib-based libraries. |
| 10 | |
| 11 | It is the foundation for higher level libraries with uniform Rusty (safe and |
| 12 | strongly typed) APIs. It avoids exposing GLib-specific data types where |
| 13 | possible and is not meant to provide comprehensive GLib bindings, which |
| 14 | would often amount to duplicating the Rust Standard Library or other utility |
| 15 | crates. |
| 16 | |
| 17 | ## Minimum supported Rust version |
| 18 | |
| 19 | Currently, the minimum supported Rust version is `1.83.0`. |
| 20 | |
| 21 | ## Dynamic typing |
| 22 | |
| 23 | Most types in the GLib family have [`Type`] identifiers. |
| 24 | Their corresponding Rust types implement the [`StaticType`] trait. |
| 25 | |
| 26 | A dynamically typed [`Value`] can carry values of any `StaticType`. |
| 27 | [Variants][`Variant`] can carry values of [`StaticVariantType`]. |
| 28 | |
| 29 | ## Errors |
| 30 | |
| 31 | Errors are represented by [`Error`], which can |
| 32 | carry values from various [error domains](error::ErrorDomain) such as |
| 33 | [`FileError`]. |
| 34 | |
| 35 | ## Objects |
| 36 | |
| 37 | Each class and interface has a corresponding smart pointer struct |
| 38 | representing an instance of that type (e.g. [`Object`] for [`GObject`] or |
| 39 | [`gtk4::Widget`] for [`GtkWidget`]). They are reference counted and feature |
| 40 | interior mutability similarly to Rust's |
| 41 | [`Rc<RefCell<T>>`] |
| 42 | idiom. Consequently, cloning objects is cheap and their methods never require |
| 43 | mutable borrows. Two smart pointers are equal if they point to the same |
| 44 | object. |
| 45 | |
| 46 | The root of the object hierarchy is [`Object`]. |
| 47 | Inheritance and subtyping is denoted with the [`IsA`] |
| 48 | marker trait. The [`Cast`] trait enables upcasting |
| 49 | and downcasting. |
| 50 | |
| 51 | Interfaces and non-leaf classes also have corresponding traits (e.g. |
| 52 | [`ObjectExt`] or [`WidgetExt`]), which are blanketly implemented for all |
| 53 | their subtypes. |
| 54 | |
| 55 | You can create new subclasses of [`Object`] or other object types. Look at |
| 56 | the module's documentation for further details and a code example. |
| 57 | |
| 58 | ## Under the hood |
| 59 | |
| 60 | GLib-based libraries largely operate on pointers to various boxed or |
| 61 | reference counted structures so the bindings have to implement corresponding |
| 62 | smart pointers (wrappers), which encapsulate resource management and safety |
| 63 | checks. Such wrappers are defined via the |
| 64 | [`wrapper!`] macro, which uses abstractions |
| 65 | defined in the [`wrapper`], [`boxed`], |
| 66 | [`shared`] and [`object`][mod@object] modules. |
| 67 | The [`translate`] module defines and partly implements |
| 68 | conversions between high level Rust types (including the aforementioned |
| 69 | wrappers) and their FFI counterparts. |
| 70 | |
| 71 | ## Documentation |
| 72 | |
| 73 | * [Rust API - Stable](https://gtk-rs.org/gtk-rs-core/stable/latest/docs/glib/) |
| 74 | * [Rust API - Development](https://gtk-rs.org/gtk-rs-core/git/docs/glib) |
| 75 | * [C API](https://docs.gtk.org/glib/) |
| 76 | * [GTK Installation instructions](https://www.gtk.org/docs/installations/) |
| 77 | |
| 78 | ## Using |
| 79 | |
| 80 | We recommend using [crates from crates.io](https://crates.io/keywords/gtk-rs), |
| 81 | as [demonstrated here](https://gtk-rs.org/#using). |
| 82 | |
| 83 | If you want to track the bleeding edge, use the git dependency instead: |
| 84 | |
| 85 | ```toml |
| 86 | [dependencies] |
| 87 | glib = { git = "https://github.com/gtk-rs/gtk-rs-core.git", package = "glib" } |
| 88 | ``` |
| 89 | |
| 90 | Avoid mixing versioned and git crates like this: |
| 91 | |
| 92 | ```toml |
| 93 | # This will not compile |
| 94 | [dependencies] |
| 95 | glib = "0.13" |
| 96 | glib = { git = "https://github.com/gtk-rs/gtk-rs-core.git", package = "glib" } |
| 97 | ``` |
| 98 | |
| 99 | ## License |
| 100 | |
| 101 | __glib__ is available under the MIT License, please refer to it. |
| 102 | |
| 103 | [`Type`]: https://gtk-rs.org/gtk-rs-core/stable/latest/docs/glib/types/struct.Type.html |
| 104 | [`StaticType`]: https://gtk-rs.org/gtk-rs-core/stable/latest/docs/glib/types/trait.StaticType.html |
| 105 | [`Value`]: https://gtk-rs.org/gtk-rs-core/stable/latest/docs/glib/value/struct.Value.html |
| 106 | [`Variant`]: https://gtk-rs.org/gtk-rs-core/stable/latest/docs/glib/variant/struct.Variant.html |
| 107 | [`StaticVariantType`]: https://gtk-rs.org/gtk-rs-core/stable/latest/docs/glib/variant/trait.StaticVariantType.html |
| 108 | [`Error`]: https://gtk-rs.org/gtk-rs-core/stable/latest/docs/glib/error/struct.Error.html |
| 109 | [`FileError`]: https://gtk-rs.org/gtk-rs-core/stable/latest/docs/glib/enum.FileError.html |
| 110 | [`Object`]: https://gtk-rs.org/gtk-rs-core/stable/latest/docs/glib/object/struct.Object.html |
| 111 | [`GObject`]: https://docs.gtk.org/gobject/class.Object.html |
| 112 | [`gtk4::Widget`]: https://gtk-rs.org/gtk4-rs/stable/latest/docs/gtk4/struct.Widget.html |
| 113 | [`GtkWidget`]: https://docs.gtk.org/gtk4/class.Widget.html |
| 114 | [`Rc<RefCell<T>>`]: https://doc.rust-lang.org/stable/core/cell/index.html#introducing-mutability-inside-of-something-immutable |
| 115 | [`IsA`]: https://gtk-rs.org/gtk-rs-core/stable/latest/docs/glib/object/trait.IsA.html |
| 116 | [`Cast`]: https://gtk-rs.org/gtk-rs-core/stable/latest/docs/glib/object/trait.Cast.html |
| 117 | [`ObjectExt`]: https://gtk-rs.org/gtk-rs-core/stable/latest/docs/glib/object/trait.ObjectExt.html |
| 118 | [`WidgetExt`]: https://gtk-rs.org/gtk4-rs/stable/latest/docs/gtk4/prelude/trait.WidgetExt.html |
| 119 | [`wrapper!`]: https://gtk-rs.org/gtk-rs-core/stable/latest/docs/glib/macro.wrapper.html |
| 120 | [`wrapper`]: https://gtk-rs.org/gtk-rs-core/stable/latest/docs/glib/wrapper/index.html |
| 121 | [`boxed`]: https://gtk-rs.org/gtk-rs-core/stable/latest/docs/glib/boxed/index.html |
| 122 | [`shared`]: https://gtk-rs.org/gtk-rs-core/stable/latest/docs/glib/shared/index.html |
| 123 | [mod@object]: https://gtk-rs.org/gtk-rs-core/stable/latest/docs/glib/object/index.html |
| 124 | [`translate`]: https://gtk-rs.org/gtk-rs-core/stable/latest/docs/glib/translate/index.html |