Add some missing Bytes impls and fns

This commit is contained in:
Carl Lerche
2017-02-20 14:03:37 -08:00
parent 99aafb0a22
commit c6fe5a1e4f
+220 -13
View File
@@ -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 { 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 /// Sets the length of the buffer
/// ///
/// This will explicitly set the size of the buffer without actually /// 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 { impl Eq for BytesMut {
} }
@@ -1805,7 +1870,7 @@ impl ops::DerefMut for Inner2 {
/* /*
* *
* ===== PartialEq ===== * ===== PartialEq / PartialOrd =====
* *
*/ */
@@ -1815,9 +1880,9 @@ impl PartialEq<[u8]> for BytesMut {
} }
} }
impl PartialEq<str> for BytesMut { impl PartialOrd<[u8]> for BytesMut {
fn eq(&self, other: &str) -> bool { fn partial_cmp(&self, other: &[u8]) -> Option<cmp::Ordering> {
&**self == other.as_bytes() (**self).partial_cmp(other)
} }
} }
@@ -1827,15 +1892,45 @@ impl PartialEq<BytesMut> for [u8] {
} }
} }
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 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 { impl PartialEq<Vec<u8>> for BytesMut {
fn eq(&self, other: &Vec<u8>) -> bool { fn eq(&self, other: &Vec<u8>) -> bool {
*self == &other[..] *self == &other[..]
} }
} }
impl PartialEq<String> for BytesMut { impl PartialOrd<Vec<u8>> for BytesMut {
fn eq(&self, other: &String) -> bool { fn partial_cmp(&self, other: &Vec<u8>) -> Option<cmp::Ordering> {
*self == &other[..] (**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 { impl PartialEq<BytesMut> for String {
fn eq(&self, other: &BytesMut) -> bool { fn eq(&self, other: &BytesMut) -> bool {
*other == *self *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 impl<'a, T: ?Sized> PartialEq<&'a T> for BytesMut
where BytesMut: PartialEq<T> 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] { impl<'a> PartialEq<BytesMut> for &'a [u8] {
fn eq(&self, other: &BytesMut) -> bool { fn eq(&self, other: &BytesMut) -> bool {
*other == *self *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 { impl<'a> PartialEq<BytesMut> for &'a str {
fn eq(&self, other: &BytesMut) -> bool { fn eq(&self, other: &BytesMut) -> bool {
*other == *self *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 { impl PartialEq<[u8]> for Bytes {
fn eq(&self, other: &[u8]) -> bool { fn eq(&self, other: &[u8]) -> bool {
self.inner.as_ref() == other self.inner.as_ref() == other
} }
} }
impl PartialEq<str> for Bytes { impl PartialOrd<[u8]> for Bytes {
fn eq(&self, other: &str) -> bool { fn partial_cmp(&self, other: &[u8]) -> Option<cmp::Ordering> {
self.inner.as_ref() == other.as_bytes() 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 { impl PartialEq<Bytes> for str {
fn eq(&self, other: &Bytes) -> bool { fn eq(&self, other: &Bytes) -> bool {
*other == *self *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 { impl PartialEq<Vec<u8>> for Bytes {
fn eq(&self, other: &Vec<u8>) -> bool { fn eq(&self, other: &Vec<u8>) -> bool {
*self == &other[..] *self == &other[..]
} }
} }
impl PartialEq<String> for Bytes { impl PartialOrd<Vec<u8>> for Bytes {
fn eq(&self, other: &String) -> bool { fn partial_cmp(&self, other: &Vec<u8>) -> Option<cmp::Ordering> {
*self == &other[..] 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 { impl PartialEq<Bytes> for String {
fn eq(&self, other: &Bytes) -> bool { fn eq(&self, other: &Bytes) -> bool {
*other == *self *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] { impl<'a> PartialEq<Bytes> for &'a [u8] {
fn eq(&self, other: &Bytes) -> bool { fn eq(&self, other: &Bytes) -> bool {
*other == *self *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 { impl<'a> PartialEq<Bytes> for &'a str {
fn eq(&self, other: &Bytes) -> bool { fn eq(&self, other: &Bytes) -> bool {
*other == *self *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 impl<'a, T: ?Sized> PartialEq<&'a T> for Bytes
where Bytes: PartialEq<T> where Bytes: PartialEq<T>
{ {
@@ -1938,3 +2137,11 @@ impl<'a, T: ?Sized> PartialEq<&'a T> for Bytes
*self == **other *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)
}
}