From 9d4567ba463d6c671053a5e0deca6392ff858f8b Mon Sep 17 00:00:00 2001 From: Owen Leung Date: Wed, 3 Jul 2024 00:40:19 +0800 Subject: [PATCH 1/4] implement PartialEq for Pybool implement PartialEq for Pybool --- src/types/boolobject.rs | 125 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) diff --git a/src/types/boolobject.rs b/src/types/boolobject.rs index 04c1fd4c113..4e9d611bdff 100644 --- a/src/types/boolobject.rs +++ b/src/types/boolobject.rs @@ -72,6 +72,86 @@ impl<'py> PyBoolMethods<'py> for Bound<'py, PyBool> { } } +/// Compare Bound with bool. +impl PartialEq for Bound<'_, PyBool> { + #[inline] + fn eq(&self, other: &bool) -> bool { + self.as_borrowed() == *other + } +} + +/// Compare &Bound with bool. +impl PartialEq for &'_ Bound<'_, PyBool> { + #[inline] + fn eq(&self, other: &bool) -> bool { + self.as_borrowed() == *other + } +} + +/// Compare Bound with &bool. +impl PartialEq<&'_ bool> for Bound<'_, PyBool> { + #[inline] + fn eq(&self, other: &&bool) -> bool { + self.as_borrowed() == **other + } +} + +/// Compare bool with Bound +impl PartialEq> for bool { + #[inline] + fn eq(&self, other: &Bound<'_, PyBool>) -> bool { + *self == other.as_borrowed() + } +} + +/// Compare bool with &Bound +impl PartialEq<&'_ Bound<'_, PyBool>> for bool { + #[inline] + fn eq(&self, other: &&'_ Bound<'_, PyBool>) -> bool { + *self == other.as_borrowed() + } +} + +/// Compare &bool with Bound +impl PartialEq> for &'_ bool { + #[inline] + fn eq(&self, other: &Bound<'_, PyBool>) -> bool { + **self == other.as_borrowed() + } +} + +/// Compare Borrowed with bool +impl PartialEq for Borrowed<'_, '_, PyBool> { + #[inline] + fn eq(&self, other: &bool) -> bool { + self.is_true() == *other + } +} + +/// Compare Borrowed with &bool +impl PartialEq<&bool> for Borrowed<'_, '_, PyBool> { + #[inline] + fn eq(&self, other: &&bool) -> bool { + self.is_true() == **other + } +} + +/// Compare bool with Borrowed +impl PartialEq> for bool { + #[inline] + fn eq(&self, other: &Borrowed<'_, '_, PyBool>) -> bool { + *self == other.is_true() + } +} + +/// Compare &bool with Borrowed +impl PartialEq> for &'_ bool { + #[inline] + fn eq(&self, other: &Borrowed<'_, '_, PyBool>) -> bool { + **self == other.is_true() + } +} + /// Converts a Rust `bool` to a Python `bool`. impl ToPyObject for bool { #[inline] @@ -191,4 +271,49 @@ mod tests { assert!(false.to_object(py).is(&*PyBool::new_bound(py, false))); }); } + + #[test] + fn test_pybool_comparisons() { + Python::with_gil(|py| { + let py_bool = PyBool::new_bound(py, true); + let rust_bool = true; + + // Bound<'_, PyBool> == bool + assert_eq!(*py_bool, rust_bool); + + // Bound<'_, PyBool> == &bool + assert_eq!(*py_bool, &rust_bool); + + // &Bound<'_, PyBool> == bool + assert_eq!(&*py_bool, rust_bool); + + // &Bound<'_, PyBool> == &bool + assert_eq!(&*py_bool, &rust_bool); + + // bool == Bound<'_, PyBool> + assert_eq!(rust_bool, *py_bool); + + // bool == &Bound<'_, PyBool> + assert_eq!(rust_bool, &*py_bool); + + // &bool == Bound<'_, PyBool> + assert_eq!(&rust_bool, *py_bool); + + // &bool == &Bound<'_, PyBool> + assert_eq!(&rust_bool, &*py_bool); + + // Borrowed<'_, '_, PyBool> == bool + assert_eq!(py_bool, rust_bool); + + // Borrowed<'_, '_, PyBool> == &bool + assert_eq!(py_bool, &rust_bool); + + // bool == Borrowed<'_, '_, PyBool> + assert_eq!(rust_bool, py_bool); + + // &bool == Borrowed<'_, '_, PyBool> + assert_eq!(&rust_bool, py_bool); + assert_eq!(py_bool, rust_bool) + }) + } } From 0fad93087ef4b4862692f9a759bedfa49e245adc Mon Sep 17 00:00:00 2001 From: Owen Leung Date: Wed, 3 Jul 2024 13:31:26 +0800 Subject: [PATCH 2/4] fix failing cargodoc and add changelog file --- newsfragments/4250.added.md | 1 + src/types/boolobject.rs | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 10 deletions(-) create mode 100644 newsfragments/4250.added.md diff --git a/newsfragments/4250.added.md b/newsfragments/4250.added.md new file mode 100644 index 00000000000..9a00b8fcfd1 --- /dev/null +++ b/newsfragments/4250.added.md @@ -0,0 +1 @@ +Implement `PartialEq` for `Bound<'py, PyBool>`. \ No newline at end of file diff --git a/src/types/boolobject.rs b/src/types/boolobject.rs index 4e9d611bdff..2ec562b78de 100644 --- a/src/types/boolobject.rs +++ b/src/types/boolobject.rs @@ -72,7 +72,7 @@ impl<'py> PyBoolMethods<'py> for Bound<'py, PyBool> { } } -/// Compare Bound with bool. +/// Compare `Bound` with `bool`. impl PartialEq for Bound<'_, PyBool> { #[inline] fn eq(&self, other: &bool) -> bool { @@ -80,7 +80,7 @@ impl PartialEq for Bound<'_, PyBool> { } } -/// Compare &Bound with bool. +/// Compare `&Bound` with `bool`. impl PartialEq for &'_ Bound<'_, PyBool> { #[inline] fn eq(&self, other: &bool) -> bool { @@ -88,7 +88,7 @@ impl PartialEq for &'_ Bound<'_, PyBool> { } } -/// Compare Bound with &bool. +/// Compare `Bound` with `&bool`. impl PartialEq<&'_ bool> for Bound<'_, PyBool> { #[inline] fn eq(&self, other: &&bool) -> bool { @@ -96,7 +96,7 @@ impl PartialEq<&'_ bool> for Bound<'_, PyBool> { } } -/// Compare bool with Bound +/// Compare `bool` with `Bound` impl PartialEq> for bool { #[inline] fn eq(&self, other: &Bound<'_, PyBool>) -> bool { @@ -104,7 +104,7 @@ impl PartialEq> for bool { } } -/// Compare bool with &Bound +/// Compare `bool` with `&Bound` impl PartialEq<&'_ Bound<'_, PyBool>> for bool { #[inline] fn eq(&self, other: &&'_ Bound<'_, PyBool>) -> bool { @@ -112,7 +112,7 @@ impl PartialEq<&'_ Bound<'_, PyBool>> for bool { } } -/// Compare &bool with Bound +/// Compare `&bool` with `Bound` impl PartialEq> for &'_ bool { #[inline] fn eq(&self, other: &Bound<'_, PyBool>) -> bool { @@ -120,7 +120,7 @@ impl PartialEq> for &'_ bool { } } -/// Compare Borrowed with bool +/// Compare `Borrowed` with `bool` impl PartialEq for Borrowed<'_, '_, PyBool> { #[inline] fn eq(&self, other: &bool) -> bool { @@ -128,7 +128,7 @@ impl PartialEq for Borrowed<'_, '_, PyBool> { } } -/// Compare Borrowed with &bool +/// Compare `Borrowed` with `&bool` impl PartialEq<&bool> for Borrowed<'_, '_, PyBool> { #[inline] fn eq(&self, other: &&bool) -> bool { @@ -136,7 +136,7 @@ impl PartialEq<&bool> for Borrowed<'_, '_, PyBool> { } } -/// Compare bool with Borrowed +/// Compare `bool` with `Borrowed` impl PartialEq> for bool { #[inline] fn eq(&self, other: &Borrowed<'_, '_, PyBool>) -> bool { @@ -144,7 +144,7 @@ impl PartialEq> for bool { } } -/// Compare &bool with Borrowed +/// Compare `&bool` with `Borrowed` impl PartialEq> for &'_ bool { #[inline] fn eq(&self, other: &Borrowed<'_, '_, PyBool>) -> bool { From 981a66691a7cdfcfd5e074d72615713a8c100b4b Mon Sep 17 00:00:00 2001 From: Owen Leung Date: Wed, 3 Jul 2024 13:34:29 +0800 Subject: [PATCH 3/4] Use PR number for change log file --- newsfragments/{4250.added.md => 4305.added.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename newsfragments/{4250.added.md => 4305.added.md} (100%) diff --git a/newsfragments/4250.added.md b/newsfragments/4305.added.md similarity index 100% rename from newsfragments/4250.added.md rename to newsfragments/4305.added.md From 9a484e45ec0cf45e1f8f88be33b4c9d2de975a2b Mon Sep 17 00:00:00 2001 From: Owen Leung Date: Wed, 3 Jul 2024 21:32:11 +0800 Subject: [PATCH 4/4] Add false checking into test --- src/types/boolobject.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/types/boolobject.rs b/src/types/boolobject.rs index 2ec562b78de..ee19797d66e 100644 --- a/src/types/boolobject.rs +++ b/src/types/boolobject.rs @@ -276,44 +276,58 @@ mod tests { fn test_pybool_comparisons() { Python::with_gil(|py| { let py_bool = PyBool::new_bound(py, true); + let py_bool_false = PyBool::new_bound(py, false); let rust_bool = true; // Bound<'_, PyBool> == bool assert_eq!(*py_bool, rust_bool); + assert_ne!(*py_bool_false, rust_bool); // Bound<'_, PyBool> == &bool assert_eq!(*py_bool, &rust_bool); + assert_ne!(*py_bool_false, &rust_bool); // &Bound<'_, PyBool> == bool assert_eq!(&*py_bool, rust_bool); + assert_ne!(&*py_bool_false, rust_bool); // &Bound<'_, PyBool> == &bool assert_eq!(&*py_bool, &rust_bool); + assert_ne!(&*py_bool_false, &rust_bool); // bool == Bound<'_, PyBool> assert_eq!(rust_bool, *py_bool); + assert_ne!(rust_bool, *py_bool_false); // bool == &Bound<'_, PyBool> assert_eq!(rust_bool, &*py_bool); + assert_ne!(rust_bool, &*py_bool_false); // &bool == Bound<'_, PyBool> assert_eq!(&rust_bool, *py_bool); + assert_ne!(&rust_bool, *py_bool_false); // &bool == &Bound<'_, PyBool> assert_eq!(&rust_bool, &*py_bool); + assert_ne!(&rust_bool, &*py_bool_false); // Borrowed<'_, '_, PyBool> == bool assert_eq!(py_bool, rust_bool); + assert_ne!(py_bool_false, rust_bool); // Borrowed<'_, '_, PyBool> == &bool assert_eq!(py_bool, &rust_bool); + assert_ne!(py_bool_false, &rust_bool); // bool == Borrowed<'_, '_, PyBool> assert_eq!(rust_bool, py_bool); + assert_ne!(rust_bool, py_bool_false); // &bool == Borrowed<'_, '_, PyBool> assert_eq!(&rust_bool, py_bool); - assert_eq!(py_bool, rust_bool) + assert_ne!(&rust_bool, py_bool_false); + assert_eq!(py_bool, rust_bool); + assert_ne!(py_bool_false, rust_bool); }) } }