Debian Codegraph @

tests/object_subclass_dynamic.rs

raw symbols SCIP
1 // Take a look at the license at the top of the repository in the LICENSE file.
2
3 use glib::{prelude::*, subclass::prelude::*};
4
5 mod static_ {
6 use super::*;
7
8 pub mod imp {
9 use super::*;
10
11 // impl for an object interface to register as a static type.
12 #[derive(Clone, Copy)]
13 #[repr(C)]
14 pub struct MyStaticInterfaceClass {
15 parent: glib::gobject_ffi::GTypeInterface,
16 }
17
18 unsafe impl InterfaceStruct for MyStaticInterfaceClass {
19 type Type = MyStaticInterface;
20 }
21
22 pub enum MyStaticInterface {}
23
24 #[glib::object_interface]
25 impl ObjectInterface for MyStaticInterface {
26 const NAME: &'static str = "MyStaticInterface";
27
28 type Interface = MyStaticInterfaceClass;
29 }
30
31 pub trait MyStaticInterfaceImpl:
32 ObjectImpl + ObjectSubclass<Type: IsA<super::MyStaticInterface>>
33 {
34 }
35
36 // impl for an object subclass to register as a static type and that implements `MyStaticInterface`.
37 #[derive(Default)]
38 pub struct MyStaticType;
39
40 #[glib::object_subclass]
41 impl ObjectSubclass for MyStaticType {
42 const NAME: &'static str = "MyStaticType";
43 type Type = super::MyStaticType;
44 type Interfaces = (super::MyStaticInterface,);
45 }
46
47 impl ObjectImpl for MyStaticType {}
48
49 impl MyStaticInterfaceImpl for MyStaticType {}
50
51 pub trait MyStaticTypeImpl:
52 ObjectImpl + ObjectSubclass<Type: IsA<super::MyStaticType>>
53 {
54 }
55 }
56
57 // an object interface to register as a static type.
58 glib::wrapper! {
59 pub struct MyStaticInterface(ObjectInterface<imp::MyStaticInterface>);
60 }
61
62 unsafe impl<T: imp::MyStaticInterfaceImpl> IsImplementable<T> for MyStaticInterface {}
63
64 // an object subclass to register as a static type and that implements `MyStaticInterface`.
65 glib::wrapper! {
66 pub struct MyStaticType(ObjectSubclass<imp::MyStaticType>) @implements MyStaticInterface;
67 }
68
69 unsafe impl<T: imp::MyStaticTypeImpl> IsSubclassable<T> for MyStaticType {}
70 }
71
72 use static_::{
73 imp::{MyStaticInterfaceImpl, MyStaticTypeImpl},
74 *,
75 };
76
77 mod module {
78 use super::*;
79
80 mod imp {
81 use super::*;
82
83 // impl for a object interface to register as a dynamic type and that extends `MyStaticInterface`.
84 #[derive(Clone, Copy)]
85 #[repr(C)]
86 pub struct MyModuleInterfaceClass {
87 parent: glib::gobject_ffi::GTypeInterface,
88 }
89
90 unsafe impl InterfaceStruct for MyModuleInterfaceClass {
91 type Type = MyModuleInterface;
92 }
93
94 pub enum MyModuleInterface {}
95
96 #[glib::object_interface]
97 #[object_interface_dynamic]
98 impl ObjectInterface for MyModuleInterface {
99 const NAME: &'static str = "MyModuleInterface";
100 type Prerequisites = (MyStaticInterface,);
101 type Interface = MyModuleInterfaceClass;
102 }
103
104 pub trait MyModuleInterfaceImpl:
105 ObjectImpl + ObjectSubclass<Type: IsA<super::MyModuleInterface>>
106 {
107 }
108
109 // impl for an object subclass to register as a dynamic type and that extends `MyStaticType` and that implements `MyStaticInterface` and `MyModuleInterface`.
110 #[derive(Default)]
111 pub struct MyModuleType;
112
113 #[glib::object_subclass]
114 #[object_subclass_dynamic]
115 impl ObjectSubclass for MyModuleType {
116 const NAME: &'static str = "MyModuleType";
117 type Type = super::MyModuleType;
118 type ParentType = MyStaticType;
119 type Interfaces = (MyStaticInterface, super::MyModuleInterface);
120 }
121
122 impl ObjectImpl for MyModuleType {}
123
124 impl MyStaticTypeImpl for MyModuleType {}
125
126 impl MyStaticInterfaceImpl for MyModuleType {}
127
128 impl MyModuleInterfaceImpl for MyModuleType {}
129
130 // impl for an object interface to lazy register as a dynamic type and that extends `MyStaticInterface`.
131 #[derive(Clone, Copy)]
132 #[repr(C)]
133 pub struct MyModuleInterfaceLazyClass {
134 parent: glib::gobject_ffi::GTypeInterface,
135 }
136
137 unsafe impl InterfaceStruct for MyModuleInterfaceLazyClass {
138 type Type = MyModuleInterfaceLazy;
139 }
140
141 pub enum MyModuleInterfaceLazy {}
142
143 #[glib::object_interface]
144 #[object_interface_dynamic(lazy_registration = true)]
145 impl ObjectInterface for MyModuleInterfaceLazy {
146 const NAME: &'static str = "MyModuleInterfaceLazy";
147 type Prerequisites = (MyStaticInterface,);
148 type Interface = MyModuleInterfaceLazyClass;
149 }
150
151 pub trait MyModuleInterfaceLazyImpl:
152 ObjectImpl + ObjectSubclass<Type: IsA<super::MyModuleInterfaceLazy>>
153 {
154 }
155
156 // impl for an object subclass to lazy register as a dynamic type and that extends `MyStaticType` and that implements `MyStaticInterface` and `MyModuleInterfaceLazy`.
157 #[derive(Default)]
158 pub struct MyModuleTypeLazy;
159
160 #[glib::object_subclass]
161 #[object_subclass_dynamic(lazy_registration = true)]
162 impl ObjectSubclass for MyModuleTypeLazy {
163 const NAME: &'static str = "MyModuleTypeLazy";
164 type Type = super::MyModuleTypeLazy;
165 type ParentType = MyStaticType;
166 type Interfaces = (MyStaticInterface, super::MyModuleInterfaceLazy);
167 }
168
169 impl ObjectImpl for MyModuleTypeLazy {}
170
171 impl MyStaticTypeImpl for MyModuleTypeLazy {}
172
173 impl MyStaticInterfaceImpl for MyModuleTypeLazy {}
174
175 impl MyModuleInterfaceLazyImpl for MyModuleTypeLazy {}
176
177 // impl for a type module (must extend `glib::TypeModule` and must implement `glib::TypePlugin`).
178 #[derive(Default)]
179 pub struct MyModule;
180
181 #[glib::object_subclass]
182 impl ObjectSubclass for MyModule {
183 const NAME: &'static str = "MyModule";
184 type Type = super::MyModule;
185 type ParentType = glib::TypeModule;
186 type Interfaces = (glib::TypePlugin,);
187 }
188
189 impl ObjectImpl for MyModule {}
190
191 impl TypePluginImpl for MyModule {}
192
193 impl TypeModuleImpl for MyModule {
194 fn load(&self) -> bool {
195 // registers object subclasses and interfaces as dynamic types.
196 let my_module = self.obj();
197 let type_module: &glib::TypeModule = my_module.upcast_ref();
198 MyModuleInterface::on_implementation_load(type_module)
199 && MyModuleType::on_implementation_load(type_module)
200 && MyModuleInterfaceLazy::on_implementation_load(type_module)
201 && MyModuleTypeLazy::on_implementation_load(type_module)
202 }
203
204 fn unload(&self) {
205 // marks object subclasses and interfaces as unregistered.
206 let my_module = self.obj();
207 let type_module: &glib::TypeModule = my_module.upcast_ref();
208 MyModuleTypeLazy::on_implementation_unload(type_module);
209 MyModuleInterfaceLazy::on_implementation_unload(type_module);
210 MyModuleType::on_implementation_unload(type_module);
211 MyModuleInterface::on_implementation_unload(type_module);
212 }
213 }
214 }
215
216 // an object interface to register as a dynamic type and that extends `MyStaticInterface`.
217 glib::wrapper! {
218 pub struct MyModuleInterface(ObjectInterface<imp::MyModuleInterface>) @requires MyStaticInterface;
219 }
220
221 unsafe impl<T: imp::MyModuleInterfaceImpl> IsImplementable<T> for MyModuleInterface {}
222
223 // an object subclass to register as a dynamic type and that extends `MyStaticType` and that implements `MyStaticInterface` and `MyModuleInterface`.
224 glib::wrapper! {
225 pub struct MyModuleType(ObjectSubclass<imp::MyModuleType>) @extends MyStaticType, @implements MyStaticInterface, MyModuleInterface;
226 }
227
228 // an object interface to lazy register as a dynamic type and that extends `MyStaticInterface`.
229 glib::wrapper! {
230 pub struct MyModuleInterfaceLazy(ObjectInterface<imp::MyModuleInterfaceLazy>) @requires MyStaticInterface;
231 }
232
233 unsafe impl<T: imp::MyModuleInterfaceLazyImpl> IsImplementable<T> for MyModuleInterfaceLazy {}
234
235 // an object subclass to lazy register as a dynamic type and that extends `MyStaticType` that implements `MyStaticInterface` and `MyModuleInterfaceLazy`.
236 glib::wrapper! {
237 pub struct MyModuleTypeLazy(ObjectSubclass<imp::MyModuleTypeLazy>) @extends MyStaticType, @implements MyStaticInterface, MyModuleInterfaceLazy;
238 }
239
240 // a module (must extend `glib::TypeModule` and must implement `glib::TypePlugin`).
241 glib::wrapper! {
242 pub struct MyModule(ObjectSubclass<imp::MyModule>)
243 @extends glib::TypeModule, @implements glib::TypePlugin;
244 }
245
246 #[test]
247 fn dynamic_object_subclasses() {
248 use glib::prelude::TypeModuleExt;
249
250 // checks types of object subclasses and of object interfaces to register as dynamic types are invalid (module is not loaded yet).
251 assert!(!imp::MyModuleInterface::type_().is_valid());
252 assert!(!imp::MyModuleType::type_().is_valid());
253 assert!(!imp::MyModuleInterfaceLazy::type_().is_valid());
254 assert!(!imp::MyModuleTypeLazy::type_().is_valid());
255
256 // simulates the GLib type system to load/unload the module.
257 let module = glib::Object::new::<MyModule>();
258 TypeModuleExt::use_(&module);
259 TypeModuleExt::unuse(&module);
260
261 // checks types of object subclasses and of object interfaces registered as dynamic types are valid (module is unloaded).
262 assert!(imp::MyModuleInterface::type_().is_valid());
263 assert!(imp::MyModuleType::type_().is_valid());
264 // checks types of object subclasses and of object interfaces that are lazy registered as dynamic types are still invalid (module is unloaded).
265 assert!(!imp::MyModuleInterfaceLazy::type_().is_valid());
266 assert!(!imp::MyModuleTypeLazy::type_().is_valid());
267
268 // simulates the GLib type system to load the module.
269 TypeModuleExt::use_(&module);
270
271 // checks types of object subclasses and of object interfaces registered as dynamic types are valid (module is loaded).
272 let iface_type = imp::MyModuleInterface::type_();
273 assert!(iface_type.is_valid());
274 let obj_type = imp::MyModuleType::type_();
275 assert!(obj_type.is_valid());
276 let iface_lazy_type = imp::MyModuleInterfaceLazy::type_();
277 assert!(iface_lazy_type.is_valid());
278 let obj_lazy_type = imp::MyModuleTypeLazy::type_();
279 assert!(obj_lazy_type.is_valid());
280
281 // checks plugin of object subclasses and of object interfaces is `MyModule`.
282 assert_eq!(
283 iface_type.plugin().as_ref(),
284 Some(module.upcast_ref::<glib::TypePlugin>())
285 );
286 assert_eq!(
287 obj_type.plugin().as_ref(),
288 Some(module.upcast_ref::<glib::TypePlugin>())
289 );
290 assert_eq!(
291 iface_lazy_type.plugin().as_ref(),
292 Some(module.upcast_ref::<glib::TypePlugin>())
293 );
294 assert_eq!(
295 obj_lazy_type.plugin().as_ref(),
296 Some(module.upcast_ref::<glib::TypePlugin>())
297 );
298
299 // simulates the GLib type system to unload the module.
300 TypeModuleExt::unuse(&module);
301
302 // checks types of object subclasses and of object interfaces registered as dynamic types are still valid (should have been marked as unloaded by the GLib type system but this cannot be checked).
303 assert!(imp::MyModuleInterface::type_().is_valid());
304 assert!(imp::MyModuleType::type_().is_valid());
305 assert!(imp::MyModuleInterfaceLazy::type_().is_valid());
306 assert!(imp::MyModuleTypeLazy::type_().is_valid());
307
308 // simulates the GLib type system to reload the module.
309 TypeModuleExt::use_(&module);
310
311 // checks types of object subclasses and of object interfaces registered as dynamic types are still valid (should have been marked as unloaded by the GLib type system but this cannot be checked).
312 assert!(imp::MyModuleInterface::type_().is_valid());
313 assert!(imp::MyModuleType::type_().is_valid());
314 assert!(imp::MyModuleInterfaceLazy::type_().is_valid());
315 assert!(imp::MyModuleTypeLazy::type_().is_valid());
316
317 // simulates the GLib type system to unload the module.
318 TypeModuleExt::unuse(&module);
319 }
320 }
321
322 pub mod plugin {
323 use super::*;
324
325 pub mod imp {
326 use super::*;
327 use std::cell::Cell;
328
329 // impl for a object interface to register as a dynamic type and that extends `MyStaticInterface`.
330 #[derive(Clone, Copy)]
331 #[repr(C)]
332 pub struct MyPluginInterfaceClass {
333 parent: glib::gobject_ffi::GTypeInterface,
334 }
335
336 unsafe impl InterfaceStruct for MyPluginInterfaceClass {
337 type Type = MyPluginInterface;
338 }
339
340 pub enum MyPluginInterface {}
341
342 #[glib::object_interface]
343 #[object_interface_dynamic(plugin_type = super::MyPlugin)]
344 impl ObjectInterface for MyPluginInterface {
345 const NAME: &'static str = "MyPluginInterface";
346 type Prerequisites = (MyStaticInterface,);
347 type Interface = MyPluginInterfaceClass;
348 }
349
350 pub trait MyPluginInterfaceImpl:
351 ObjectImpl + ObjectSubclass<Type: IsA<super::MyPluginInterface>>
352 {
353 }
354
355 // impl for an object subclass to register as a dynamic type and that extends `MyStaticType` and that implements `MyStaticInterface` and `MyPluginInterface`.
356 #[derive(Default)]
357 pub struct MyPluginType;
358
359 #[glib::object_subclass]
360 #[object_subclass_dynamic(plugin_type = super::MyPlugin)]
361 impl ObjectSubclass for MyPluginType {
362 const NAME: &'static str = "MyPluginType";
363 type Type = super::MyPluginType;
364 type ParentType = MyStaticType;
365 type Interfaces = (MyStaticInterface, super::MyPluginInterface);
366 }
367
368 impl ObjectImpl for MyPluginType {}
369
370 impl MyStaticTypeImpl for MyPluginType {}
371
372 impl MyStaticInterfaceImpl for MyPluginType {}
373
374 impl MyPluginInterfaceImpl for MyPluginType {}
375
376 // impl for an object interface to lazy register as a dynamic type and that extends `MyStaticInterface`.
377 #[derive(Clone, Copy)]
378 #[repr(C)]
379 pub struct MyPluginInterfaceLazyClass {
380 parent: glib::gobject_ffi::GTypeInterface,
381 }
382
383 unsafe impl InterfaceStruct for MyPluginInterfaceLazyClass {
384 type Type = MyPluginInterfaceLazy;
385 }
386
387 pub enum MyPluginInterfaceLazy {}
388
389 #[glib::object_interface]
390 #[object_interface_dynamic(plugin_type = super::MyPlugin, lazy_registration = true)]
391 impl ObjectInterface for MyPluginInterfaceLazy {
392 const NAME: &'static str = "MyPluginInterfaceLazy";
393 type Prerequisites = (MyStaticInterface,);
394 type Interface = MyPluginInterfaceLazyClass;
395 }
396
397 pub trait MyPluginInterfaceLazyImpl:
398 ObjectImpl + ObjectSubclass<Type: IsA<super::MyPluginInterfaceLazy>>
399 {
400 }
401
402 // impl for an object subclass to lazy register as a dynamic type and that extends `MyStaticType` and that implements `MyStaticInterface` and `MyPluginInterfaceLazy`.
403 #[derive(Default)]
404 pub struct MyPluginTypeLazy;
405
406 #[glib::object_subclass]
407 #[object_subclass_dynamic(plugin_type = super::MyPlugin, lazy_registration = true)]
408 impl ObjectSubclass for MyPluginTypeLazy {
409 const NAME: &'static str = "MyPluginTypeLazy";
410 type Type = super::MyPluginTypeLazy;
411 type ParentType = MyStaticType;
412 type Interfaces = (MyStaticInterface, super::MyPluginInterfaceLazy);
413 }
414
415 impl ObjectImpl for MyPluginTypeLazy {}
416
417 impl MyStaticTypeImpl for MyPluginTypeLazy {}
418
419 impl MyStaticInterfaceImpl for MyPluginTypeLazy {}
420
421 impl MyPluginInterfaceLazyImpl for MyPluginTypeLazy {}
422
423 // impl for a type plugin (must implement `glib::TypePlugin`).
424 #[derive(Default)]
425 pub struct MyPlugin {
426 my_interface_type_info: Cell<Option<glib::TypeInfo>>,
427 my_interface_iface_info: Cell<Option<glib::InterfaceInfo>>,
428 my_type_type_info: Cell<Option<glib::TypeInfo>>,
429 my_interface_lazy_type_info: Cell<Option<glib::TypeInfo>>,
430 my_interface_lazy_iface_info: Cell<Option<glib::InterfaceInfo>>,
431 my_type_lazy_type_info: Cell<Option<glib::TypeInfo>>,
432 }
433
434 #[glib::object_subclass]
435 impl ObjectSubclass for MyPlugin {
436 const NAME: &'static str = "MyPlugin";
437 type Type = super::MyPlugin;
438 type Interfaces = (glib::TypePlugin,);
439 }
440
441 impl ObjectImpl for MyPlugin {}
442
443 impl TypePluginImpl for MyPlugin {
444 fn use_plugin(&self) {
445 // registers object subclasses and interfaces as dynamic types.
446 let my_plugin = self.obj();
447 MyPluginInterface::on_implementation_load(my_plugin.as_ref());
448 MyPluginType::on_implementation_load(my_plugin.as_ref());
449 MyPluginInterfaceLazy::on_implementation_load(my_plugin.as_ref());
450 MyPluginTypeLazy::on_implementation_load(my_plugin.as_ref());
451 }
452
453 fn unuse_plugin(&self) {
454 // marks object subclasses and interfaces as unregistered.
455 let my_plugin = self.obj();
456 MyPluginTypeLazy::on_implementation_unload(my_plugin.as_ref());
457 MyPluginInterfaceLazy::on_implementation_unload(my_plugin.as_ref());
458 MyPluginType::on_implementation_unload(my_plugin.as_ref());
459 MyPluginInterface::on_implementation_unload(my_plugin.as_ref());
460 }
461
462 fn complete_type_info(
463 &self,
464 type_: glib::Type,
465 ) -> (glib::TypeInfo, glib::TypeValueTable) {
466 let info = match type_ {
467 type_ if type_ == MyPluginType::type_() => self.my_type_type_info.get(),
468 type_ if type_ == MyPluginInterface::type_() => {
469 self.my_interface_type_info.get()
470 }
471 type_ if type_ == MyPluginTypeLazy::type_() => {
472 self.my_type_lazy_type_info.get()
473 }
474 type_ if type_ == MyPluginInterfaceLazy::type_() => {
475 self.my_interface_lazy_type_info.get()
476 }
477 _ => panic!("unexpected"),
478 };
479 match info {
480 Some(info) => (info, glib::TypeValueTable::default()),
481 _ => panic!("unexpected"),
482 }
483 }
484
485 fn complete_interface_info(
486 &self,
487 _instance_type: glib::Type,
488 interface_type: glib::Type,
489 ) -> glib::InterfaceInfo {
490 let info = match interface_type {
491 type_ if type_ == MyPluginInterface::type_() => {
492 self.my_interface_iface_info.get()
493 }
494 type_ if type_ == MyPluginInterfaceLazy::type_() => {
495 self.my_interface_lazy_iface_info.get()
496 }
497 _ => panic!("unexpected"),
498 };
499 match info {
500 Some(info) => info,
501 _ => panic!("unexpected"),
502 }
503 }
504 }
505
506 impl TypePluginRegisterImpl for MyPlugin {
507 fn add_dynamic_interface(
508 &self,
509 instance_type: glib::Type,
510 interface_type: glib::Type,
511 interface_info: &glib::InterfaceInfo,
512 ) {
513 if !instance_type.is_a(interface_type) {
514 instance_type.add_interface_dynamic(
515 interface_type,
516 self.obj().upcast_ref::<glib::TypePlugin>(),
517 );
518 }
519 match interface_type {
520 type_ if type_ == imp::MyPluginInterface::type_() => {
521 self.my_interface_iface_info.set(Some(*interface_info))
522 }
523 type_ if type_ == imp::MyPluginInterfaceLazy::type_() => {
524 self.my_interface_lazy_iface_info.set(Some(*interface_info))
525 }
526 _ => panic!("unexpected"),
527 };
528 }
529
530 fn register_dynamic_type(
531 &self,
532 parent_type: glib::Type,
533 type_name: &str,
534 type_info: &glib::TypeInfo,
535 flags: glib::TypeFlags,
536 ) -> glib::Type {
537 let type_ = glib::Type::from_name(type_name).unwrap_or_else(|| {
538 glib::Type::register_dynamic(
539 parent_type,
540 type_name,
541 self.obj().upcast_ref::<glib::TypePlugin>(),
542 flags,
543 )
544 });
545 if type_.is_valid() {
546 match type_name {
547 imp::MyPluginType::NAME => self.my_type_type_info.set(Some(*type_info)),
548 imp::MyPluginInterface::NAME => {
549 self.my_interface_type_info.set(Some(*type_info))
550 }
551 imp::MyPluginTypeLazy::NAME => {
552 self.my_type_lazy_type_info.set(Some(*type_info))
553 }
554 imp::MyPluginInterfaceLazy::NAME => {
555 self.my_interface_lazy_type_info.set(Some(*type_info))
556 }
557 _ => panic!("unexpected"),
558 };
559 }
560 type_
561 }
562 }
563 }
564
565 // an object interface to register as a dynamic type and that extends `MyStaticInterface`.
566 glib::wrapper! {
567 pub struct MyPluginInterface(ObjectInterface<imp::MyPluginInterface>) @requires MyStaticInterface;
568 }
569
570 unsafe impl<T: imp::MyPluginInterfaceImpl> IsImplementable<T> for MyPluginInterface {}
571
572 // an object subclass to register as a dynamic type and that extends `MyStaticType` and that implements `MyStaticInterface` and `MyPluginInterface`.
573 glib::wrapper! {
574 pub struct MyPluginType(ObjectSubclass<imp::MyPluginType>) @extends MyStaticType, @implements MyPluginInterface, MyStaticInterface;
575 }
576
577 // an object interface to lazy register as a dynamic type and that extends `MyStaticInterface`.
578 glib::wrapper! {
579 pub struct MyPluginInterfaceLazy(ObjectInterface<imp::MyPluginInterfaceLazy>) @requires MyStaticInterface;
580 }
581
582 unsafe impl<T: imp::MyPluginInterfaceLazyImpl> IsImplementable<T> for MyPluginInterfaceLazy {}
583
584 // an object subclass to lazy register as a dynamic type and that extends `MyStaticType` that implements `MyStaticInterface` and `MyPluginInterfaceLazy`.
585 glib::wrapper! {
586 pub struct MyPluginTypeLazy(ObjectSubclass<imp::MyPluginTypeLazy>) @extends MyStaticType, @implements MyPluginInterfaceLazy, MyStaticInterface;
587 }
588
589 // a plugin (must implement `glib::TypePlugin`).
590 glib::wrapper! {
591 pub struct MyPlugin(ObjectSubclass<imp::MyPlugin>) @implements glib::TypePlugin;
592 }
593
594 #[test]
595 fn dynamic_object_subclasses() {
596 use glib::prelude::TypePluginExt;
597
598 // checks types of object subclasses and of object interfaces to register as dynamic types are invalid (plugin is not used yet).
599 assert!(!imp::MyPluginInterface::type_().is_valid());
600 assert!(!imp::MyPluginType::type_().is_valid());
601 assert!(!imp::MyPluginInterfaceLazy::type_().is_valid());
602 assert!(!imp::MyPluginTypeLazy::type_().is_valid());
603
604 // simulates the GLib type system to use/unuse the plugin.
605 let plugin = glib::Object::new::<MyPlugin>();
606 TypePluginExt::use_(&plugin);
607 TypePluginExt::unuse(&plugin);
608
609 // checks types of object subclasses and of object interfaces registered as dynamic types are valid (plugin is unused).
610 assert!(imp::MyPluginInterface::type_().is_valid());
611 assert!(imp::MyPluginType::type_().is_valid());
612 // check types of object subclasses and of object interfaces that are lazy registered as dynamic types are still invalid (plugin is unused)
613 assert!(!imp::MyPluginInterfaceLazy::type_().is_valid());
614 assert!(!imp::MyPluginTypeLazy::type_().is_valid());
615
616 // simulates the GLib type system to use the plugin.
617 TypePluginExt::use_(&plugin);
618
619 // checks types of object subclasses and of object interfaces registered as dynamic types are valid (plugin is used).
620 let iface_type = imp::MyPluginInterface::type_();
621 assert!(iface_type.is_valid());
622 let obj_type = imp::MyPluginType::type_();
623 assert!(obj_type.is_valid());
624 let iface_lazy_type = imp::MyPluginInterfaceLazy::type_();
625 assert!(iface_lazy_type.is_valid());
626 let obj_lazy_type = imp::MyPluginTypeLazy::type_();
627 assert!(obj_lazy_type.is_valid());
628
629 // checks plugin of object subclasses and of object interfaces is `MyPlugin`.
630 assert_eq!(
631 iface_type.plugin().as_ref(),
632 Some(plugin.upcast_ref::<glib::TypePlugin>())
633 );
634 assert_eq!(
635 obj_type.plugin().as_ref(),
636 Some(plugin.upcast_ref::<glib::TypePlugin>())
637 );
638 assert_eq!(
639 iface_lazy_type.plugin().as_ref(),
640 Some(plugin.upcast_ref::<glib::TypePlugin>())
641 );
642 assert_eq!(
643 obj_lazy_type.plugin().as_ref(),
644 Some(plugin.upcast_ref::<glib::TypePlugin>())
645 );
646
647 // simulates the GLib type system to unuse the plugin.
648 TypePluginExt::unuse(&plugin);
649
650 // checks types of object subclasses and of object interfaces registered as dynamic types are still valid.
651 assert!(imp::MyPluginInterface::type_().is_valid());
652 assert!(imp::MyPluginType::type_().is_valid());
653 assert!(imp::MyPluginInterfaceLazy::type_().is_valid());
654 assert!(imp::MyPluginTypeLazy::type_().is_valid());
655
656 // simulates the GLib type system to reuse the plugin.
657 TypePluginExt::use_(&plugin);
658
659 // checks types of object subclasses and of object interfaces registered as dynamic types are still valid.
660 assert!(imp::MyPluginInterface::type_().is_valid());
661 assert!(imp::MyPluginType::type_().is_valid());
662 assert!(imp::MyPluginInterfaceLazy::type_().is_valid());
663 assert!(imp::MyPluginTypeLazy::type_().is_valid());
664
665 // simulates the GLib type system to unuse the plugin.
666 TypePluginExt::unuse(&plugin);
667 }
668 }