Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support generic types in glib::object_subclass #1286

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

andy128k
Copy link
Contributor

Support generic types in glib::object_subclass and glib::object_interface macros.

@andy128k andy128k marked this pull request as ready for review January 21, 2024 13:47
@bilelmoussaoui
Copy link
Member

What would be the use case here?

@andy128k
Copy link
Contributor Author

Well, code reuse. Let's say, I want to write a generic widget once and re-use it in a different contexts.

Copy link
Member

@sdroege sdroege left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some tests to actually make use of this at runtime and make sure the types work correctly?

@@ -22,16 +22,15 @@ pub fn impl_object_interface(input: &mut syn::ItemImpl) -> TokenStream {
..
} = input;

let self_ty_as_ident = match &**self_ty {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't look too close but I think this is currently unsound. There's only a single type registered for every T, so you can currently safely cast between different T. Not sure how that can be solved nicely.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, single type. Similar to generics in Java with type erasure.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlike in Java this is unsound here though :) In the simplest case, you could have a MyObj<T> that stores a T, and then simply casting that between T: String to T: u32 and the subclass couldn't know about that happening and accesses the value with the wrong type then.

@andy128k andy128k marked this pull request as draft January 22, 2024 20:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants