Add convenience PartialEq for BytesMut and Bytes (#141)

Saves the cognitive load of having to wrap them in slices to compare
them when that seems like what one would expect.

Signed-off-by: Clint Byrum <clint@fewbar.com>
This commit is contained in:
Clint Byrum
2017-06-30 20:33:01 -07:00
committed by Carl Lerche
parent 7ed78cef47
commit fb2d8cf1c0
2 changed files with 25 additions and 0 deletions
+14
View File
@@ -2525,3 +2525,17 @@ impl<'a, T: ?Sized> PartialOrd<&'a T> for Bytes
self.partial_cmp(&**other)
}
}
impl PartialEq<BytesMut> for Bytes
{
fn eq(&self, other: &BytesMut) -> bool {
&other[..] == &self[..]
}
}
impl PartialEq<Bytes> for BytesMut
{
fn eq(&self, other: &Bytes) -> bool {
&other[..] == &self[..]
}
}
+11
View File
@@ -503,3 +503,14 @@ fn stress() {
assert_eq!(*buf, data[..]);
}
}
#[test]
fn partial_eq_bytesmut() {
let bytes = Bytes::from(&b"The quick red fox"[..]);
let bytesmut = BytesMut::from(&b"The quick red fox"[..]);
assert!(bytes == bytesmut);
assert!(bytesmut == bytes);
let bytes2 = Bytes::from(&b"Jumped over the lazy brown dog"[..]);
assert!(bytes2 != bytesmut);
assert!(bytesmut != bytes2);
}