mirror of
https://github.com/rust-embedded/heapless.git
synced 2025-10-02 06:50:32 +00:00
Auto merge of #14 - japaric:debug-eq, r=japaric
impl Debug, PartialEq & Eq cc @fluffysquirrels
This commit is contained in:
commit
72a79e6833
@ -6,8 +6,7 @@ matrix:
|
|||||||
rust: nightly
|
rust: nightly
|
||||||
|
|
||||||
- env: TARGET=thumbv6m-none-eabi
|
- env: TARGET=thumbv6m-none-eabi
|
||||||
# work around rust-lang/rust#45802
|
rust: nightly
|
||||||
rust: nightly-2017-11-01
|
|
||||||
addons:
|
addons:
|
||||||
apt:
|
apt:
|
||||||
sources:
|
sources:
|
||||||
@ -34,7 +33,11 @@ script:
|
|||||||
|
|
||||||
after_script: set +e
|
after_script: set +e
|
||||||
|
|
||||||
cache: cargo
|
cache:
|
||||||
|
cargo: true
|
||||||
|
directories:
|
||||||
|
- $HOME/.xargo
|
||||||
|
|
||||||
before_cache:
|
before_cache:
|
||||||
# Travis can't cache files that are not readable by "others"
|
# Travis can't cache files that are not readable by "others"
|
||||||
- chmod -R a+r $HOME/.cargo
|
- chmod -R a+r $HOME/.cargo
|
||||||
|
59
src/vec.rs
59
src/vec.rs
@ -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);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user