2024-02-28 19:36:55 +00:00
|
|
|
use crate::sealed::Sealed;
|
2023-12-14 19:07:47 +00:00
|
|
|
use crate::{
|
|
|
|
ffi,
|
2023-12-21 12:04:45 +00:00
|
|
|
instance::{Borrowed, Bound},
|
2023-12-14 19:07:47 +00:00
|
|
|
PyAny, PyResult, Python,
|
|
|
|
};
|
2023-12-14 10:51:18 +00:00
|
|
|
|
|
|
|
pub(crate) trait FfiPtrExt: Sealed {
|
2023-12-21 12:04:45 +00:00
|
|
|
unsafe fn assume_owned_or_err(self, py: Python<'_>) -> PyResult<Bound<'_, PyAny>>;
|
2023-12-23 14:26:02 +00:00
|
|
|
unsafe fn assume_owned_or_opt(self, py: Python<'_>) -> Option<Bound<'_, PyAny>>;
|
2023-12-21 12:04:45 +00:00
|
|
|
unsafe fn assume_owned(self, py: Python<'_>) -> Bound<'_, PyAny>;
|
2023-12-14 19:07:47 +00:00
|
|
|
|
|
|
|
/// Assumes this pointer is borrowed from a parent object.
|
|
|
|
///
|
|
|
|
/// Warning: the lifetime `'a` is not bounded by the function arguments; the caller is
|
|
|
|
/// responsible to ensure this is tied to some appropriate lifetime.
|
2023-12-21 12:02:56 +00:00
|
|
|
unsafe fn assume_borrowed_or_err<'a>(self, py: Python<'_>)
|
|
|
|
-> PyResult<Borrowed<'a, '_, PyAny>>;
|
2023-12-14 19:07:47 +00:00
|
|
|
|
|
|
|
/// Same as `assume_borrowed_or_err`, but doesn't fetch an error on NULL.
|
2023-12-21 12:02:56 +00:00
|
|
|
unsafe fn assume_borrowed_or_opt<'a>(self, py: Python<'_>) -> Option<Borrowed<'a, '_, PyAny>>;
|
2023-12-14 19:07:47 +00:00
|
|
|
|
|
|
|
/// Same as `assume_borrowed_or_err`, but panics on NULL.
|
2023-12-21 12:02:56 +00:00
|
|
|
unsafe fn assume_borrowed<'a>(self, py: Python<'_>) -> Borrowed<'a, '_, PyAny>;
|
2023-12-20 12:44:00 +00:00
|
|
|
|
|
|
|
/// Same as `assume_borrowed_or_err`, but does not check for NULL.
|
2023-12-21 12:02:56 +00:00
|
|
|
unsafe fn assume_borrowed_unchecked<'a>(self, py: Python<'_>) -> Borrowed<'a, '_, PyAny>;
|
2023-12-14 10:51:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FfiPtrExt for *mut ffi::PyObject {
|
|
|
|
#[inline]
|
2023-12-21 12:04:45 +00:00
|
|
|
unsafe fn assume_owned_or_err(self, py: Python<'_>) -> PyResult<Bound<'_, PyAny>> {
|
|
|
|
Bound::from_owned_ptr_or_err(py, self)
|
2023-12-14 10:51:18 +00:00
|
|
|
}
|
|
|
|
|
2023-12-23 14:26:02 +00:00
|
|
|
#[inline]
|
|
|
|
unsafe fn assume_owned_or_opt(self, py: Python<'_>) -> Option<Bound<'_, PyAny>> {
|
|
|
|
Bound::from_owned_ptr_or_opt(py, self)
|
|
|
|
}
|
|
|
|
|
2023-12-14 10:51:18 +00:00
|
|
|
#[inline]
|
2024-04-19 11:44:36 +00:00
|
|
|
#[track_caller]
|
2023-12-21 12:04:45 +00:00
|
|
|
unsafe fn assume_owned(self, py: Python<'_>) -> Bound<'_, PyAny> {
|
|
|
|
Bound::from_owned_ptr(py, self)
|
2023-12-14 10:51:18 +00:00
|
|
|
}
|
2023-12-14 19:07:47 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
unsafe fn assume_borrowed_or_err<'a>(
|
|
|
|
self,
|
|
|
|
py: Python<'_>,
|
2023-12-21 12:02:56 +00:00
|
|
|
) -> PyResult<Borrowed<'a, '_, PyAny>> {
|
|
|
|
Borrowed::from_ptr_or_err(py, self)
|
2023-12-14 19:07:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2023-12-21 12:02:56 +00:00
|
|
|
unsafe fn assume_borrowed_or_opt<'a>(self, py: Python<'_>) -> Option<Borrowed<'a, '_, PyAny>> {
|
|
|
|
Borrowed::from_ptr_or_opt(py, self)
|
2023-12-14 19:07:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2024-04-19 11:44:36 +00:00
|
|
|
#[track_caller]
|
2023-12-21 12:02:56 +00:00
|
|
|
unsafe fn assume_borrowed<'a>(self, py: Python<'_>) -> Borrowed<'a, '_, PyAny> {
|
|
|
|
Borrowed::from_ptr(py, self)
|
2023-12-14 19:07:47 +00:00
|
|
|
}
|
2023-12-20 12:44:00 +00:00
|
|
|
|
|
|
|
#[inline]
|
2023-12-21 12:02:56 +00:00
|
|
|
unsafe fn assume_borrowed_unchecked<'a>(self, py: Python<'_>) -> Borrowed<'a, '_, PyAny> {
|
|
|
|
Borrowed::from_ptr_unchecked(py, self)
|
2023-12-20 12:44:00 +00:00
|
|
|
}
|
2023-12-14 10:51:18 +00:00
|
|
|
}
|