Skip to content

Commit 5860c4f

Browse files
implement PartialEq for Pybool & bool (#4305)
* implement PartialEq for Pybool implement PartialEq for Pybool * fix failing cargodoc and add changelog file * Use PR number for change log file * Add false checking into test
1 parent 0af0227 commit 5860c4f

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed

newsfragments/4305.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Implement `PartialEq<bool>` for `Bound<'py, PyBool>`.

src/types/boolobject.rs

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,86 @@ impl<'py> PyBoolMethods<'py> for Bound<'py, PyBool> {
7272
}
7373
}
7474

75+
/// Compare `Bound<PyBool>` with `bool`.
76+
impl PartialEq<bool> for Bound<'_, PyBool> {
77+
#[inline]
78+
fn eq(&self, other: &bool) -> bool {
79+
self.as_borrowed() == *other
80+
}
81+
}
82+
83+
/// Compare `&Bound<PyBool>` with `bool`.
84+
impl PartialEq<bool> for &'_ Bound<'_, PyBool> {
85+
#[inline]
86+
fn eq(&self, other: &bool) -> bool {
87+
self.as_borrowed() == *other
88+
}
89+
}
90+
91+
/// Compare `Bound<PyBool>` with `&bool`.
92+
impl PartialEq<&'_ bool> for Bound<'_, PyBool> {
93+
#[inline]
94+
fn eq(&self, other: &&bool) -> bool {
95+
self.as_borrowed() == **other
96+
}
97+
}
98+
99+
/// Compare `bool` with `Bound<PyBool>`
100+
impl PartialEq<Bound<'_, PyBool>> for bool {
101+
#[inline]
102+
fn eq(&self, other: &Bound<'_, PyBool>) -> bool {
103+
*self == other.as_borrowed()
104+
}
105+
}
106+
107+
/// Compare `bool` with `&Bound<PyBool>`
108+
impl PartialEq<&'_ Bound<'_, PyBool>> for bool {
109+
#[inline]
110+
fn eq(&self, other: &&'_ Bound<'_, PyBool>) -> bool {
111+
*self == other.as_borrowed()
112+
}
113+
}
114+
115+
/// Compare `&bool` with `Bound<PyBool>`
116+
impl PartialEq<Bound<'_, PyBool>> for &'_ bool {
117+
#[inline]
118+
fn eq(&self, other: &Bound<'_, PyBool>) -> bool {
119+
**self == other.as_borrowed()
120+
}
121+
}
122+
123+
/// Compare `Borrowed<PyBool>` with `bool`
124+
impl PartialEq<bool> for Borrowed<'_, '_, PyBool> {
125+
#[inline]
126+
fn eq(&self, other: &bool) -> bool {
127+
self.is_true() == *other
128+
}
129+
}
130+
131+
/// Compare `Borrowed<PyBool>` with `&bool`
132+
impl PartialEq<&bool> for Borrowed<'_, '_, PyBool> {
133+
#[inline]
134+
fn eq(&self, other: &&bool) -> bool {
135+
self.is_true() == **other
136+
}
137+
}
138+
139+
/// Compare `bool` with `Borrowed<PyBool>`
140+
impl PartialEq<Borrowed<'_, '_, PyBool>> for bool {
141+
#[inline]
142+
fn eq(&self, other: &Borrowed<'_, '_, PyBool>) -> bool {
143+
*self == other.is_true()
144+
}
145+
}
146+
147+
/// Compare `&bool` with `Borrowed<PyBool>`
148+
impl PartialEq<Borrowed<'_, '_, PyBool>> for &'_ bool {
149+
#[inline]
150+
fn eq(&self, other: &Borrowed<'_, '_, PyBool>) -> bool {
151+
**self == other.is_true()
152+
}
153+
}
154+
75155
/// Converts a Rust `bool` to a Python `bool`.
76156
impl ToPyObject for bool {
77157
#[inline]
@@ -191,4 +271,63 @@ mod tests {
191271
assert!(false.to_object(py).is(&*PyBool::new_bound(py, false)));
192272
});
193273
}
274+
275+
#[test]
276+
fn test_pybool_comparisons() {
277+
Python::with_gil(|py| {
278+
let py_bool = PyBool::new_bound(py, true);
279+
let py_bool_false = PyBool::new_bound(py, false);
280+
let rust_bool = true;
281+
282+
// Bound<'_, PyBool> == bool
283+
assert_eq!(*py_bool, rust_bool);
284+
assert_ne!(*py_bool_false, rust_bool);
285+
286+
// Bound<'_, PyBool> == &bool
287+
assert_eq!(*py_bool, &rust_bool);
288+
assert_ne!(*py_bool_false, &rust_bool);
289+
290+
// &Bound<'_, PyBool> == bool
291+
assert_eq!(&*py_bool, rust_bool);
292+
assert_ne!(&*py_bool_false, rust_bool);
293+
294+
// &Bound<'_, PyBool> == &bool
295+
assert_eq!(&*py_bool, &rust_bool);
296+
assert_ne!(&*py_bool_false, &rust_bool);
297+
298+
// bool == Bound<'_, PyBool>
299+
assert_eq!(rust_bool, *py_bool);
300+
assert_ne!(rust_bool, *py_bool_false);
301+
302+
// bool == &Bound<'_, PyBool>
303+
assert_eq!(rust_bool, &*py_bool);
304+
assert_ne!(rust_bool, &*py_bool_false);
305+
306+
// &bool == Bound<'_, PyBool>
307+
assert_eq!(&rust_bool, *py_bool);
308+
assert_ne!(&rust_bool, *py_bool_false);
309+
310+
// &bool == &Bound<'_, PyBool>
311+
assert_eq!(&rust_bool, &*py_bool);
312+
assert_ne!(&rust_bool, &*py_bool_false);
313+
314+
// Borrowed<'_, '_, PyBool> == bool
315+
assert_eq!(py_bool, rust_bool);
316+
assert_ne!(py_bool_false, rust_bool);
317+
318+
// Borrowed<'_, '_, PyBool> == &bool
319+
assert_eq!(py_bool, &rust_bool);
320+
assert_ne!(py_bool_false, &rust_bool);
321+
322+
// bool == Borrowed<'_, '_, PyBool>
323+
assert_eq!(rust_bool, py_bool);
324+
assert_ne!(rust_bool, py_bool_false);
325+
326+
// &bool == Borrowed<'_, '_, PyBool>
327+
assert_eq!(&rust_bool, py_bool);
328+
assert_ne!(&rust_bool, py_bool_false);
329+
assert_eq!(py_bool, rust_bool);
330+
assert_ne!(py_bool_false, rust_bool);
331+
})
332+
}
194333
}

0 commit comments

Comments
 (0)