From a1e77c5a66d2ce471b63e6ae16582185c6f04e66 Mon Sep 17 00:00:00 2001 From: Lily Foote Date: Tue, 13 Feb 2024 23:30:16 +0000 Subject: [PATCH] Document using as_borrowed in the Bound migration (#3833) --- guide/src/migration.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/guide/src/migration.md b/guide/src/migration.md index 1c8655be..93678489 100644 --- a/guide/src/migration.md +++ b/guide/src/migration.md @@ -217,6 +217,12 @@ Because the new `Bound` API brings ownership out of the PyO3 framework and in - `Bound` and `Bound` cannot support indexing with `list[0]`, you should use `list.get_item(0)` instead. - `Bound::iter_borrowed` is slightly more efficient than `Bound::iter`. The default iteration of `Bound` cannot return borrowed references because Rust does not (yet) have "lending iterators". Similarly `Bound::get_borrowed_item` is more efficient than `Bound::get_item` for the same reason. - `&Bound` does not implement `FromPyObject` (although it might be possible to do this in the future once the GIL Refs API is completely removed). Use `bound_any.downcast::()` instead of `bound_any.extract::<&Bound>()`. +- To convert between `&PyAny` and `&Bound` you can use the `as_borrowed()` method: + +```rust,ignore +let gil_ref: &PyAny = ...; +let bound: &Bound = &gil_ref.as_borrowed(); +``` #### Migrating `FromPyObject` implementations