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

Figure out how to handle custom "subclassing" traits

Open bilelmoussaoui opened this issue 4 years ago • 1 comments

See https://github.com/gtk-rs/gtk4-rs/pull/500 for context

Currently for cairo:

You have a parent type cairo::Surface and other sub-types like cairo::ImageSurface. If a function takes a cairo::Surface and you have a cairo::ImageSurface, you pass it as such some_func(&*image_surface) that's because there's a impl Deref for cairo::WhateverSurface with a target = cairo::Surface

for gtk4-rs, we use AsRef in those functions, we can then just call some_func(&some_render_node) , we should probably decide on something and use it across the crates.

I would vote for AsRef as it's kind of similar to IsA in terms of "syntax" and it's less magical than a Deref implementation :)

bilelmoussaoui avatar Jul 05 '21 16:07 bilelmoussaoui

For single inheritance without interfaces it makes sense to use Deref IMHO, but if you feel strong about it you can also do AsRef<Parent> and impl<T: AsRef<Parent>> ExtTrait for T { ... } instead. It's just more boilerplate.

sdroege avatar Sep 16 '22 09:09 sdroege

@bilelmoussaoui What should we do about this?

sdroege avatar Oct 08 '22 15:10 sdroege

I guess similar to have we did for fundamental types, going with impl AsRef T.

bilelmoussaoui avatar Oct 08 '22 15:10 bilelmoussaoui

You do that?

sdroege avatar Oct 08 '22 15:10 sdroege

Sure, I will update gtk4-rs per the changes in #783 first

bilelmoussaoui avatar Oct 08 '22 15:10 bilelmoussaoui