Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement PartialEq for Pybool & bool #4305

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>`.
125 changes: 125 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,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)
})
}
LilyFoote marked this conversation as resolved.
Show resolved Hide resolved
}
Loading