1use super::any::PyAnyMethods;
2
3use crate::{ffi, instance::Bound, PyAny};
4
5#[repr(transparent)]
15pub struct PyInt(PyAny);
16
17pyobject_native_type_core!(PyInt, pyobject_native_static_type_object!(ffi::PyLong_Type), #checkfunction=ffi::PyLong_Check);
18
19#[deprecated(since = "0.23.0", note = "use `PyInt` instead")]
21pub type PyLong = PyInt;
22
23macro_rules! int_compare {
24 ($rust_type: ty) => {
25 impl PartialEq<$rust_type> for Bound<'_, PyInt> {
26 #[inline]
27 fn eq(&self, other: &$rust_type) -> bool {
28 if let Ok(value) = self.extract::<$rust_type>() {
29 value == *other
30 } else {
31 false
32 }
33 }
34 }
35 impl PartialEq<Bound<'_, PyInt>> for $rust_type {
36 #[inline]
37 fn eq(&self, other: &Bound<'_, PyInt>) -> bool {
38 if let Ok(value) = other.extract::<$rust_type>() {
39 value == *self
40 } else {
41 false
42 }
43 }
44 }
45 };
46}
47
48int_compare!(i8);
49int_compare!(u8);
50int_compare!(i16);
51int_compare!(u16);
52int_compare!(i32);
53int_compare!(u32);
54int_compare!(i64);
55int_compare!(u64);
56int_compare!(i128);
57int_compare!(u128);
58int_compare!(isize);
59int_compare!(usize);
60
61#[cfg(test)]
62mod tests {
63 use crate::{IntoPyObject, Python};
64
65 #[test]
66 fn test_partial_eq() {
67 Python::with_gil(|py| {
68 let v_i8 = 123i8;
69 let v_u8 = 123i8;
70 let v_i16 = 123i16;
71 let v_u16 = 123u16;
72 let v_i32 = 123i32;
73 let v_u32 = 123u32;
74 let v_i64 = 123i64;
75 let v_u64 = 123u64;
76 let v_i128 = 123i128;
77 let v_u128 = 123u128;
78 let v_isize = 123isize;
79 let v_usize = 123usize;
80 let obj = 123_i64.into_pyobject(py).unwrap();
81 assert_eq!(v_i8, obj);
82 assert_eq!(obj, v_i8);
83
84 assert_eq!(v_u8, obj);
85 assert_eq!(obj, v_u8);
86
87 assert_eq!(v_i16, obj);
88 assert_eq!(obj, v_i16);
89
90 assert_eq!(v_u16, obj);
91 assert_eq!(obj, v_u16);
92
93 assert_eq!(v_i32, obj);
94 assert_eq!(obj, v_i32);
95
96 assert_eq!(v_u32, obj);
97 assert_eq!(obj, v_u32);
98
99 assert_eq!(v_i64, obj);
100 assert_eq!(obj, v_i64);
101
102 assert_eq!(v_u64, obj);
103 assert_eq!(obj, v_u64);
104
105 assert_eq!(v_i128, obj);
106 assert_eq!(obj, v_i128);
107
108 assert_eq!(v_u128, obj);
109 assert_eq!(obj, v_u128);
110
111 assert_eq!(v_isize, obj);
112 assert_eq!(obj, v_isize);
113
114 assert_eq!(v_usize, obj);
115 assert_eq!(obj, v_usize);
116
117 let big_num = (u8::MAX as u16) + 1;
118 let big_obj = big_num.into_pyobject(py).unwrap();
119
120 for x in 0u8..=u8::MAX {
121 assert_ne!(x, big_obj);
122 assert_ne!(big_obj, x);
123 }
124 });
125 }
126}