-
-
Notifications
You must be signed in to change notification settings - Fork 115
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
base: main
Are you sure you want to change the base?
Conversation
What would be the use case here? |
Well, code reuse. Let's say, I want to write a generic widget once and re-use it in a different contexts. |
There was a problem hiding this 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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Support generic types in
glib::object_subclass
andglib::object_interface
macros.