use crate::err::PyResult;
use crate::ffi_ptr_ext::FfiPtrExt;
use crate::py_result_ext::PyResultExt;
use crate::{ffi, Bound, PyAny};
#[cfg(RustPython)]
use crate::{
sync::PyOnceLock,
types::{PyType, PyTypeMethods},
Py,
};
#[repr(transparent)]
pub struct PyMemoryView(PyAny);
#[cfg(not(RustPython))]
pyobject_native_type_core!(PyMemoryView, pyobject_native_static_type_object!(ffi::PyMemoryView_Type), "builtins", "memoryview", #checkfunction=ffi::PyMemoryView_Check);
#[cfg(RustPython)]
pyobject_native_type_core!(
PyMemoryView,
|py| {
static TYPE: PyOnceLock<Py<PyType>> = PyOnceLock::new();
TYPE.import(py, "builtins", "memoryview").unwrap().as_type_ptr()
},
"builtins",
"memoryview",
#checkfunction=ffi::PyMemoryView_Check
);
impl PyMemoryView {
pub fn from<'py>(src: &Bound<'py, PyAny>) -> PyResult<Bound<'py, Self>> {
unsafe {
ffi::PyMemoryView_FromObject(src.as_ptr())
.assume_owned_or_err(src.py())
.cast_into_unchecked()
}
}
}
impl<'py> TryFrom<&Bound<'py, PyAny>> for Bound<'py, PyMemoryView> {
type Error = crate::PyErr;
fn try_from(value: &Bound<'py, PyAny>) -> Result<Self, Self::Error> {
PyMemoryView::from(value)
}
}