1use crate::sealed::Sealed;
2use crate::{
3 ffi,
4 instance::{Borrowed, Bound},
5 PyAny, PyResult, Python,
6};
7
8pub(crate) trait FfiPtrExt: Sealed {
9 unsafe fn assume_owned_or_err(self, py: Python<'_>) -> PyResult<Bound<'_, PyAny>>;
13
14 unsafe fn assume_owned_or_opt(self, py: Python<'_>) -> Option<Bound<'_, PyAny>>;
16
17 unsafe fn assume_owned(self, py: Python<'_>) -> Bound<'_, PyAny>;
19
20 unsafe fn assume_owned_unchecked(self, py: Python<'_>) -> Bound<'_, PyAny>;
22
23 unsafe fn assume_borrowed_or_err<'a>(self, py: Python<'_>)
28 -> PyResult<Borrowed<'a, '_, PyAny>>;
29
30 unsafe fn assume_borrowed_or_opt<'a>(self, py: Python<'_>) -> Option<Borrowed<'a, '_, PyAny>>;
32
33 unsafe fn assume_borrowed<'a>(self, py: Python<'_>) -> Borrowed<'a, '_, PyAny>;
35
36 unsafe fn assume_borrowed_unchecked<'a>(self, py: Python<'_>) -> Borrowed<'a, '_, PyAny>;
38}
39
40impl FfiPtrExt for *mut ffi::PyObject {
41 #[inline]
45 unsafe fn assume_owned_or_err(self, py: Python<'_>) -> PyResult<Bound<'_, PyAny>> {
46 unsafe { Bound::from_owned_ptr_or_err(py, self) }
48 }
49
50 #[inline]
54 unsafe fn assume_owned_or_opt(self, py: Python<'_>) -> Option<Bound<'_, PyAny>> {
55 unsafe { Bound::from_owned_ptr_or_opt(py, self) }
57 }
58
59 #[inline]
63 #[track_caller]
64 unsafe fn assume_owned(self, py: Python<'_>) -> Bound<'_, PyAny> {
65 unsafe { Bound::from_owned_ptr(py, self) }
67 }
68
69 #[inline]
73 unsafe fn assume_owned_unchecked(self, py: Python<'_>) -> Bound<'_, PyAny> {
74 unsafe { Bound::from_owned_ptr_unchecked(py, self) }
76 }
77
78 #[inline]
82 unsafe fn assume_borrowed_or_err<'a>(
83 self,
84 py: Python<'_>,
85 ) -> PyResult<Borrowed<'a, '_, PyAny>> {
86 unsafe { Borrowed::from_ptr_or_err(py, self) }
88 }
89
90 #[inline]
94 unsafe fn assume_borrowed_or_opt<'a>(self, py: Python<'_>) -> Option<Borrowed<'a, '_, PyAny>> {
95 unsafe { Borrowed::from_ptr_or_opt(py, self) }
97 }
98
99 #[inline]
103 #[track_caller]
104 unsafe fn assume_borrowed<'a>(self, py: Python<'_>) -> Borrowed<'a, '_, PyAny> {
105 unsafe { Borrowed::from_ptr(py, self) }
107 }
108
109 #[inline]
113 unsafe fn assume_borrowed_unchecked<'a>(self, py: Python<'_>) -> Borrowed<'a, '_, PyAny> {
114 unsafe { Borrowed::from_ptr_unchecked(py, self) }
116 }
117}