util: Add some utility methods to SmallVec.

This commit is contained in:
Patrick Walton 2014-03-28 12:47:32 -07:00
parent 1b04165f67
commit dae4166eb3

View file

@ -60,6 +60,12 @@ pub trait SmallVec<T> : SmallVecPrivate<T> {
}
}
fn mut_iter<'a>(&'a mut self) -> SmallVecMutIterator<'a,T> {
SmallVecMutIterator {
iter: self.iter(),
}
}
/// NB: For efficiency reasons (avoiding making a second copy of the inline elements), this
/// actually clears out the original array instead of moving it.
fn move_iter<'a>(&'a mut self) -> SmallVecMoveIterator<'a,T> {
@ -94,6 +100,12 @@ pub trait SmallVec<T> : SmallVecPrivate<T> {
}
}
fn push_all_move<V:SmallVec<T>>(&mut self, mut other: V) {
for value in other.move_iter() {
self.push(value)
}
}
fn grow(&mut self, new_cap: uint) {
unsafe {
let new_alloc: *mut T = cast::transmute(global_heap::malloc_raw(mem::size_of::<T>() *
@ -194,6 +206,22 @@ impl<'a,T> Iterator<&'a T> for SmallVecIterator<'a,T> {
}
}
pub struct SmallVecMutIterator<'a,T> {
priv iter: SmallVecIterator<'a,T>,
}
impl<'a,T> Iterator<&'a mut T> for SmallVecMutIterator<'a,T> {
#[inline]
fn next(&mut self) -> Option<&'a mut T> {
unsafe {
match self.iter.next() {
None => None,
Some(reference) => Some(cast::transmute::<&'a T,&'a mut T>(reference)),
}
}
}
}
pub struct SmallVecMoveIterator<'a,T> {
priv allocation: Option<*mut u8>,
priv iter: SmallVecIterator<'static,T>,