| 1 | use crate::err::PyResult; |
| 2 | use crate::ffi_ptr_ext::FfiPtrExt; |
| 3 | use crate::py_result_ext::PyResultExt; |
| 4 | use crate::{ffi, Bound, PyAny}; |
| 5 | |
| 6 | /// Represents a Python `memoryview`. |
| 7 | /// |
| 8 | /// Values of this type are accessed via PyO3's smart pointers, e.g. as |
| 9 | /// [`Py<PyMemoryView>`][crate::Py] or [`Bound<'py, PyMemoryView>`][Bound]. |
| 10 | #[repr (transparent)] |
| 11 | pub struct PyMemoryView(PyAny); |
| 12 | |
| 13 | pyobject_native_type_core!(PyMemoryView, pyobject_native_static_type_object!(ffi::PyMemoryView_Type), #checkfunction=ffi::PyMemoryView_Check); |
| 14 | |
| 15 | impl PyMemoryView { |
| 16 | /// Creates a new Python `memoryview` object from another Python object that |
| 17 | /// implements the buffer protocol. |
| 18 | pub fn from<'py>(src: &Bound<'py, PyAny>) -> PyResult<Bound<'py, Self>> { |
| 19 | unsafe { |
| 20 | ffiResult, …>::PyMemoryView_FromObject(base:src.as_ptr()) |
| 21 | .assume_owned_or_err(src.py()) |
| 22 | .downcast_into_unchecked() |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | /// Deprecated name for [`PyMemoryView::from`]. |
| 27 | #[deprecated (since = "0.23.0" , note = "renamed to `PyMemoryView::from`" )] |
| 28 | #[inline ] |
| 29 | pub fn from_bound<'py>(src: &Bound<'py, PyAny>) -> PyResult<Bound<'py, Self>> { |
| 30 | Self::from(src) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | impl<'py> TryFrom<&Bound<'py, PyAny>> for Bound<'py, PyMemoryView> { |
| 35 | type Error = crate::PyErr; |
| 36 | |
| 37 | /// Creates a new Python `memoryview` object from another Python object that |
| 38 | /// implements the buffer protocol. |
| 39 | fn try_from(value: &Bound<'py, PyAny>) -> Result<Self, Self::Error> { |
| 40 | PyMemoryView::from(src:value) |
| 41 | } |
| 42 | } |
| 43 | |