-
Notifications
You must be signed in to change notification settings - Fork 779
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 PyBytes and [u8] #4259
Changes from 4 commits
eaddbfc
a002fd8
af6d92f
daf147c
bd1c1b6
c4f3d3a
edd87ab
b488470
b8dc030
c90926e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Implement `PartialEq<str>` for `Bound<'py, PyBytes>`. | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,29 @@ use std::str; | |
/// Represents a Python `bytes` object. | ||
/// | ||
/// This type is immutable. | ||
/// | ||
/// # Equality | ||
/// | ||
/// For convenience, [`Bound<'py, PyBytes>`] implements [`PartialEq<str>`] to allow comparing the | ||
codeguru42 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// data in the Python bytes to a Rust `[u8]`. | ||
/// | ||
/// This is not always the most appropriate way to compare Python bytes, as Python bytes subclasses | ||
/// may have different equality semantics. In situations where subclasses overriding equality might be | ||
/// relevant, use [`PyAnyMethods::eq`], at cost of the additional overhead of a Python method call. | ||
/// | ||
/// ```rust | ||
/// # use pyo3::prelude::*; | ||
/// use pyo3::types::PyBytes; | ||
/// | ||
/// # Python::with_gil(|py| { | ||
/// let py_bytes = PyBytes::new_bound(py, b"foo"); | ||
/// // via PartialEq<[u8]> | ||
/// assert_eq!(py_bytes, b"foo"); | ||
/// | ||
/// // via Python equality | ||
/// assert!(py_bytes.as_any().eq(b"foo").unwrap()); | ||
/// # }); | ||
/// ``` | ||
codeguru42 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#[repr(transparent)] | ||
pub struct PyBytes(PyAny); | ||
|
||
|
@@ -191,6 +214,106 @@ impl<I: SliceIndex<[u8]>> Index<I> for Bound<'_, PyBytes> { | |
} | ||
} | ||
|
||
/// Compares whether the Python bytes object is equal to the [u8]. | ||
/// | ||
/// In some cases Python equality might be more appropriate; see the note on [`PyBytes`]. | ||
impl PartialEq<[u8]> for Bound<'_, PyBytes> { | ||
#[inline] | ||
fn eq(&self, other: &[u8]) -> bool { | ||
self.as_borrowed() == *other | ||
} | ||
} | ||
|
||
/// Compares whether the Python bytes object is equal to the [u8]. | ||
/// | ||
/// In some cases Python equality might be more appropriate; see the note on [`PyBytes`]. | ||
impl PartialEq<&'_ [u8]> for Bound<'_, PyBytes> { | ||
#[inline] | ||
fn eq(&self, other: &&[u8]) -> bool { | ||
self.as_borrowed() == **other | ||
} | ||
} | ||
|
||
/// Compares whether the Python bytes object is equal to the [u8]. | ||
/// | ||
/// In some cases Python equality might be more appropriate; see the note on [`PyBytes`]. | ||
impl PartialEq<Bound<'_, PyBytes>> for [u8] { | ||
#[inline] | ||
fn eq(&self, other: &Bound<'_, PyBytes>) -> bool { | ||
*self == other.as_borrowed() | ||
} | ||
} | ||
|
||
/// Compares whether the Python bytes object is equal to the [u8]. | ||
/// | ||
/// In some cases Python equality might be more appropriate; see the note on [`PyBytes`]. | ||
impl PartialEq<&'_ Bound<'_, PyBytes>> for [u8] { | ||
#[inline] | ||
fn eq(&self, other: &&Bound<'_, PyBytes>) -> bool { | ||
*self == other.as_borrowed() | ||
} | ||
} | ||
|
||
/// Compares whether the Python bytes object is equal to the [u8]. | ||
/// | ||
/// In some cases Python equality might be more appropriate; see the note on [`PyBytes`]. | ||
impl PartialEq<Bound<'_, PyBytes>> for &'_ [u8] { | ||
#[inline] | ||
fn eq(&self, other: &Bound<'_, PyBytes>) -> bool { | ||
**self == other.as_borrowed() | ||
} | ||
} | ||
|
||
/// Compares whether the Python bytes object is equal to the [u8]. | ||
/// | ||
/// In some cases Python equality might be more appropriate; see the note on [`PyBytes`]. | ||
impl PartialEq<[u8]> for &'_ Bound<'_, PyBytes> { | ||
#[inline] | ||
fn eq(&self, other: &[u8]) -> bool { | ||
self.as_borrowed() == other | ||
} | ||
} | ||
|
||
/// Compares whether the Python bytes object is equal to the [u8]. | ||
/// | ||
/// In some cases Python equality might be more appropriate; see the note on [`PyBytes`]. | ||
impl PartialEq<[u8]> for Borrowed<'_, '_, PyBytes> { | ||
#[inline] | ||
fn eq(&self, other: &[u8]) -> bool { | ||
self.to_cow().map_or(false, |s| s == other) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From what I can tell, all the other
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Icxolu Thanks for the feedback. I tried your suggestion and tests fail with
I guess we need the length of the array as part of the type? Or should I use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, this is because If we want we can also add implementations for arrays directly as well, using const generics, something like the following: impl <const N: usize> PartialEq<[u8; N]> for Borrowed<'_, '_, PyString> { ... } There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ...or are my tests wrong and need changed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They currently test comparison with arrays (which are nor implemented) instead of the comparison with slices. This can be a bit surprising, since often arrays can be automatically coerced to slices by the compiler. In this case the compiler is not smart enough to see that this is what we want. If you change your test to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For context, your first response wasn't showing when I typed my previous comment. |
||
} | ||
} | ||
|
||
/// Compares whether the Python bytes object is equal to the [u8]. | ||
/// | ||
/// In some cases Python equality might be more appropriate; see the note on [`PyBytes`]. | ||
impl PartialEq<&[u8]> for Borrowed<'_, '_, PyBytes> { | ||
#[inline] | ||
fn eq(&self, other: &&[u8]) -> bool { | ||
*self == **other | ||
} | ||
} | ||
|
||
/// Compares whether the Python bytes object is equal to the [u8]. | ||
/// | ||
/// In some cases Python equality might be more appropriate; see the note on [`PyBytes`]. | ||
impl PartialEq<Borrowed<'_, '_, PyBytes>> for [u8] { | ||
#[inline] | ||
fn eq(&self, other: &Borrowed<'_, '_, PyBytes>) -> bool { | ||
other == self | ||
} | ||
} | ||
|
||
/// Compares whether the Python bytes object is equal to the [u8]. | ||
/// | ||
/// In some cases Python equality might be more appropriate; see the note on [`PyBytes`]. | ||
impl PartialEq<Borrowed<'_, '_, PyBytes>> for &'_ [u8] { | ||
#[inline] | ||
fn eq(&self, other: &Borrowed<'_, '_, PyBytes>) -> bool { | ||
other == self | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
@@ -251,4 +374,34 @@ mod tests { | |
.is_instance_of::<PyValueError>(py)); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn test_comparisons() { | ||
Python::with_gil(|py| { | ||
let b = b"hello, world"; | ||
let py_bytes = PyBytes::new_bound(py, b); | ||
|
||
assert_eq!(py_bytes, b"hello, world"); | ||
|
||
assert_eq!(py_bytes, b); | ||
assert_eq!(&py_bytes, b); | ||
assert_eq!(b, py_bytes); | ||
assert_eq!(b, &py_bytes); | ||
|
||
assert_eq!(py_bytes, *b); | ||
assert_eq!(&py_bytes, *b); | ||
assert_eq!(*b, py_bytes); | ||
assert_eq!(*b, &py_bytes); | ||
|
||
let py_string = py_bytes.as_borrowed(); | ||
|
||
assert_eq!(py_string, b); | ||
assert_eq!(&py_string, b); | ||
assert_eq!(b, py_string); | ||
assert_eq!(b, &py_string); | ||
|
||
assert_eq!(py_string, *b); | ||
assert_eq!(*b, py_string); | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I saw this but wasn't able to accept it in time before the merge.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll fix it on my next PR for #4245
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No prob; I got it in #4266 thanks to @alex