Skip to content

Commit

Permalink
implement IntoPy and ToPyObject for Borrowed<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Dec 31, 2023
1 parent fbd896b commit 99f3230
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,19 @@ impl<T> Clone for Borrowed<'_, '_, T> {

impl<T> Copy for Borrowed<'_, '_, T> {}

impl<T> IntoPy<Py<PyAny>> for Borrowed<'_, '_, T> {
fn into_py(self, py: Python<'_>) -> Py<PyAny> {
// Safety: Borrowed<'_, '_, T> is a pointer to a Python object
unsafe { Py::from_borrowed_ptr(py, self.as_ptr()) }
}
}

impl<T> ToPyObject for Borrowed<'_, '_, T> {
fn to_object(&self, py: Python<'_>) -> Py<PyAny> {
(*self).into_py(py)
}
}

/// A GIL-independent reference to an object allocated on the Python heap.
///
/// This type does not auto-dereference to the inner object because you must prove you hold the GIL to access it.
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/invalid_result_conversion.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ error[E0277]: the trait bound `PyErr: From<MyError>` is not satisfied
| ^^^^^^^^^^^^^ the trait `From<MyError>` is not implemented for `PyErr`
|
= help: the following other types implement trait `From<T>`:
<PyErr as From<PyBorrowError>>
<PyErr as From<std::io::Error>>
<PyErr as From<PyBorrowError>>
<PyErr as From<PyBorrowMutError>>
<PyErr as From<PyDowncastError<'a>>>
<PyErr as From<DowncastError<'_, '_>>>
Expand Down

0 comments on commit 99f3230

Please sign in to comment.