Skip to content

Commit

Permalink
review how to handle TypeModule lifecycle
Browse files Browse the repository at this point in the history
Signed-off-by: fbrouille <[email protected]>
  • Loading branch information
fbrouille committed Aug 30, 2023
1 parent d631c13 commit 05c4b24
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 44 deletions.
14 changes: 7 additions & 7 deletions glib-macros/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,9 +492,9 @@ fn module_subclassable() {
assert!(!module::imp::MyModuleTypeLazy::type_().is_valid());

// simulate the glib type system to load/unload the module
let module = module::MyModule::new();
TypeModuleExt::use_(module.as_ref());
TypeModuleExt::unuse(module.as_ref());
let module = glib::Object::new::<module::MyModule>();
TypeModuleExt::use_(&module);
TypeModuleExt::unuse(&module);

// check types of module types and of module interfaces that are immediately registered are valid (module was loaded)
assert!(module::imp::MyModuleInterface::type_().is_valid());
Expand All @@ -504,7 +504,7 @@ fn module_subclassable() {
assert!(!module::imp::MyModuleTypeLazy::type_().is_valid());

// simulate the glib type system to load the module
TypeModuleExt::use_(module.as_ref());
TypeModuleExt::use_(&module);

// check types of module types and of module interfaces are valid (module is loaded)
let iface_type = module::imp::MyModuleInterface::type_();
Expand Down Expand Up @@ -535,7 +535,7 @@ fn module_subclassable() {
);

// simulate the glib type system to unload the module
TypeModuleExt::unuse(module.as_ref());
TypeModuleExt::unuse(&module);

// check types of module types and of module interfaces are still valid (should have been marked as unloaded by the glib type system but this cannot be checked)
assert!(module::imp::MyModuleInterface::type_().is_valid());
Expand All @@ -544,7 +544,7 @@ fn module_subclassable() {
assert!(module::imp::MyModuleTypeLazy::type_().is_valid());

// simulate the glib type system to reload the module
TypeModuleExt::use_(module.as_ref());
TypeModuleExt::use_(&module);

// check types of module types and of module interfaces are still valid (should have been marked as loaded by the glib type system but this cannot be checked)
assert!(module::imp::MyModuleInterface::type_().is_valid());
Expand All @@ -553,7 +553,7 @@ fn module_subclassable() {
assert!(module::imp::MyModuleTypeLazy::type_().is_valid());

// simulate the glib type system to unload the module
TypeModuleExt::unuse(module.as_ref());
TypeModuleExt::unuse(&module);
}

#[test]
Expand Down
1 change: 0 additions & 1 deletion glib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ mod date_time;
mod time_span;
mod time_zone;
pub use self::time_span::*;
mod type_module;
pub mod value;
pub mod variant;
mod variant_dict;
Expand Down
1 change: 0 additions & 1 deletion glib/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
pub use crate::{
gobject::traits::{TypeModuleExt, TypePluginExt},
param_spec::ParamSpecBuilderExt,
type_module::TypeModuleExtManual,
Cast, CastNone, IsA, ObjectExt, ObjectType, ParamSpecType, StaticType, StaticTypeExt,
StaticVariantType, ToSendValue, ToValue, ToVariant,
};
13 changes: 12 additions & 1 deletion glib/src/subclass/type_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ unsafe extern "C" fn load<T: TypeModuleImpl>(
let imp = instance.imp();

let res = imp.load();
// GLib type system expects a module to never be disposed if types has been
// successfully loaded.
// The following code prevents the Rust wrapper (`glib::TypeModule` subclass)
// to dispose the module when dropped by ensuring the reference count is > 1.
// Nothing is done if loading types has failed, allowing application to drop
// and dispose the invalid module.
if res && (*(type_module as *const gobject_ffi::GObject)).ref_count == 1 {
unsafe {
gobject_ffi::g_object_ref(type_module as _);
}
}

res.into_glib()
}
Expand Down Expand Up @@ -127,7 +138,7 @@ mod tests {

#[test]
fn test_simple_type_module() {
let simple_type_module = SimpleTypeModule::new();
let simple_type_module = crate::Object::new::<SimpleTypeModule>();
// simulate the glib type system to load the module
assert!(simple_type_module.use_());
simple_type_module.unuse();
Expand Down
34 changes: 0 additions & 34 deletions glib/src/type_module.rs

This file was deleted.

0 comments on commit 05c4b24

Please sign in to comment.