Skip to content

Commit

Permalink
implement PartialEq for Pybool & bool (#4305)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Owen-CH-Leung authored Jul 5, 2024
1 parent 0af0227 commit 5860c4f
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
1 change: 1 addition & 0 deletions newsfragments/4305.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Implement `PartialEq<bool>` for `Bound<'py, PyBool>`.
139 changes: 139 additions & 0 deletions src/types/boolobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,86 @@ impl<'py> PyBoolMethods<'py> for Bound<'py, PyBool> {
}
}

/// Compare `Bound<PyBool>` with `bool`.
impl PartialEq<bool> for Bound<'_, PyBool> {
#[inline]
fn eq(&self, other: &bool) -> bool {
self.as_borrowed() == *other
}
}

/// Compare `&Bound<PyBool>` with `bool`.
impl PartialEq<bool> for &'_ Bound<'_, PyBool> {
#[inline]
fn eq(&self, other: &bool) -> bool {
self.as_borrowed() == *other
}
}

/// Compare `Bound<PyBool>` with `&bool`.
impl PartialEq<&'_ bool> for Bound<'_, PyBool> {
#[inline]
fn eq(&self, other: &&bool) -> bool {
self.as_borrowed() == **other
}
}

/// Compare `bool` with `Bound<PyBool>`
impl PartialEq<Bound<'_, PyBool>> for bool {
#[inline]
fn eq(&self, other: &Bound<'_, PyBool>) -> bool {
*self == other.as_borrowed()
}
}

/// Compare `bool` with `&Bound<PyBool>`
impl PartialEq<&'_ Bound<'_, PyBool>> for bool {
#[inline]
fn eq(&self, other: &&'_ Bound<'_, PyBool>) -> bool {
*self == other.as_borrowed()
}
}

/// Compare `&bool` with `Bound<PyBool>`
impl PartialEq<Bound<'_, PyBool>> for &'_ bool {
#[inline]
fn eq(&self, other: &Bound<'_, PyBool>) -> bool {
**self == other.as_borrowed()
}
}

/// Compare `Borrowed<PyBool>` with `bool`
impl PartialEq<bool> for Borrowed<'_, '_, PyBool> {
#[inline]
fn eq(&self, other: &bool) -> bool {
self.is_true() == *other
}
}

/// Compare `Borrowed<PyBool>` with `&bool`
impl PartialEq<&bool> for Borrowed<'_, '_, PyBool> {
#[inline]
fn eq(&self, other: &&bool) -> bool {
self.is_true() == **other
}
}

/// Compare `bool` with `Borrowed<PyBool>`
impl PartialEq<Borrowed<'_, '_, PyBool>> for bool {
#[inline]
fn eq(&self, other: &Borrowed<'_, '_, PyBool>) -> bool {
*self == other.is_true()
}
}

/// Compare `&bool` with `Borrowed<PyBool>`
impl PartialEq<Borrowed<'_, '_, PyBool>> 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]
Expand Down Expand Up @@ -191,4 +271,63 @@ 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 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_ne!(&rust_bool, py_bool_false);
assert_eq!(py_bool, rust_bool);
assert_ne!(py_bool_false, rust_bool);
})
}
}

0 comments on commit 5860c4f

Please sign in to comment.