From a3eb328378e17d7fa79f0876393269dd4e511ab8 Mon Sep 17 00:00:00 2001 From: David Hewitt Date: Mon, 29 Jan 2024 13:31:04 +0000 Subject: [PATCH] migrate `FromPyObject` for `Bound` and `Py` to `extract_bound` --- newsfragments/3776.changed.md | 1 + src/instance.rs | 19 +++++++------------ 2 files changed, 8 insertions(+), 12 deletions(-) create mode 100644 newsfragments/3776.changed.md diff --git a/newsfragments/3776.changed.md b/newsfragments/3776.changed.md new file mode 100644 index 00000000..71ffd893 --- /dev/null +++ b/newsfragments/3776.changed.md @@ -0,0 +1 @@ +Relax bound of `FromPyObject` for `Py` to just `T: PyTypeCheck`. diff --git a/src/instance.rs b/src/instance.rs index d7edfe06..e29f4635 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -1405,27 +1405,22 @@ impl Drop for Py { } } -impl<'a, T> FromPyObject<'a> for Py +impl FromPyObject<'_> for Py where - T: PyTypeInfo, - &'a T::AsRefTarget: FromPyObject<'a>, - T::AsRefTarget: 'a + AsPyPointer, + T: PyTypeCheck, { /// Extracts `Self` from the source `PyObject`. - fn extract(ob: &'a PyAny) -> PyResult { - unsafe { - ob.extract::<&T::AsRefTarget>() - .map(|val| Py::from_borrowed_ptr(ob.py(), val.as_ptr())) - } + fn extract_bound(ob: &Bound<'_, PyAny>) -> PyResult { + ob.extract::>().map(Bound::unbind) } } -impl<'a, T> FromPyObject<'a> for Bound<'a, T> +impl<'py, T> FromPyObject<'py> for Bound<'py, T> where - T: PyTypeInfo, + T: PyTypeCheck, { /// Extracts `Self` from the source `PyObject`. - fn extract_bound(ob: &Bound<'a, PyAny>) -> PyResult { + fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult { ob.downcast().map(Clone::clone).map_err(Into::into) } }