Add PartialOrd and Ord to EasyBuf, generalize PartialEq.

This commit is contained in:
Martin Hoffmann
2017-02-18 20:30:01 +01:00
parent 1bc2ef6aff
commit 54dcd3a015
+25 -6
View File
@@ -1,5 +1,6 @@
use std::fmt;
use std::io;
use std::hash;
use std::mem;
use std::cmp;
use std::ops::{Deref, DerefMut};
@@ -24,12 +25,6 @@ pub struct EasyBuf {
end: usize,
}
impl PartialEq for EasyBuf {
fn eq(&self, other: &EasyBuf) -> bool {
self.as_slice() == other.as_slice()
}
}
/// An RAII object returned from `get_mut` which provides mutable access to the
/// underlying `Vec<u8>`.
pub struct EasyBufMut<'a> {
@@ -201,6 +196,30 @@ impl From<Vec<u8>> for EasyBuf {
}
}
impl<T: AsRef<[u8]>> PartialEq<T> for EasyBuf {
fn eq(&self, other: &T) -> bool {
self.as_slice().eq(other.as_ref())
}
}
impl Ord for EasyBuf {
fn cmp(&self, other: &Self) -> cmp::Ordering {
self.as_slice().cmp(other.as_slice())
}
}
impl<T: AsRef<[u8]>> PartialOrd<T> for EasyBuf {
fn partial_cmp(&self, other: &T) -> Option<cmp::Ordering> {
self.as_slice().partial_cmp(other.as_ref())
}
}
impl hash::Hash for EasyBuf {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
self.as_slice().hash(state)
}
}
impl<'a> Drop for EasyBufMut<'a> {
fn drop(&mut self) {
*self.end = self.buf.len();