| 1 |
# Handling generation errors |
| 2 |
|
| 3 |
Luckily there are only a few errors which can happen with [gir] generation. |
| 4 |
Let's take a look at them. |
| 5 |
|
| 6 |
### Cannot find macros |
| 7 |
|
| 8 |
Compilation of the generated bindings may fail with errors like the following: |
| 9 |
|
| 10 |
```console |
| 11 |
error: cannot find macro `skip_assert_initialized` in this scope |
| 12 |
--> src/auto/enums.rs:83:9 |
| 13 |
| |
| 14 |
83 | skip_assert_initialized!(); |
| 15 |
| ^^^^^^^^^^^^^^^^^^^^^^^ |
| 16 |
| |
| 17 |
= help: have you added the `#[macro_use]` on the module/import? |
| 18 |
|
| 19 |
error: cannot find macro `assert_initialized_main_thread` in this scope |
| 20 |
--> src/auto/example.rs:33:9 |
| 21 |
| |
| 22 |
33 | assert_initialized_main_thread!(); |
| 23 |
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 24 |
| |
| 25 |
= help: have you added the `#[macro_use]` on the module/import? |
| 26 |
``` |
| 27 |
|
| 28 |
In this case you’ll have to implement them yourself. |
| 29 |
Macros are order-dependent and you *must* insert this code before declaring modules that use it (e.g. `mod auto`). |
| 30 |
For example, you can add the following to your `lib.rs` file: |
| 31 |
|
| 32 |
```rust |
| 33 |
/// No-op. |
| 34 |
macro_rules! skip_assert_initialized { |
| 35 |
() => {}; |
| 36 |
} |
| 37 |
|
| 38 |
/// Asserts that this is the main thread and either `gdk::init` or `gtk::init` has been called. |
| 39 |
macro_rules! assert_initialized_main_thread { |
| 40 |
() => { |
| 41 |
if !::gtk::is_initialized_main_thread() { |
| 42 |
if ::gtk::is_initialized() { |
| 43 |
panic!("GTK may only be used from the main thread."); |
| 44 |
} else { |
| 45 |
panic!("GTK has not been initialized. Call `gtk::init` first."); |
| 46 |
} |
| 47 |
} |
| 48 |
}; |
| 49 |
} |
| 50 |
``` |
| 51 |
|
| 52 |
|
| 53 |
One complication here is that the `assert_initialized_main_thread!` macro depends on the exact library. |
| 54 |
If it's GTK-based then the above macro is likely correct, unless the library has its own initialization function. |
| 55 |
If it has its own initialization function it would need to be handled in addition to GTK's here in the same way. |
| 56 |
|
| 57 |
For non-GTK-based libraries the following macro would handle the initialization function of that library in the same way, or if there is none it would simply do nothing: |
| 58 |
|
| 59 |
```rust |
| 60 |
/// No-op. |
| 61 |
macro_rules! assert_initialized_main_thread { |
| 62 |
() => {}; |
| 63 |
} |
| 64 |
``` |
| 65 |
|
| 66 |
## Missing memory management functions |
| 67 |
|
| 68 |
If [gir] generation fails (for whatever reason), it means you'll have to implement the type yourself. |
| 69 |
Just like types from other `gtk-rs` crates, you'll need to put it into the "manual" list. |
| 70 |
Then you need to put the type into the `src` folder (or inside a subfolder, you know how Rust works). |
| 71 |
|
| 72 |
/!\ Don't forget to reexport the type inside your `src/lib.rs` file. |
| 73 |
For example, let's take a look at the [requisition.rs](https://github.com/gtk-rs/gtk3-rs/blob/master/gtk/src/requisition.rs) file from the `gtk3` crate. |
| 74 |
|
| 75 |
Since it's a "simple" type (no pointer, therefore no memory management to do), [gir] doesn't know how to generate it. |
| 76 |
You'll need to implement some traits by hand like `ToGlibPtr` or `ToGlibPtrMut` (depending on your needs). |
| 77 |
|
| 78 |
## Bad function generation |
| 79 |
In some cases, the generated code isn't correct (array parameters are often an issue). |
| 80 |
In such cases, it's better to just make the implementation yourself. |
| 81 |
As an example, let's say you want to implement `Region::is_empty` yourself. |
| 82 |
A few changes have to be made. |
| 83 |
Let's start with `Gir.toml`: |
| 84 |
|
| 85 |
```toml |
| 86 |
generate = [ |
| 87 |
"GtkSource.Language", |
| 88 |
] |
| 89 |
|
| 90 |
[[object]] |
| 91 |
name = "GtkSource.Region" |
| 92 |
status = "generate" |
| 93 |
[[object.function]] |
| 94 |
name = "is_empty" |
| 95 |
ignore = true |
| 96 |
``` |
| 97 |
|
| 98 |
So to sum up what was written above: we removed "GtkSource.Region" from the "generate" list and we created a new entry for it. |
| 99 |
Then we say to [gir] that it should generate (through `status = "generate"`). |
| 100 |
However, we also tell it that we don't want the "is_empty" function to be generated. |
| 101 |
|
| 102 |
Now that we've done that, we need to implement it. |
| 103 |
Let's create a `src/region.rs` file: |
| 104 |
|
| 105 |
```rust |
| 106 |
use glib::object::IsA; |
| 107 |
use glib::translate::*; |
| 108 |
use Region; |
| 109 |
|
| 110 |
pub trait RegionExtManual: 'static { |
| 111 |
pub fn is_empty(&self) -> bool; |
| 112 |
} |
| 113 |
|
| 114 |
impl<O: IsA<Region>> RegionExtManual for O { |
| 115 |
pub fn is_empty(&self) -> bool { |
| 116 |
// blablabla |
| 117 |
true |
| 118 |
} |
| 119 |
} |
| 120 |
``` |
| 121 |
|
| 122 |
You might wonder: "why not just implementing it on the `Region` type directly?". |
| 123 |
Because like this, a subclass will also be able to use this trait easily as long as it implements `IsA<Region>`. |
| 124 |
For instance, in gtk, everything that implements `IsA<Widget>` (so almost every GTK types) can use those methods. |
| 125 |
|
| 126 |
As usual, don't forget to reexport the trait. |
| 127 |
A little tip about reexporting manual traits: in `gtk3-rs`, we create a `src/prelude.rs` file which reexports all traits (both manual and generated ones), making it simpler for users to use them through `use [DEPENDENCY]::prelude::*`. |
| 128 |
The `src/prelude.rs` file looks like this: |
| 129 |
|
| 130 |
```rust |
| 131 |
pub use crate::auto::traits::*; |
| 132 |
pub use region::RegionExtManual; |
| 133 |
``` |
| 134 |
|
| 135 |
Then it's reexported as follows from the `src/lib.rs` file: |
| 136 |
|
| 137 |
```rust |
| 138 |
pub mod prelude; |
| 139 |
pub use prelude::*; |
| 140 |
``` |
| 141 |
|
| 142 |
## Manually defined traits missing from the documentation |
| 143 |
If you defined traits manually, you can add them to the "Implements" section in the documentation for classes and interfaces by using the `manual_traits = []` option in the `Gir.toml` file. |
| 144 |
Here is an example: |
| 145 |
|
| 146 |
```toml |
| 147 |
[[object]] |
| 148 |
name = "Gtk.Assistant" |
| 149 |
status = "generate" |
| 150 |
#add link to trait from current crate |
| 151 |
manual_traits = ["AssistantExtManual"] |
| 152 |
|
| 153 |
[[object]] |
| 154 |
name = "Gtk.Application" |
| 155 |
status = "generate" |
| 156 |
#add link to trait from other crate |
| 157 |
manual_traits = ["gio::ApplicationExtManual"] |
| 158 |
``` |
| 159 |
|
| 160 |
|
| 161 |
[gir]: https://github.com/gtk-rs/gir |