Add essential traits: Clone, Eq, Hash, referencing traits, Debug
This commit is contained in:
+54
@@ -6,7 +6,13 @@ use std::ops::{
|
|||||||
DerefMut,
|
DerefMut,
|
||||||
};
|
};
|
||||||
use std::slice;
|
use std::slice;
|
||||||
|
|
||||||
|
// extra traits
|
||||||
use std::convert::From;
|
use std::convert::From;
|
||||||
|
use std::borrow::{Borrow, BorrowMut};
|
||||||
|
use std::convert::{AsRef, AsMut};
|
||||||
|
use std::hash::{Hash, Hasher};
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
/// Make sure the non-nullable pointer optimization does not occur!
|
/// Make sure the non-nullable pointer optimization does not occur!
|
||||||
enum Flag<T> {
|
enum Flag<T> {
|
||||||
@@ -364,6 +370,52 @@ impl<A: Array> iter::FromIterator<A::Item> for ArrayVec<A> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<A: Array> Clone for ArrayVec<A>
|
||||||
|
where A::Item: Clone
|
||||||
|
{
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
self.iter().cloned().collect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A: Array> Hash for ArrayVec<A>
|
||||||
|
where A::Item: Hash
|
||||||
|
{
|
||||||
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||||
|
Hash::hash(&**self, state)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A: Array> PartialEq for ArrayVec<A>
|
||||||
|
where A::Item: PartialEq
|
||||||
|
{
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
**self == **other
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A: Array> Eq for ArrayVec<A> where A::Item: Eq { }
|
||||||
|
|
||||||
|
impl<A: Array> Borrow<[A::Item]> for ArrayVec<A> {
|
||||||
|
fn borrow(&self) -> &[A::Item] { self }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A: Array> BorrowMut<[A::Item]> for ArrayVec<A> {
|
||||||
|
fn borrow_mut(&mut self) -> &mut [A::Item] { self }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A: Array> AsRef<[A::Item]> for ArrayVec<A> {
|
||||||
|
fn as_ref(&self) -> &[A::Item] { self }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A: Array> AsMut<[A::Item]> for ArrayVec<A> {
|
||||||
|
fn as_mut(&mut self) -> &mut [A::Item] { self }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A: Array> fmt::Debug for ArrayVec<A> where A::Item: fmt::Debug {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { (**self).fmt(f) }
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_simple() {
|
fn test_simple() {
|
||||||
use std::ops::Add;
|
use std::ops::Add;
|
||||||
@@ -378,6 +430,8 @@ fn test_simple() {
|
|||||||
println!("{:?}", elt);
|
println!("{:?}", elt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
println!("{:?}", vec);
|
||||||
|
|
||||||
let sum = vec.iter().map(|x| x.iter().fold(0, Add::add)).fold(0, Add::add);
|
let sum = vec.iter().map(|x| x.iter().fold(0, Add::add)).fold(0, Add::add);
|
||||||
assert_eq!(sum, 13 + 87);
|
assert_eq!(sum, 13 + 87);
|
||||||
let sum_len = vec.into_iter().map(|x| x.len()).fold(0, Add::add);
|
let sum_len = vec.into_iter().map(|x| x.len()).fold(0, Add::add);
|
||||||
|
|||||||
Reference in New Issue
Block a user