diff --git a/src/bytes.rs b/src/bytes.rs index 5d392951ee73358251f99134e84bf45a6b594edb..a5418273ebe1c9bc188ea05f0adf8f223a27fe0f 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -693,6 +693,18 @@ impl PartialEq for Bytes { } } +impl PartialOrd for Bytes { + fn partial_cmp(&self, other: &Bytes) -> Option<cmp::Ordering> { + self.inner.as_ref().partial_cmp(other.inner.as_ref()) + } +} + +impl Ord for Bytes { + fn cmp(&self, other: &Bytes) -> cmp::Ordering { + self.inner.as_ref().cmp(other.inner.as_ref()) + } +} + impl Eq for Bytes { } @@ -906,6 +918,47 @@ impl BytesMut { } } + /// Shortens the buffer, keeping the first `len` bytes and dropping the + /// rest. + /// + /// If `len` is greater than the buffer's current length, this has no + /// effect. + /// + /// The [`split_off`] method can emulate `truncate`, but this causes the + /// excess bytes to be returned instead of dropped. + /// + /// # Examples + /// + /// ``` + /// use bytes::BytesMut; + /// + /// let mut buf = BytesMut::from(&b"hello world"[..]); + /// buf.truncate(5); + /// assert_eq!(buf, b"hello"[..]); + /// ``` + /// + /// [`split_off`]: #method.split_off + pub fn truncate(&mut self, len: usize) { + if len <= self.len() { + unsafe { self.set_len(len); } + } + } + + /// Clears the buffer, removing all data. + /// + /// # Examples + /// + /// ``` + /// use bytes::BytesMut; + /// + /// let mut buf = BytesMut::from(&b"hello world"[..]); + /// buf.clear(); + /// assert!(buf.is_empty()); + /// ``` + pub fn clear(&mut self) { + self.truncate(0); + } + /// Sets the length of the buffer /// /// This will explicitly set the size of the buffer without actually @@ -1138,6 +1191,18 @@ impl PartialEq for BytesMut { } } +impl PartialOrd for BytesMut { + fn partial_cmp(&self, other: &BytesMut) -> Option<cmp::Ordering> { + self.inner.as_ref().partial_cmp(other.inner.as_ref()) + } +} + +impl Ord for BytesMut { + fn cmp(&self, other: &BytesMut) -> cmp::Ordering { + self.inner.as_ref().cmp(other.inner.as_ref()) + } +} + impl Eq for BytesMut { } @@ -1805,7 +1870,7 @@ impl ops::DerefMut for Inner2 { /* * - * ===== PartialEq ===== + * ===== PartialEq / PartialOrd ===== * */ @@ -1815,27 +1880,57 @@ impl PartialEq<[u8]> for BytesMut { } } +impl PartialOrd<[u8]> for BytesMut { + fn partial_cmp(&self, other: &[u8]) -> Option<cmp::Ordering> { + (**self).partial_cmp(other) + } +} + +impl PartialEq<BytesMut> for [u8] { + fn eq(&self, other: &BytesMut) -> bool { + *other == *self + } +} + +impl PartialOrd<BytesMut> for [u8] { + fn partial_cmp(&self, other: &BytesMut) -> Option<cmp::Ordering> { + other.partial_cmp(self) + } +} + impl PartialEq<str> for BytesMut { fn eq(&self, other: &str) -> bool { &**self == other.as_bytes() } } -impl PartialEq<BytesMut> for [u8] { +impl PartialOrd<str> for BytesMut { + fn partial_cmp(&self, other: &str) -> Option<cmp::Ordering> { + (**self).partial_cmp(other.as_bytes()) + } +} + +impl PartialEq<BytesMut> for str { fn eq(&self, other: &BytesMut) -> bool { *other == *self } } +impl PartialOrd<BytesMut> for str { + fn partial_cmp(&self, other: &BytesMut) -> Option<cmp::Ordering> { + other.partial_cmp(self) + } +} + impl PartialEq<Vec<u8>> for BytesMut { fn eq(&self, other: &Vec<u8>) -> bool { *self == &other[..] } } -impl PartialEq<String> for BytesMut { - fn eq(&self, other: &String) -> bool { - *self == &other[..] +impl PartialOrd<Vec<u8>> for BytesMut { + fn partial_cmp(&self, other: &Vec<u8>) -> Option<cmp::Ordering> { + (**self).partial_cmp(&other[..]) } } @@ -1845,12 +1940,36 @@ impl PartialEq<BytesMut> for Vec<u8> { } } +impl PartialOrd<BytesMut> for Vec<u8> { + fn partial_cmp(&self, other: &BytesMut) -> Option<cmp::Ordering> { + other.partial_cmp(self) + } +} + +impl PartialEq<String> for BytesMut { + fn eq(&self, other: &String) -> bool { + *self == &other[..] + } +} + +impl PartialOrd<String> for BytesMut { + fn partial_cmp(&self, other: &String) -> Option<cmp::Ordering> { + (**self).partial_cmp(other.as_bytes()) + } +} + impl PartialEq<BytesMut> for String { fn eq(&self, other: &BytesMut) -> bool { *other == *self } } +impl PartialOrd<BytesMut> for String { + fn partial_cmp(&self, other: &BytesMut) -> Option<cmp::Ordering> { + other.partial_cmp(self) + } +} + impl<'a, T: ?Sized> PartialEq<&'a T> for BytesMut where BytesMut: PartialEq<T> { @@ -1859,27 +1978,47 @@ impl<'a, T: ?Sized> PartialEq<&'a T> for BytesMut } } +impl<'a, T: ?Sized> PartialOrd<&'a T> for BytesMut + where BytesMut: PartialOrd<T> +{ + fn partial_cmp(&self, other: &&'a T) -> Option<cmp::Ordering> { + self.partial_cmp(*other) + } +} + impl<'a> PartialEq<BytesMut> for &'a [u8] { fn eq(&self, other: &BytesMut) -> bool { *other == *self } } +impl<'a> PartialOrd<BytesMut> for &'a [u8] { + fn partial_cmp(&self, other: &BytesMut) -> Option<cmp::Ordering> { + other.partial_cmp(self) + } +} + impl<'a> PartialEq<BytesMut> for &'a str { fn eq(&self, other: &BytesMut) -> bool { *other == *self } } +impl<'a> PartialOrd<BytesMut> for &'a str { + fn partial_cmp(&self, other: &BytesMut) -> Option<cmp::Ordering> { + other.partial_cmp(self) + } +} + impl PartialEq<[u8]> for Bytes { fn eq(&self, other: &[u8]) -> bool { self.inner.as_ref() == other } } -impl PartialEq<str> for Bytes { - fn eq(&self, other: &str) -> bool { - self.inner.as_ref() == other.as_bytes() +impl PartialOrd<[u8]> for Bytes { + fn partial_cmp(&self, other: &[u8]) -> Option<cmp::Ordering> { + self.inner.as_ref().partial_cmp(other) } } @@ -1889,21 +2028,45 @@ impl PartialEq<Bytes> for [u8] { } } +impl PartialOrd<Bytes> for [u8] { + fn partial_cmp(&self, other: &Bytes) -> Option<cmp::Ordering> { + other.partial_cmp(self) + } +} + +impl PartialEq<str> for Bytes { + fn eq(&self, other: &str) -> bool { + self.inner.as_ref() == other.as_bytes() + } +} + +impl PartialOrd<str> for Bytes { + fn partial_cmp(&self, other: &str) -> Option<cmp::Ordering> { + self.inner.as_ref().partial_cmp(other.as_bytes()) + } +} + impl PartialEq<Bytes> for str { fn eq(&self, other: &Bytes) -> bool { *other == *self } } +impl PartialOrd<Bytes> for str { + fn partial_cmp(&self, other: &Bytes) -> Option<cmp::Ordering> { + other.partial_cmp(self) + } +} + impl PartialEq<Vec<u8>> for Bytes { fn eq(&self, other: &Vec<u8>) -> bool { *self == &other[..] } } -impl PartialEq<String> for Bytes { - fn eq(&self, other: &String) -> bool { - *self == &other[..] +impl PartialOrd<Vec<u8>> for Bytes { + fn partial_cmp(&self, other: &Vec<u8>) -> Option<cmp::Ordering> { + self.inner.as_ref().partial_cmp(&other[..]) } } @@ -1913,24 +2076,60 @@ impl PartialEq<Bytes> for Vec<u8> { } } +impl PartialOrd<Bytes> for Vec<u8> { + fn partial_cmp(&self, other: &Bytes) -> Option<cmp::Ordering> { + other.partial_cmp(self) + } +} + +impl PartialEq<String> for Bytes { + fn eq(&self, other: &String) -> bool { + *self == &other[..] + } +} + +impl PartialOrd<String> for Bytes { + fn partial_cmp(&self, other: &String) -> Option<cmp::Ordering> { + self.inner.as_ref().partial_cmp(other.as_bytes()) + } +} + impl PartialEq<Bytes> for String { fn eq(&self, other: &Bytes) -> bool { *other == *self } } +impl PartialOrd<Bytes> for String { + fn partial_cmp(&self, other: &Bytes) -> Option<cmp::Ordering> { + other.partial_cmp(self) + } +} + impl<'a> PartialEq<Bytes> for &'a [u8] { fn eq(&self, other: &Bytes) -> bool { *other == *self } } +impl<'a> PartialOrd<Bytes> for &'a [u8] { + fn partial_cmp(&self, other: &Bytes) -> Option<cmp::Ordering> { + other.partial_cmp(self) + } +} + impl<'a> PartialEq<Bytes> for &'a str { fn eq(&self, other: &Bytes) -> bool { *other == *self } } +impl<'a> PartialOrd<Bytes> for &'a str { + fn partial_cmp(&self, other: &Bytes) -> Option<cmp::Ordering> { + other.partial_cmp(self) + } +} + impl<'a, T: ?Sized> PartialEq<&'a T> for Bytes where Bytes: PartialEq<T> { @@ -1938,3 +2137,11 @@ impl<'a, T: ?Sized> PartialEq<&'a T> for Bytes *self == **other } } + +impl<'a, T: ?Sized> PartialOrd<&'a T> for Bytes + where Bytes: PartialOrd<T> +{ + fn partial_cmp(&self, other: &&'a T) -> Option<cmp::Ordering> { + self.partial_cmp(&**other) + } +}