All Projects → sdroege → gobject-example-rs

sdroege / gobject-example-rs

Licence: other
Example for exporting a GObject/C API from Rust

Programming Languages

rust
11053 projects
c
50402 projects - #5 most used programming language
Makefile
30231 projects
vala
323 projects
python
139335 projects - #7 most used programming language
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to gobject-example-rs

Gir
Tool to generate rust bindings and user API for glib-based libraries
Stars: ✭ 124 (+300%)
Mutual labels:  gtk, gnome
Azure-theme
Azure is a clean and fresh blue theme for GTK 3, GTK 2 and Gnome-Shell
Stars: ✭ 100 (+222.58%)
Mutual labels:  gtk, gnome
Adementary Theme
A theme for GNOME/Budgie/Xfce/Cinnamon desktop with elementary-ish aesthetics.
Stars: ✭ 129 (+316.13%)
Mutual labels:  gtk, gnome
Hamster Gtk
A GTK3 time tracker. Powered by 'hamster-lib'
Stars: ✭ 106 (+241.94%)
Mutual labels:  gtk, gnome
Awesome Gtk
List of awesome GTK+ (gtk3) applications
Stars: ✭ 174 (+461.29%)
Mutual labels:  gtk, gnome
Tangram
Run web apps on your desktop.
Stars: ✭ 114 (+267.74%)
Mutual labels:  gtk, gnome
gnomit
A simple Git commit message editor for Gnome.
Stars: ✭ 30 (-3.23%)
Mutual labels:  gtk, gnome
Fragments
Moved to GNOME GitLab -> https://gitlab.gnome.org/haecker-felix/Fragments
Stars: ✭ 80 (+158.06%)
Mutual labels:  gtk, gnome
Automathemely
Simple, set-and-forget python application for changing between desktop themes according to light and dark hours
Stars: ✭ 169 (+445.16%)
Mutual labels:  gtk, gnome
Gtkhash
A cross-platform desktop utility for computing message digests or checksums
Stars: ✭ 167 (+438.71%)
Mutual labels:  gtk, gnome
Clapper
A GNOME media player built using GJS with GTK4 toolkit and powered by GStreamer with OpenGL rendering.
Stars: ✭ 103 (+232.26%)
Mutual labels:  gtk, gnome
Systemtray
Cross-platform SystemTray support for Swing/AWT, GtkStatusIcon, and AppIndicator on Java 8+
Stars: ✭ 253 (+716.13%)
Mutual labels:  gtk, gnome
Vimix Gtk Themes
Vimix is a flat Material Design theme for GTK 3, GTK 2 and Gnome-Shell etc.
Stars: ✭ 1,384 (+4364.52%)
Mutual labels:  gtk, gnome
Arc Theme Red
A red themed derivation of https://github.com/horst3180/arc-theme
Stars: ✭ 117 (+277.42%)
Mutual labels:  gtk, gnome
Gtk Theming Guide
Novice guide towards making your own first gtk theme!
Stars: ✭ 95 (+206.45%)
Mutual labels:  gtk, gnome
Hydrapaper
A Gtk utility to set two different backgrounds for each monitor on GNOME (which lacks this feature)
Stars: ✭ 134 (+332.26%)
Mutual labels:  gtk, gnome
Manjarin Gtk Theme
GTK2 and GTK3 theme developed for the Manjaro-Gnome Edition
Stars: ✭ 30 (-3.23%)
Mutual labels:  gtk, gnome
Sharexin
ShareX for Linux and BSD
Stars: ✭ 79 (+154.84%)
Mutual labels:  gtk, gnome
Fractal
Matrix.org gtk+ client
Stars: ✭ 156 (+403.23%)
Mutual labels:  gtk, gnome
Swiftgtk
A Swift wrapper around gtk-3.x and gtk-4.x that is largely auto-generated from gobject-introspection
Stars: ✭ 180 (+480.65%)
Mutual labels:  gtk, gnome

Example for exporting a GObject/C API from Rust

This repository contains an example Rust crate that can compile to a C-compatible shared library and that exports a GObject C API.

At the same time the API provided by this crate can be used directly from Rust by statically linking this crate as well as by dynamically linking against the C-compatible library. Both variants provide exactly the same Rust API.

In addition it provides a way to generate GObject-Introspection metadata that allows usage from other languages, like Python and JavaScript.

Implemented Example APIs

General Structure

Each type comes with 3 Rust modules

  • mod.rs: Contains a Rust wrapper of the API. This is basically the same as what gir would autogenerate, and follows the same patterns as the GLib, GTK, etc. bindings.
  • imp.rs: Contains the definition of the types together with the actual private implementation, plus an inline FFI module that exports C-compatible FFI functions.
  • ffi.rs: Contains Rust FFI definitions of the exported C types. This is only used in combination with the bindings cargo feature (see below).

and a C header that is (for now, see issue 6) manually written.

Details

Pending refactoring in issue 10.

Usage

Usage from Rust

The API from the mod.rs can directly be used from Rust code and follows the same patterns as the GLib, GTK, etc. bindings.

There is example usage in the inline tests inside the mod.rs of each type.

Statically linked crate

The crate can be directly added as a dependency in some other projects Cargo.toml and then the API from the individual mod.rs is available.

This statically links the implementation into the application.

Running make check would run tests in this mode.

Dynamically linked C library (bindings feature)

When adding the crate as a dependency and enabling the bindings cargo feature then the actual implementation of all the types is omitted. Instead dynamic linking against the implementation from the C-compatible shared library will happen.

The API is otherwise exactly the same.

Running make check-bindings would run tests in this mode.

Usage from C

Running cargo build will create a C-compatible shared library in target/debug/libgobject_example.so. The corresponding headers for the API can be found in the include directory.

test.c contains some example usage of the API and make run-c compiles and runs this.

Usage from Python

Via gobject-introspection a Ex-0.1.typelib file is created. This can be used by pygobject to expose the API directly to Python.

test.py contains some example usage of the API and make run-python runs this.

Usage from JavaScript/GJS

Via gobject-introspection a Ex-0.1.typelib file is created. This can be used by gjs to expose the API directly to JavaScript. An alternative for node.js would be node-gtk.

test.js contains some example usage of the API and make run-gjs runs this.

Usage from Vala

Via gobject-introspection a Ex-0.1.gir file is created that contains an XML representation of the API. Vala can directly make use of this.

test.vala contains some example usage of the API and make run-vala runs this.

Usage from other languages

The gobject-introspection .gir and .typelib files can be used to autogenerate bindings for dozens of different languages.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].