Skip to content

Commit 99f3230

Browse files
committed
implement IntoPy and ToPyObject for Borrowed<T>
1 parent fbd896b commit 99f3230

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/instance.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,19 @@ impl<T> Clone for Borrowed<'_, '_, T> {
355355

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

358+
impl<T> IntoPy<Py<PyAny>> for Borrowed<'_, '_, T> {
359+
fn into_py(self, py: Python<'_>) -> Py<PyAny> {
360+
// Safety: Borrowed<'_, '_, T> is a pointer to a Python object
361+
unsafe { Py::from_borrowed_ptr(py, self.as_ptr()) }
362+
}
363+
}
364+
365+
impl<T> ToPyObject for Borrowed<'_, '_, T> {
366+
fn to_object(&self, py: Python<'_>) -> Py<PyAny> {
367+
(*self).into_py(py)
368+
}
369+
}
370+
358371
/// A GIL-independent reference to an object allocated on the Python heap.
359372
///
360373
/// This type does not auto-dereference to the inner object because you must prove you hold the GIL to access it.

tests/ui/invalid_result_conversion.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ error[E0277]: the trait bound `PyErr: From<MyError>` is not satisfied
55
| ^^^^^^^^^^^^^ the trait `From<MyError>` is not implemented for `PyErr`
66
|
77
= help: the following other types implement trait `From<T>`:
8-
<PyErr as From<PyBorrowError>>
98
<PyErr as From<std::io::Error>>
9+
<PyErr as From<PyBorrowError>>
1010
<PyErr as From<PyBorrowMutError>>
1111
<PyErr as From<PyDowncastError<'a>>>
1212
<PyErr as From<DowncastError<'_, '_>>>

0 commit comments

Comments
 (0)