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