From 99f32306ef67e40018ef0b1c00915d0b85aa1fc5 Mon Sep 17 00:00:00 2001 From: David Hewitt Date: Sun, 31 Dec 2023 21:13:48 +0000 Subject: [PATCH] implement `IntoPy` and `ToPyObject` for `Borrowed` --- src/instance.rs | 13 +++++++++++++ tests/ui/invalid_result_conversion.stderr | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/instance.rs b/src/instance.rs index 4ca58c89626..8c3ac59ed3f 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -355,6 +355,19 @@ impl Clone for Borrowed<'_, '_, T> { impl Copy for Borrowed<'_, '_, T> {} +impl IntoPy> for Borrowed<'_, '_, T> { + fn into_py(self, py: Python<'_>) -> Py { + // Safety: Borrowed<'_, '_, T> is a pointer to a Python object + unsafe { Py::from_borrowed_ptr(py, self.as_ptr()) } + } +} + +impl ToPyObject for Borrowed<'_, '_, T> { + fn to_object(&self, py: Python<'_>) -> Py { + (*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. diff --git a/tests/ui/invalid_result_conversion.stderr b/tests/ui/invalid_result_conversion.stderr index 2720c71db63..b3e65517e36 100644 --- a/tests/ui/invalid_result_conversion.stderr +++ b/tests/ui/invalid_result_conversion.stderr @@ -5,8 +5,8 @@ error[E0277]: the trait bound `PyErr: From` is not satisfied | ^^^^^^^^^^^^^ the trait `From` is not implemented for `PyErr` | = help: the following other types implement trait `From`: - > > + > > >> >>