Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

Add API for getting the instance from the object implementation #396

Merged
merged 1 commit into from Nov 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/subclass/object.rs
Expand Up @@ -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;
}
}
Expand Down
22 changes: 21 additions & 1 deletion src/subclass/types.rs
Expand Up @@ -198,7 +198,9 @@ pub trait ObjectSubclass: ObjectImpl + Sized + 'static {
const NAME: &'static str;

/// Parent Rust type to inherit from.
type ParentType: IsA<Object> + FromGlibPtrBorrow<*mut <Self::ParentType as Wrapper>::GlibType>;
type ParentType: IsA<Object>
+ FromGlibPtrBorrow<*mut <Self::ParentType as Wrapper>::GlibType>
+ FromGlibPtrNone<*mut <Self::ParentType as Wrapper>::GlibType>;

/// The C instance struct.
///
Expand Down Expand Up @@ -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);
sdroege marked this conversation as resolved.
Show resolved Hide resolved
let ptr = ptr as *mut u8 as *mut <Self::ParentType as Wrapper>::GlibType;

from_glib_none(ptr)
}
sdroege marked this conversation as resolved.
Show resolved Hide resolved
}

/// Additional type initialization.
///
/// This is called right after the type was registered and allows
Expand Down