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