Remove usage of `AsPyPointer` in `IntoPy<PyObject>` trait implementation

This commit is contained in:
Alex Gaynor 2023-08-16 07:33:40 -04:00
parent 9363491d54
commit 9f1b56b659
Failed to extract signature
4 changed files with 31 additions and 4 deletions

View File

@ -0,0 +1 @@
Replace blanket `impl IntoPy<PyObject> for &T where T: AsPyPointer` with implementations of `impl IntoPy<PyObject>` for `&PyAny`, `&T where T: AsRef<PyAny>`, and `&Py<T>`.

View File

@ -276,16 +276,23 @@ impl IntoPy<PyObject> for () {
}
}
impl<T> IntoPy<PyObject> for &'_ T
where
T: AsPyPointer,
{
impl IntoPy<PyObject> for &'_ PyAny {
#[inline]
fn into_py(self, py: Python<'_>) -> PyObject {
unsafe { PyObject::from_borrowed_ptr(py, self.as_ptr()) }
}
}
impl<T> IntoPy<PyObject> for &'_ T
where
T: AsRef<PyAny>,
{
#[inline]
fn into_py(self, py: Python<'_>) -> PyObject {
unsafe { PyObject::from_borrowed_ptr(py, self.as_ref().as_ptr()) }
}
}
impl<T: Copy + ToPyObject> ToPyObject for Cell<T> {
fn to_object(&self, py: Python<'_>) -> PyObject {
self.get().to_object(py)

View File

@ -925,6 +925,13 @@ impl<T> IntoPy<PyObject> for Py<T> {
}
}
impl<T> IntoPy<PyObject> for &'_ Py<T> {
#[inline]
fn into_py(self, py: Python<'_>) -> PyObject {
self.to_object(py)
}
}
impl<T> crate::AsPyPointer for Py<T> {
/// Gets the underlying FFI pointer, returns a borrowed pointer.
#[inline]

View File

@ -743,6 +743,12 @@ impl<T: PyClass> IntoPy<PyObject> for PyRef<'_, T> {
}
}
impl<T: PyClass> IntoPy<PyObject> for &'_ PyRef<'_, T> {
fn into_py(self, py: Python<'_>) -> PyObject {
self.inner.into_py(py)
}
}
impl<'a, T: PyClass> std::convert::TryFrom<&'a PyCell<T>> for crate::PyRef<'a, T> {
type Error = PyBorrowError;
fn try_from(cell: &'a crate::PyCell<T>) -> Result<Self, Self::Error> {
@ -867,6 +873,12 @@ impl<T: PyClass<Frozen = False>> IntoPy<PyObject> for PyRefMut<'_, T> {
}
}
impl<T: PyClass<Frozen = False>> IntoPy<PyObject> for &'_ PyRefMut<'_, T> {
fn into_py(self, py: Python<'_>) -> PyObject {
self.inner.into_py(py)
}
}
impl<'a, T: PyClass<Frozen = False>> AsPyPointer for PyRefMut<'a, T> {
fn as_ptr(&self) -> *mut ffi::PyObject {
self.inner.as_ptr()