Merge pull request #3779 from davidhewitt/bound-from-ptr

expose `Bound::from_owned_ptr` etc
This commit is contained in:
David Hewitt 2024-02-05 19:26:57 +00:00 committed by GitHub
commit 020ed39327
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 13 additions and 12 deletions

View File

@ -105,33 +105,34 @@ where
}
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
///
/// `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 a valid pointer to a Python object
/// - `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)))
}
/// 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
///
/// `ptr` must be a valid pointer to a Python object, or NULL.
pub(crate) unsafe fn from_owned_ptr_or_opt(
py: Python<'py>,
ptr: *mut ffi::PyObject,
) -> Option<Self> {
/// - `ptr` must be a valid pointer to a Python object, or null
/// - `ptr` must be an owned Python reference, as the `Bound<'py, PyAny>` will assume ownership
pub unsafe fn from_owned_ptr_or_opt(py: Python<'py>, ptr: *mut ffi::PyObject) -> Option<Self> {
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
///
/// `ptr` must be a valid pointer to a Python object, or NULL.
pub(crate) unsafe fn from_owned_ptr_or_err(
/// - `ptr` must be a valid pointer to a Python object, or null
/// - `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>,
ptr: *mut ffi::PyObject,
) -> PyResult<Self> {