impl Debug, PartialEq & Eq

This commit is contained in:
Jorge Aparicio 2017-12-21 13:57:43 +01:00
parent c644d4cf13
commit 0869d70877

View File

@ -1,5 +1,5 @@
use core::marker::{PhantomData, Unsize}; use core::marker::{PhantomData, Unsize};
use core::{ops, ptr, slice}; use core::{fmt, ops, ptr, slice};
use untagged_option::UntaggedOption; use untagged_option::UntaggedOption;
@ -94,9 +94,10 @@ where
/// new_len is less than len, the Vec is simply truncated. /// new_len is less than len, the Vec is simply truncated.
/// ///
/// See also [`resize_default`]. /// See also [`resize_default`].
pub fn resize(&mut self, new_len: usize, value: T) pub fn resize(&mut self, new_len: usize, value: T) -> Result<(), BufferFullError>
-> Result<(), BufferFullError> where
where T: Clone { T: Clone,
{
if new_len > self.capacity() { if new_len > self.capacity() {
return Err(BufferFullError); return Err(BufferFullError);
} }
@ -119,13 +120,25 @@ where
/// If `new_len` is less than `len`, the `Vec` is simply truncated. /// If `new_len` is less than `len`, the `Vec` is simply truncated.
/// ///
/// See also [`resize`]. /// See also [`resize`].
pub fn resize_default(&mut self, new_len: usize) pub fn resize_default(&mut self, new_len: usize) -> Result<(), BufferFullError>
-> Result<(), BufferFullError> where
where T: Clone + Default { T: Clone + Default,
{
self.resize(new_len, T::default()) self.resize(new_len, T::default())
} }
} }
impl<T, A> fmt::Debug for Vec<T, A>
where
A: Unsize<[T]>,
T: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let slice: &[T] = &**self;
slice.fmt(f)
}
}
impl<T, A> Drop for Vec<T, A> impl<T, A> Drop for Vec<T, A>
where where
A: Unsize<[T]>, A: Unsize<[T]>,
@ -159,6 +172,24 @@ where
} }
} }
impl<T, A, B> PartialEq<Vec<T, B>> for Vec<T, A>
where
A: Unsize<[T]>,
B: Unsize<[T]>,
T: PartialEq,
{
fn eq(&self, rhs: &Vec<T, B>) -> bool {
PartialEq::eq(&**self, &**rhs)
}
}
impl<T, A> Eq for Vec<T, A>
where
A: Unsize<[T]>,
T: Eq,
{
}
impl<T, A> ops::Deref for Vec<T, A> impl<T, A> ops::Deref for Vec<T, A>
where where
A: Unsize<[T]>, A: Unsize<[T]>,
@ -225,6 +256,19 @@ mod tests {
assert_eq!(unsafe { COUNT }, 0); assert_eq!(unsafe { COUNT }, 0);
} }
#[test]
fn eq() {
let mut xs: Vec<i32, [i32; 4]> = Vec::new();
let mut ys: Vec<i32, [i32; 8]> = Vec::new();
assert_eq!(xs, ys);
xs.push(1).unwrap();
ys.push(1).unwrap();
assert_eq!(xs, ys);
}
#[test] #[test]
fn full() { fn full() {
let mut v: Vec<i32, [i32; 4]> = Vec::new(); let mut v: Vec<i32, [i32; 4]> = Vec::new();
@ -322,7 +366,6 @@ mod tests {
v.resize(2, 0).unwrap(); v.resize(2, 0).unwrap();
assert_eq!(v.len(), 2); assert_eq!(v.len(), 2);
// Shrink by 2 // Shrink by 2
v.resize(0, 0).unwrap(); v.resize(0, 0).unwrap();
assert_eq!(v.len(), 0); assert_eq!(v.len(), 0);