pyfunction: allow wrap_pyfunction to work on imports (even cross-crate) #2091
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I'm really pumped about this! I had an idea earlier today how to make
wrap_pyfunction!
work on a#[pyfunction]
imported from another module.How it works:
#[pyfunction] fn foo
now generates a hiddenmod foo
alongside the function. They're allowed to coexist, anduse foo
will bring both into the new scope. The hidden module contains just enough exports forwrap_pyfunction
to be able to get what it needs. Seepyo3-macros-backend/src/pyfunction.rs
for the details.This works really nicely - it even works across different crates! 🚀
(This trick could potentially also be used for
#[pymodule]
/wrap_pymodule!
, though I'll save that for another day, because things may also change when we implement#[pymodule]
onmod
as proposed in #694 (comment).)