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

Commit 08f04dc

Browse files
committed
Add accessor for getting the interface implementation from an instance
Mirrors the exactly same API from the ObjectSubclass trait.
1 parent bfa1c13 commit 08f04dc

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/subclass/interface.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::ptr;
1111

1212
use super::{InitializingType, Property};
1313
use translate::*;
14-
use {StaticType, Type, Value};
14+
use {IsA, Object, ObjectExt, StaticType, Type, Value};
1515

1616
impl<T: ObjectInterface> InitializingType<T> {
1717
/// Adds an interface prerequisite for `I` to the type.
@@ -101,6 +101,21 @@ pub trait ObjectInterface: Sized + 'static {
101101
}
102102

103103
pub trait ObjectInterfaceExt: ObjectInterface {
104+
/// Get interface from an instance.
105+
///
106+
/// This will panic if `obj` does not implement the interface.
107+
fn from_instance<T: IsA<Object>>(obj: &T) -> &Self {
108+
assert!(obj.as_ref().get_type().is_a(&Self::get_type()));
109+
110+
unsafe {
111+
let klass = (*(obj.as_ptr() as *const gobject_ffi::GTypeInstance)).g_class;
112+
let interface =
113+
gobject_ffi::g_type_interface_peek(klass as *mut _, Self::get_type().to_glib());
114+
assert!(!interface.is_null());
115+
&*(interface as *const Self)
116+
}
117+
}
118+
104119
/// Install properties on the interface.
105120
///
106121
/// All implementors of the interface must provide these properties.

0 commit comments

Comments
 (0)