You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* docs: add narrative docs for BoundObject
* grammar fixes
* use where to break up signature
* fix incorrect last sentence
* Update guide/src/types.md
Co-authored-by: Icxolu <[email protected]>
* move example to traits.md and rework with a stronger motivation
* fix cross-reference target
* fix type
* build result vec without copying
* simplify example a little
* update explanation sentence
* fix API docs link
---------
Co-authored-by: Icxolu <[email protected]>
Copy file name to clipboardExpand all lines: guide/src/conversions/traits.md
+68Lines changed: 68 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -626,6 +626,73 @@ impl IntoPy<PyObject> for MyPyObjectWrapper {
626
626
}
627
627
```
628
628
629
+
#### `BoundObject` for conversions that may be `Bound` or `Borrowed`
630
+
631
+
`IntoPyObject::into_py_object` returns either `Bound` or `Borrowed` depending on the implementation for a concrete type. For example, the `IntoPyObject` implementation for `u32` produces a `Bound<'py, PyInt>` and the `bool` implementation produces a `Borrowed<'py, 'py, PyBool>`:
In this example if we wanted to combine `ints_as_pyints` and `bools_as_pybool` into a single `Vec<Py<PyAny>>` to return from the `with_gil` closure, we would have to manually convert the concrete types for the smart pointers and the python types.
657
+
658
+
Instead, we can write a function that generically converts vectors of either integers or bools into a vector of `Py<PyAny>` using the [`BoundObject`] trait:
In the example above we used `BoundObject::into_any` and `BoundObject::unbind` to manipulate the python types and smart pointers into the result type we wanted to produce from the function.
695
+
629
696
### The `ToPyObject` trait
630
697
631
698
<divclass="warning">
@@ -647,3 +714,4 @@ same purpose, except that it consumes `self`.
Since `IntoPyObject::into_pyobject` may return either a `Bound` or `Borrowed`, you may find the [`BoundObject`](conversions/traits.md#boundobject-for-conversions-that-may-be-bound-or-borrowed) trait to be useful to write code that generically handles either type of smart pointer.
161
+
160
162
Together with the introduction of `IntoPyObject` the old conversion traits `ToPyObject` and `IntoPy`
161
163
are deprecated and will be removed in a future PyO3 version.
0 commit comments