From fb2d8cf1c045059d116225fd614c53446e295fe9 Mon Sep 17 00:00:00 2001 From: Clint Byrum Date: Fri, 30 Jun 2017 20:33:01 -0700 Subject: [PATCH] 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 --- src/bytes.rs | 14 ++++++++++++++ tests/test_bytes.rs | 11 +++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/bytes.rs b/src/bytes.rs index 5945f3d..b53fbff 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -2525,3 +2525,17 @@ impl<'a, T: ?Sized> PartialOrd<&'a T> for Bytes self.partial_cmp(&**other) } } + +impl PartialEq for Bytes +{ + fn eq(&self, other: &BytesMut) -> bool { + &other[..] == &self[..] + } +} + +impl PartialEq for BytesMut +{ + fn eq(&self, other: &Bytes) -> bool { + &other[..] == &self[..] + } +} diff --git a/tests/test_bytes.rs b/tests/test_bytes.rs index b966fe0..1c8ccd0 100644 --- a/tests/test_bytes.rs +++ b/tests/test_bytes.rs @@ -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); +}