gobject_subclass/
deref.rs

1#[macro_export]
2macro_rules! gobject_subclass_deref(
3    ($name:ident, $base:ident) => {
4        gobject_subclass_deref!($name, imp::$name, $base);
5    };
6
7    ($name:ident, $target:ty, $base:ident) => {
8        use std::ops::Deref;
9
10        impl Deref for $name {
11            type Target = $target;
12
13            fn deref(&self) -> &Self::Target {
14                unsafe {
15                    let base: $base = from_glib_borrow(self.to_glib_none().0);
16                    let imp = base.get_impl();
17                    let imp = imp.downcast_ref::<$target>().unwrap();
18                    // Cast to a raw pointer to get us an appropriate lifetime: the compiler
19                    // can't know that the lifetime of base is the same as the one of self
20                    &*(imp as *const $target)
21                }
22            }
23        }
24    }
25);