gtk-rs-core icon indicating copy to clipboard operation
gtk-rs-core copied to clipboard

Change *Impl trait methods to only take `&self` and not `Self::Type` in addition

Open sdroege opened this issue 1 year ago • 0 comments

It's redundant because you can always get Self::Type via self.instance(). It would reduce quite a bit of noise and also reduces some confusion, but OTOH makes it less visible how to call object methods.

For changing this we'd have to also change to fn instance(&self) -> &Self::Type as otherwise additional refcounting happens, which is unnecessary. This would need a helper type as follows

pub struct ObjectRef<'a, T> {
    ptr: *const gobject_sys::GObject,
    phantom: PhantomData<&'a T>,
};

impl Deref for ObjectRef<'a, T> {
    type Target = T;

    fn deref(&self) -> &T {
        unsafe {
            &*(&self.ptr as *const *const _ as *const T)
        }
    }
}

...
    fn instance(&self) -> ObjectRef<T::Type> {
        let ptr = ...; // as before
        ObjectRef { ptr, phantom: PhantomData }
    }

For the actual change, this would change ObjectImpl::constructed() as follows (and all the other Impl trait methods and the parent_XXX ones on ImplExt, etc)

- fn constructed(&self, obj: &Self::Type);
+ fn constructed(&self);

sdroege avatar Sep 28 '22 07:09 sdroege