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.
Added some documentation for IntoPy, but not the redirect I was planning on, I'll do that after #1128 lands.
Rendered
Defines a conversion from a Rust type to a Python object.
It functions similarly to std's
Into
trait,but requires a GIL token as an argument.
Many functions and traits internal to PyO3 require this trait as a bound,
so a lack of this trait can manifest itself in different error messages.
Examples
With
#[pyclass]
The easiest way to implement
IntoPy
is by exposing a struct as a native Python objectby annotating it with
#[pyclass]
.Python code will see this as an instance of the
Number
class with avalue
attribute.Conversion to a Python object
However, it may not be desirable to expose the existence of
Number
to Python code.IntoPy
allows us to define a conversion to an appropriate Python object.Python code will see this as an
int
object.Dynamic conversion into Python objects.
It is also possible to return a different Python object depending on some condition.
This is useful for types like enums that can carry different types.
Python code will see this as any of the
int
,string
orNone
objects.