Skip to content
Snippets Groups Projects
Commit 72a79e68 authored by homunkulus's avatar homunkulus
Browse files

Auto merge of #14 - japaric:debug-eq, r=japaric

impl Debug, PartialEq & Eq

cc @fluffysquirrels
parents c644d4cf 6d04705a
No related branches found
No related tags found
No related merge requests found
......@@ -6,8 +6,7 @@ matrix:
rust: nightly
- env: TARGET=thumbv6m-none-eabi
# work around rust-lang/rust#45802
rust: nightly-2017-11-01
rust: nightly
addons:
apt:
sources:
......@@ -34,7 +33,11 @@ script:
after_script: set +e
cache: cargo
cache:
cargo: true
directories:
- $HOME/.xargo
before_cache:
# Travis can't cache files that are not readable by "others"
- chmod -R a+r $HOME/.cargo
......
use core::marker::{PhantomData, Unsize};
use core::{ops, ptr, slice};
use core::{fmt, ops, ptr, slice};
use untagged_option::UntaggedOption;
......@@ -94,9 +94,10 @@ where
/// new_len is less than len, the Vec is simply truncated.
///
/// See also [`resize_default`].
pub fn resize(&mut self, new_len: usize, value: T)
-> Result<(), BufferFullError>
where T: Clone {
pub fn resize(&mut self, new_len: usize, value: T) -> Result<(), BufferFullError>
where
T: Clone,
{
if new_len > self.capacity() {
return Err(BufferFullError);
}
......@@ -119,13 +120,25 @@ where
/// If `new_len` is less than `len`, the `Vec` is simply truncated.
///
/// See also [`resize`].
pub fn resize_default(&mut self, new_len: usize)
-> Result<(), BufferFullError>
where T: Clone + Default {
pub fn resize_default(&mut self, new_len: usize) -> Result<(), BufferFullError>
where
T: Clone + 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>
where
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>
where
A: Unsize<[T]>,
......@@ -225,6 +256,19 @@ mod tests {
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]
fn full() {
let mut v: Vec<i32, [i32; 4]> = Vec::new();
......@@ -322,7 +366,6 @@ mod tests {
v.resize(2, 0).unwrap();
assert_eq!(v.len(), 2);
// Shrink by 2
v.resize(0, 0).unwrap();
assert_eq!(v.len(), 0);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment