expose `Bound::from_owned_ptr` etc

This commit is contained in:
David Hewitt 2024-01-16 21:34:13 +00:00
parent ecb4ecbe22
commit 86f294f6e6
1 changed files with 13 additions and 12 deletions

View File

@ -105,33 +105,34 @@ where
} }
impl<'py> Bound<'py, PyAny> { impl<'py> Bound<'py, PyAny> {
/// Constructs a new Bound from a pointer. Panics if ptr is null. /// Constructs a new `Bound<'py, PyAny>` from a pointer. Panics if `ptr` is null.
/// ///
/// # Safety /// # Safety
/// ///
/// `ptr` must be a valid pointer to a Python object. /// - `ptr` must be a valid pointer to a Python object
pub(crate) unsafe fn from_owned_ptr(py: Python<'py>, ptr: *mut ffi::PyObject) -> Self { /// - `ptr` must be an owned Python reference, as the `Bound<'py, PyAny>` will assume ownership
pub unsafe fn from_owned_ptr(py: Python<'py>, ptr: *mut ffi::PyObject) -> Self {
Self(py, ManuallyDrop::new(Py::from_owned_ptr(py, ptr))) Self(py, ManuallyDrop::new(Py::from_owned_ptr(py, ptr)))
} }
/// Constructs a new Bound from a pointer. Returns None if ptr is null. /// Constructs a new `Bound<'py, PyAny>` from a pointer. Returns `None`` if `ptr` is null.
/// ///
/// # Safety /// # Safety
/// ///
/// `ptr` must be a valid pointer to a Python object, or NULL. /// - `ptr` must be a valid pointer to a Python object, or null
pub(crate) unsafe fn from_owned_ptr_or_opt( /// - `ptr` must be an owned Python reference, as the `Bound<'py, PyAny>` will assume ownership
py: Python<'py>, pub unsafe fn from_owned_ptr_or_opt(py: Python<'py>, ptr: *mut ffi::PyObject) -> Option<Self> {
ptr: *mut ffi::PyObject,
) -> Option<Self> {
Py::from_owned_ptr_or_opt(py, ptr).map(|obj| Self(py, ManuallyDrop::new(obj))) Py::from_owned_ptr_or_opt(py, ptr).map(|obj| Self(py, ManuallyDrop::new(obj)))
} }
/// Constructs a new Bound from a pointer. Returns error if ptr is null. /// Constructs a new `Bound<'py, PyAny>` from a pointer. Returns an `Err` by calling `PyErr::fetch`
/// if `ptr` is null.
/// ///
/// # Safety /// # Safety
/// ///
/// `ptr` must be a valid pointer to a Python object, or NULL. /// - `ptr` must be a valid pointer to a Python object, or null
pub(crate) unsafe fn from_owned_ptr_or_err( /// - `ptr` must be an owned Python reference, as the `Bound<'py, PyAny>` will assume ownership
pub unsafe fn from_owned_ptr_or_err(
py: Python<'py>, py: Python<'py>,
ptr: *mut ffi::PyObject, ptr: *mut ffi::PyObject,
) -> PyResult<Self> { ) -> PyResult<Self> {