migrate `FromPyObject` for `Bound` and `Py` to `extract_bound`

This commit is contained in:
David Hewitt 2024-01-29 13:31:04 +00:00
parent 7549a21154
commit a3eb328378
2 changed files with 8 additions and 12 deletions

View File

@ -0,0 +1 @@
Relax bound of `FromPyObject` for `Py<T>` to just `T: PyTypeCheck`.

View File

@ -1405,27 +1405,22 @@ impl<T> Drop for Py<T> {
}
}
impl<'a, T> FromPyObject<'a> for Py<T>
impl<T> FromPyObject<'_> for Py<T>
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<Self> {
unsafe {
ob.extract::<&T::AsRefTarget>()
.map(|val| Py::from_borrowed_ptr(ob.py(), val.as_ptr()))
}
fn extract_bound(ob: &Bound<'_, PyAny>) -> PyResult<Self> {
ob.extract::<Bound<'_, T>>().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<Self> {
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
ob.downcast().map(Clone::clone).map_err(Into::into)
}
}