-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathattr.object_interface.html
88 lines (83 loc) · 8.88 KB
/
attr.object_interface.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Macro for boilerplate of `ObjectInterface` implementations."><title>object_interface in glib - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-46132b98.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="glib" data-themes="" data-resource-suffix="" data-rustdoc-version="1.85.0 (4d91de4e4 2025-02-17)" data-channel="1.85.0" data-search-js="search-75f5ac3e.js" data-settings-js="settings-0f613d39.js" ><script src="../static.files/storage-59e33391.js"></script><script defer src="sidebar-items.js"></script><script defer src="../static.files/main-5f194d8c.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-893ab5e7.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-6580c154.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-044be391.svg"></head><body class="rustdoc attr"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../glib/index.html">glib</a><span class="version">0.20.9</span></h2></div><div class="sidebar-elems"><div id="rustdoc-modnav"></div></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><span class="rustdoc-breadcrumbs"><a href="index.html">glib</a></span><h1>Attribute Macro <span class="attr">object_interface</span><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../src/glib_macros/lib.rs.html#1149">Source</a> </span></div><pre class="rust item-decl"><code>#[object_interface]</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Macro for boilerplate of <a href="../glib/subclass/interface/trait.ObjectInterface.html"><code>ObjectInterface</code></a> implementations.</p>
<p>This adds implementations for the <code>get_type()</code> method, which should probably never be defined
differently.</p>
<p>It provides default values for the <code>Prerequisites</code> type parameter. If this is present, the macro
will use the provided value instead of the default.</p>
<p><code>Prerequisites</code> are interfaces for types that require a specific base class or interfaces.</p>
<div class="example-wrap ignore"><a href="#" class="tooltip" title="This example is not tested">ⓘ</a><pre class="rust rust-example-rendered"><code><span class="kw">type </span>Prerequisites = ();</code></pre></div>
<p>An object interface can be registered as a dynamic type by setting the
macro helper attribute <code>object_interface_dynamic</code>:</p>
<div class="example-wrap ignore"><a href="#" class="tooltip" title="This example is not tested">ⓘ</a><pre class="rust rust-example-rendered"><code><span class="kw">pub struct </span>MyInterface {
parent: glib::gobject_ffi::GTypeInterface,
}
<span class="attr">#[glib::object_interface]
#[object_interface_dynamic]
</span><span class="kw">unsafe impl </span>ObjectInterface <span class="kw">for </span>MyInterface { ... }</code></pre></div>
<p>As a dynamic type, an object interface must be explicitly registered when
the system loads the implementation (see <a href="../glib/gobject/type_plugin/struct.TypePlugin.html"><code>TypePlugin</code></a> and <a href="../glib/gobject/type_module/struct.TypeModule.html"><code>TypeModule</code></a>).
Therefore, whereas an object interface can be registered only once as a
static type, it can be registered several times as a dynamic type.</p>
<p>An object interface registered as a dynamic type is never unregistered. The
system calls <a href="../glib/gobject/type_plugin/trait.TypePluginExt.html#method.unuse///"><code>TypePluginExt::unuse</code></a> to unload the implementation. If the
<a href="../glib/gobject/type_plugin/struct.TypePlugin.html"><code>TypePlugin</code></a> subclass is a <a href="../glib/gobject/type_module/struct.TypeModule.html"><code>TypeModule</code></a>, the object interface
registered as a dynamic type is marked as unloaded and must be registered
again when the module is reloaded.</p>
<p>The macro helper attribute <code>object_interface_dynamic</code> provides two
behaviors when registering an object interface as a dynamic type:</p>
<ul>
<li>lazy registration: by default an object interface is registered as a
dynamic type when the system loads the implementation (e.g. when the module
is loaded). Optionally setting <code>lazy_registration</code> to <code>true</code> postpones
registration on the first use (when <code>type_()</code> is called for the first time):</li>
</ul>
<div class="example-wrap ignore"><a href="#" class="tooltip" title="This example is not tested">ⓘ</a><pre class="rust rust-example-rendered"><code><span class="kw">pub struct </span>MyInterface {
parent: glib::gobject_ffi::GTypeInterface,
}
<span class="attr">#[glib::object_interface]
#[object_interface_dynamic(lazy_registration = <span class="bool-val">true</span>)]
</span><span class="kw">unsafe impl </span>ObjectInterface <span class="kw">for </span>MyInterface { ... }</code></pre></div>
<ul>
<li>registration within <a href="../glib/gobject/type_module/struct.TypeModule.html"><code>TypeModule</code></a> subclass or within <a href="../glib/gobject/type_plugin/struct.TypePlugin.html"><code>TypePlugin</code></a>
subclass: an object interface is usually registered as a dynamic type
within a <a href="../glib/gobject/type_module/struct.TypeModule.html"><code>TypeModule</code></a> subclass:</li>
</ul>
<div class="example-wrap ignore"><a href="#" class="tooltip" title="This example is not tested">ⓘ</a><pre class="rust rust-example-rendered"><code><span class="kw">pub struct </span>MyModuleInterface {
parent: glib::gobject_ffi::GTypeInterface,
}
<span class="attr">#[glib::object_interface]
#[object_interface_dynamic]
</span><span class="kw">unsafe impl </span>ObjectInterface <span class="kw">for </span>MyModuleInterface { ... }
...
<span class="attr">#[derive(Default)]
</span><span class="kw">pub struct </span>MyModule;
...
<span class="kw">impl </span>TypeModuleImpl <span class="kw">for </span>MyModule {
<span class="kw">fn </span>load(<span class="kw-2">&</span><span class="self">self</span>) -> bool {
<span class="comment">// registers object interfaces as dynamic types.
</span><span class="kw">let </span>my_module = <span class="self">self</span>.obj();
<span class="kw">let </span>type_module: <span class="kw-2">&</span>glib::TypeModule = my_module.upcast_ref();
MyModuleInterface::on_implementation_load(type_module)
}
...
}</code></pre></div>
<p>Optionally setting <code>plugin_type</code> allows to register an object interface as
a dynamic type within a <a href="../glib/gobject/type_plugin/struct.TypePlugin.html"><code>TypePlugin</code></a> subclass that is not a <a href="../glib/gobject/type_module/struct.TypeModule.html"><code>TypeModule</code></a>:</p>
<div class="example-wrap ignore"><a href="#" class="tooltip" title="This example is not tested">ⓘ</a><pre class="rust rust-example-rendered"><code><span class="kw">pub struct </span>MyPluginInterface {
parent: glib::gobject_ffi::GTypeInterface,
}
<span class="attr">#[glib::object_interface]
#[object_interface_dynamic(plugin_type = MyPlugin)]
</span><span class="kw">unsafe impl </span>ObjectInterface <span class="kw">for </span>MyPluginInterface { ... }
...
<span class="attr">#[derive(Default)]
</span><span class="kw">pub struct </span>MyPlugin;
...
<span class="kw">impl </span>TypePluginImpl <span class="kw">for </span>MyPlugin {
<span class="kw">fn </span>use_plugin(<span class="kw-2">&</span><span class="self">self</span>) {
<span class="comment">// register object interfaces as dynamic types.
</span><span class="kw">let </span>my_plugin = <span class="self">self</span>.obj();
MyPluginInterface::on_implementation_load(my_plugin.as_ref());
}
...
}</code></pre></div>
</div></details></section></div></main></body></html>