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]
42 unsafe fn assume_owned_or_err(self, py: Python<'_>) -> PyResult<Bound<'_, PyAny>> {
43 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 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 Bound::from_owned_ptr(py, self)
55 }
56
57 #[inline]
58 unsafe fn assume_owned_unchecked(self, py: Python<'_>) -> Bound<'_, PyAny> {
59 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 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 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 Borrowed::from_ptr(py, self)
79 }
80
81 #[inline]
82 unsafe fn assume_borrowed_unchecked<'a>(self, py: Python<'_>) -> Borrowed<'a, '_, PyAny> {
83 Borrowed::from_ptr_unchecked(py, self)
84 }
85}