pyo3/
internal_tricks.rs
1use crate::ffi::{self, Py_ssize_t, PY_SSIZE_T_MAX};
2pub struct PrivateMarker;
3
4macro_rules! private_decl {
5 () => {
6 fn __private__(&self) -> crate::internal_tricks::PrivateMarker;
9 };
10}
11
12macro_rules! private_impl {
13 () => {
14 fn __private__(&self) -> crate::internal_tricks::PrivateMarker {
15 crate::internal_tricks::PrivateMarker
16 }
17 };
18}
19
20macro_rules! pyo3_exception {
21 ($doc: expr, $name: ident, $base: ty) => {
22 #[doc = $doc]
23 #[repr(transparent)]
24 #[allow(non_camel_case_types)]
25 pub struct $name($crate::PyAny);
26
27 $crate::impl_exception_boilerplate!($name);
28
29 $crate::create_exception_type_object!(pyo3_runtime, $name, $base, Some($doc));
30 };
31}
32
33pub(crate) fn get_ssize_index(index: usize) -> Py_ssize_t {
36 index.min(PY_SSIZE_T_MAX as usize) as Py_ssize_t
37}
38
39#[inline]
41pub(crate) const fn ptr_from_ref<T>(t: &T) -> *const T {
42 t as *const T
43}
44
45#[inline]
47pub(crate) fn ptr_from_mut<T>(t: &mut T) -> *mut T {
48 t as *mut T
49}
50
51pub(crate) fn clear_eq(f: Option<ffi::inquiry>, g: ffi::inquiry) -> bool {
53 #[cfg(fn_ptr_eq)]
54 {
55 let Some(f) = f else { return false };
56 std::ptr::fn_addr_eq(f, g)
57 }
58
59 #[cfg(not(fn_ptr_eq))]
60 {
61 f == Some(g)
62 }
63}
64
65pub(crate) fn traverse_eq(f: Option<ffi::traverseproc>, g: ffi::traverseproc) -> bool {
67 #[cfg(fn_ptr_eq)]
68 {
69 let Some(f) = f else { return false };
70 std::ptr::fn_addr_eq(f, g)
71 }
72
73 #[cfg(not(fn_ptr_eq))]
74 {
75 f == Some(g)
76 }
77}