diff --git a/src/subclass/object.rs b/src/subclass/object.rs index 34bb6ee8..e1e77605 100644 --- a/src/subclass/object.rs +++ b/src/subclass/object.rs @@ -377,6 +377,9 @@ mod test { fn constructed(&self, obj: &Object) { self.parent_constructed(obj); + + assert_eq!(obj, &self.get_instance()); + *self.constructed.borrow_mut() = true; } } diff --git a/src/subclass/types.rs b/src/subclass/types.rs index 8a87e534..8dcdaecc 100644 --- a/src/subclass/types.rs +++ b/src/subclass/types.rs @@ -198,7 +198,9 @@ pub trait ObjectSubclass: ObjectImpl + Sized + 'static { const NAME: &'static str; /// Parent Rust type to inherit from. - type ParentType: IsA + FromGlibPtrBorrow<*mut ::GlibType>; + type ParentType: IsA + + FromGlibPtrBorrow<*mut ::GlibType> + + FromGlibPtrNone<*mut ::GlibType>; /// The C instance struct. /// @@ -241,6 +243,24 @@ pub trait ObjectSubclass: ObjectImpl + Sized + 'static { } } + /// Returns the corresponding object instance. + fn get_instance(&self) -> Self::ParentType { + unsafe { + let data = Self::type_data(); + let type_ = data.as_ref().get_type(); + assert_ne!(type_, Type::Invalid); + + let offset = -data.as_ref().private_offset; + assert_ne!(offset, 0); + + let ptr = self as *const Self as *const u8; + let ptr = ptr.offset(offset); + let ptr = ptr as *mut u8 as *mut ::GlibType; + + from_glib_none(ptr) + } + } + /// Additional type initialization. /// /// This is called right after the type was registered and allows