1use crate::err::PyResult;
2use crate::ffi_ptr_ext::FfiPtrExt;
3use crate::py_result_ext::PyResultExt;
4use crate::{ffi, Bound, PyAny};
5#[cfg(feature = "gil-refs")]
6use crate::{AsPyPointer, PyNativeType};
7
8#[repr(transparent)]
13pub struct PyMemoryView(PyAny);
14
15pyobject_native_type_core!(PyMemoryView, pyobject_native_static_type_object!(ffi::PyMemoryView_Type), #checkfunction=ffi::PyMemoryView_Check);
16
17impl PyMemoryView {
18 #[cfg(feature = "gil-refs")]
20 #[deprecated(
21 since = "0.21.0",
22 note = "`PyMemoryView::from` will be replaced by `PyMemoryView::from_bound` in a future PyO3 version"
23 )]
24 pub fn from(src: &PyAny) -> PyResult<&PyMemoryView> {
25 PyMemoryView::from_bound(&src.as_borrowed()).map(Bound::into_gil_ref)
26 }
27
28 pub fn from_bound<'py>(src: &Bound<'py, PyAny>) -> PyResult<Bound<'py, Self>> {
31 unsafe {
32 ffi::PyMemoryView_FromObject(src.as_ptr())
33 .assume_owned_or_err(src.py())
34 .downcast_into_unchecked()
35 }
36 }
37}
38
39#[cfg(feature = "gil-refs")]
40impl<'py> TryFrom<&'py PyAny> for &'py PyMemoryView {
41 type Error = crate::PyErr;
42
43 fn try_from(value: &'py PyAny) -> Result<Self, Self::Error> {
46 PyMemoryView::from_bound(&value.as_borrowed()).map(Bound::into_gil_ref)
47 }
48}
49
50impl<'py> TryFrom<&Bound<'py, PyAny>> for Bound<'py, PyMemoryView> {
51 type Error = crate::PyErr;
52
53 fn try_from(value: &Bound<'py, PyAny>) -> Result<Self, Self::Error> {
56 PyMemoryView::from_bound(value)
57 }
58}