| 1 | use crate::sealed::Sealed; |
| 2 | use crate::{ |
| 3 | ffi, |
| 4 | instance::{Borrowed, Bound}, |
| 5 | PyAny, PyResult, Python, |
| 6 | }; |
| 7 | |
| 8 | pub(crate) trait FfiPtrExt: Sealed { |
| 9 | /// Assumes this pointer carries a Python reference which needs to be decref'd. |
| 10 | /// |
| 11 | /// If the pointer is NULL, this function will fetch an error. |
| 12 | unsafe fn assume_owned_or_err(self, py: Python<'_>) -> PyResult<Bound<'_, PyAny>>; |
| 13 | |
| 14 | /// Same as `assume_owned_or_err`, but doesn't fetch an error on NULL. |
| 15 | unsafe fn assume_owned_or_opt(self, py: Python<'_>) -> Option<Bound<'_, PyAny>>; |
| 16 | |
| 17 | /// Same as `assume_owned_or_err`, but panics on NULL. |
| 18 | unsafe fn assume_owned(self, py: Python<'_>) -> Bound<'_, PyAny>; |
| 19 | |
| 20 | /// Same as `assume_owned_or_err`, but does not check for NULL. |
| 21 | unsafe fn assume_owned_unchecked(self, py: Python<'_>) -> Bound<'_, PyAny>; |
| 22 | |
| 23 | /// Assumes this pointer is borrowed from a parent object. |
| 24 | /// |
| 25 | /// Warning: the lifetime `'a` is not bounded by the function arguments; the caller is |
| 26 | /// responsible to ensure this is tied to some appropriate lifetime. |
| 27 | unsafe fn assume_borrowed_or_err<'a>(self, py: Python<'_>) |
| 28 | -> PyResult<Borrowed<'a, '_, PyAny>>; |
| 29 | |
| 30 | /// Same as `assume_borrowed_or_err`, but doesn't fetch an error on NULL. |
| 31 | unsafe fn assume_borrowed_or_opt<'a>(self, py: Python<'_>) -> Option<Borrowed<'a, '_, PyAny>>; |
| 32 | |
| 33 | /// Same as `assume_borrowed_or_err`, but panics on NULL. |
| 34 | unsafe fn assume_borrowed<'a>(self, py: Python<'_>) -> Borrowed<'a, '_, PyAny>; |
| 35 | |
| 36 | /// Same as `assume_borrowed_or_err`, but does not check for NULL. |
| 37 | unsafe fn assume_borrowed_unchecked<'a>(self, py: Python<'_>) -> Borrowed<'a, '_, PyAny>; |
| 38 | } |
| 39 | |
| 40 | impl FfiPtrExt for *mut ffi::PyObject { |
| 41 | #[inline ] |
| 42 | unsafe fn assume_owned_or_err(self, py: Python<'_>) -> PyResult<Bound<'_, PyAny>> { |
| 43 | unsafe { Bound::from_owned_ptr_or_err(py, self) } |
| 44 | } |
| 45 | |
| 46 | #[inline ] |
| 47 | unsafe fn assume_owned_or_opt(self, py: Python<'_>) -> Option<Bound<'_, PyAny>> { |
| 48 | unsafe { Bound::from_owned_ptr_or_opt(py, self) } |
| 49 | } |
| 50 | |
| 51 | #[inline ] |
| 52 | #[track_caller ] |
| 53 | unsafe fn assume_owned(self, py: Python<'_>) -> Bound<'_, PyAny> { |
| 54 | unsafe { Bound::from_owned_ptr(py, self) } |
| 55 | } |
| 56 | |
| 57 | #[inline ] |
| 58 | unsafe fn assume_owned_unchecked(self, py: Python<'_>) -> Bound<'_, PyAny> { |
| 59 | unsafe { Bound::from_owned_ptr_unchecked(py, self) } |
| 60 | } |
| 61 | |
| 62 | #[inline ] |
| 63 | unsafe fn assume_borrowed_or_err<'a>( |
| 64 | self, |
| 65 | py: Python<'_>, |
| 66 | ) -> PyResult<Borrowed<'a, '_, PyAny>> { |
| 67 | unsafe { Borrowed::from_ptr_or_err(py, self) } |
| 68 | } |
| 69 | |
| 70 | #[inline ] |
| 71 | unsafe fn assume_borrowed_or_opt<'a>(self, py: Python<'_>) -> Option<Borrowed<'a, '_, PyAny>> { |
| 72 | unsafe { Borrowed::from_ptr_or_opt(py, self) } |
| 73 | } |
| 74 | |
| 75 | #[inline ] |
| 76 | #[track_caller ] |
| 77 | unsafe fn assume_borrowed<'a>(self, py: Python<'_>) -> Borrowed<'a, '_, PyAny> { |
| 78 | unsafe { Borrowed::from_ptr(py, self) } |
| 79 | } |
| 80 | |
| 81 | #[inline ] |
| 82 | unsafe fn assume_borrowed_unchecked<'a>(self, py: Python<'_>) -> Borrowed<'a, '_, PyAny> { |
| 83 | unsafe { Borrowed::from_ptr_unchecked(py, self) } |
| 84 | } |
| 85 | } |
| 86 | |