pyfunction: use extract_argument with holder to avoid extractext #2503
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.
Motivated by #2487, this is hopefully a better long-term solution than patching to avoid upsetting clippy.
This is a reworking of the pyfunction macro implementations to have some smarter internal type inference for extracting reference values. At the moment there is an
ExtractExt
trait and a lot of generated macro code dancing with derefs which make clippy quite unhappy.This PR replaces
ExtractExt
with aPyFunctionArgument
trait which avoids the need for deref juggling by taking a&mut holder
argument used as temporary storage. ForT: FromPyObject
types the holder is just()
(and should hopefully be optimised out), and for&T
/&mut T
for#[pyclass]
types the holder isOption<PyRef<T>>
andOption<PyRefMut<T>>
(Option
because if the wrong argument is passed from Python then the holder never gets populated).I was careful to check both benchmarks and generated lines of code, and I got no noticeable difference to either. The net result is more friendly on type inference, leads to simpler macro code, and avoids clippy trouble.
To have a practical example of what this change results as - for a function which looks like below:
The relevant generated code has changed from:
to: