-
Notifications
You must be signed in to change notification settings - Fork 783
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 default method implementation #2014
Conversation
bc12174
to
77726a5
Compare
pyo3-macros-backend/src/pyclass.rs
Outdated
// It's collected last so Python ignore them if it found methods with same names. | ||
visitor(collector.py_class_default_impls()); |
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.
It seems a bit more thorny than that:
Looks like if the method definition has METH_COEXIST
then it overwrites any previous method. So we just need to be careful to not use METH_COEXIST
with these default methods. Which I guess is fine.
I need to touch a lot of implementation details in order to rename the generated method, and there's a lot of code duplication between |
3cb56bb
to
943cc3d
Compare
943cc3d
to
44d05dd
Compare
So I forgot there is |
94c23e4
to
58f1c94
Compare
I was very sick from my vaccine shot yesterday, so couldn't do anything. Sorry. You mentioned there would be trait conflicts from default |
No worries, I have also been very busy. I'll do my best to review this over the weekend, or end of Monday at the latest. |
9a1c40c
to
517a6e4
Compare
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.
Thanks, this looks fine to me! Sorry for the delay.
Just needs the CHANGELOG entry removed please (and maybe squash this branch at the same time) and then I think it's good to merge.
EDIT: oh and also the #[doc(hidden)]
addition to the generated methods.
pub fn gen_default_slot_impls(cls: &syn::Ident, method_defs: Vec<TokenStream>) -> TokenStream { | ||
// This function uses a lot of `unwrap()`; since method_defs are provided by us, they should | ||
// all succeed. |
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'm not a huge fan of taking Vec<TokenStream>
as an argument and all the corresponding unwrap
it subsequently leads to.
I think an alternative might be to take Vec<(syn::ItemFn, PyMethod, &SlotDef)>
and have the machinery to just produce the slots in here. But TBH that would probably lead to quite a large refactoring so I think it's better here to just focus on getting the end-user output as desired and then we can refactor internals of PyO3 for compile speed to our hearts' content later! 😂
517a6e4
to
b53c551
Compare
b53c551
to
3c18aae
Compare
3c18aae
to
aac0e56
Compare
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.
Perfect, thanks again!
Precursor to #2013. This allows us to inject default implementation of slot methods.
Add a trait (
PyClassDefaultSlots
) to support default methods.Add
pyimpl::gen_default_slot_impls
to help generate definition of default methods in the macro backend.Add default implementation of
__repr__
for#[pyclass]
enums.