From 4d6f94e8648f3493b43dab525e9ebade0ab8686b Mon Sep 17 00:00:00 2001 From: Icxolu <10486322+Icxolu@users.noreply.github.com> Date: Tue, 30 Jan 2024 20:28:04 +0100 Subject: [PATCH] add `Bound` constructors for `PyMemoryView` --- src/types/memoryview.rs | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/src/types/memoryview.rs b/src/types/memoryview.rs index 0d115540689..414bfc69cfa 100644 --- a/src/types/memoryview.rs +++ b/src/types/memoryview.rs @@ -1,5 +1,7 @@ use crate::err::PyResult; -use crate::{ffi, AsPyPointer, PyAny}; +use crate::ffi_ptr_ext::FfiPtrExt; +use crate::py_result_ext::PyResultExt; +use crate::{ffi, AsPyPointer, Bound, PyAny, PyNativeType}; /// Represents a Python `memoryview`. #[repr(transparent)] @@ -8,14 +10,30 @@ pub struct PyMemoryView(PyAny); pyobject_native_type_core!(PyMemoryView, pyobject_native_static_type_object!(ffi::PyMemoryView_Type), #checkfunction=ffi::PyMemoryView_Check); impl PyMemoryView { - /// Creates a new Python `memoryview` object from another Python object that - /// implements the buffer protocol. + /// Deprecated form of [`PyMemoryView::from_bound`] + #[cfg_attr( + not(feature = "gil-refs"), + deprecated( + since = "0.21.0", + note = "`PyMemoryView::from` will be replaced by `PyMemoryView::from_bound` in a future PyO3 version" + ) + )] pub fn from(src: &PyAny) -> PyResult<&PyMemoryView> { unsafe { src.py() .from_owned_ptr_or_err(ffi::PyMemoryView_FromObject(src.as_ptr())) } } + + /// Creates a new Python `memoryview` object from another Python object that + /// implements the buffer protocol. + pub fn from_bound<'py>(src: &Bound<'py, PyAny>) -> PyResult> { + unsafe { + ffi::PyMemoryView_FromObject(src.as_ptr()) + .assume_owned_or_err(src.py()) + .downcast_into_unchecked() + } + } } impl<'py> TryFrom<&'py PyAny> for &'py PyMemoryView { @@ -24,6 +42,16 @@ impl<'py> TryFrom<&'py PyAny> for &'py PyMemoryView { /// Creates a new Python `memoryview` object from another Python object that /// implements the buffer protocol. fn try_from(value: &'py PyAny) -> Result { - PyMemoryView::from(value) + PyMemoryView::from_bound(&value.as_borrowed()).map(Bound::into_gil_ref) + } +} + +impl<'py> TryFrom<&Bound<'py, PyAny>> for Bound<'py, PyMemoryView> { + type Error = crate::PyErr; + + /// Creates a new Python `memoryview` object from another Python object that + /// implements the buffer protocol. + fn try_from(value: &Bound<'py, PyAny>) -> Result { + PyMemoryView::from_bound(value) } }