util: Primarily fixes a bug where SmallVec.into_iter just doesn't work.

into_iter used to use `inline_size` as the capacity insetad of the actual
capacity. This patch fixes that, adds some utility methods to `SmallVec`
to bring it closer in functionality to `Vec`, and removes the obsolete
`owns_managed` calls.
This commit is contained in:
Clark Gaebel 2014-10-28 10:22:34 -07:00
parent 9e94ecf99c
commit 4d610e36bd

View file

@ -12,7 +12,6 @@ use std::kinds::marker::ContravariantLifetime;
use std::mem; use std::mem;
use std::ptr; use std::ptr;
use std::raw::Slice; use std::raw::Slice;
use rustrt::local_heap;
use alloc::heap; use alloc::heap;
// Generic code for all small vectors // Generic code for all small vectors
@ -60,6 +59,7 @@ pub trait SmallVecPrivate<T> {
pub trait SmallVec<T> : SmallVecPrivate<T> where T: 'static { pub trait SmallVec<T> : SmallVecPrivate<T> where T: 'static {
fn inline_size(&self) -> uint; fn inline_size(&self) -> uint;
fn len(&self) -> uint; fn len(&self) -> uint;
fn is_empty(&self) -> bool;
fn cap(&self) -> uint; fn cap(&self) -> uint;
fn spilled(&self) -> bool { fn spilled(&self) -> bool {
@ -110,12 +110,13 @@ pub trait SmallVec<T> : SmallVecPrivate<T> where T: 'static {
} else { } else {
None None
}; };
let cap = self.cap();
let inline_size = self.inline_size(); let inline_size = self.inline_size();
self.set_cap(inline_size); self.set_cap(inline_size);
self.set_len(0); self.set_len(0);
SmallVecMoveIterator { SmallVecMoveIterator {
allocation: ptr_opt, allocation: ptr_opt,
cap: inline_size, cap: cap,
iter: iter, iter: iter,
lifetime: ContravariantLifetime::<'a>, lifetime: ContravariantLifetime::<'a>,
} }
@ -169,13 +170,9 @@ pub trait SmallVec<T> : SmallVecPrivate<T> where T: 'static {
ptr::copy_nonoverlapping_memory(new_alloc, self.begin(), self.len()); ptr::copy_nonoverlapping_memory(new_alloc, self.begin(), self.len());
if self.spilled() { if self.spilled() {
if intrinsics::owns_managed::<T>() { heap::deallocate(self.mut_ptr() as *mut u8,
local_heap::local_free(self.ptr() as *mut u8) mem::size_of::<T>() * self.cap(),
} else { mem::min_align_of::<T>())
heap::deallocate(self.mut_ptr() as *mut u8,
mem::size_of::<T>() * self.cap(),
mem::min_align_of::<T>())
}
} else { } else {
let mut_begin: *mut T = mem::transmute(self.begin()); let mut_begin: *mut T = mem::transmute(self.begin());
intrinsics::set_memory(mut_begin, 0, self.len()) intrinsics::set_memory(mut_begin, 0, self.len())
@ -326,13 +323,9 @@ impl<'a, T: 'static> Drop for SmallVecMoveIterator<'a,T> {
None => {} None => {}
Some(allocation) => { Some(allocation) => {
unsafe { unsafe {
if intrinsics::owns_managed::<T>() { heap::deallocate(allocation as *mut u8,
local_heap::local_free(allocation as *mut u8) mem::size_of::<T>() * self.cap,
} else { mem::min_align_of::<T>())
heap::deallocate(allocation as *mut u8,
mem::size_of::<T>() * self.cap,
mem::min_align_of::<T>())
}
} }
} }
} }
@ -383,6 +376,9 @@ macro_rules! def_small_vector(
fn len(&self) -> uint { fn len(&self) -> uint {
self.len self.len
} }
fn is_empty(&self) -> bool {
self.len == 0
}
fn cap(&self) -> uint { fn cap(&self) -> uint {
self.cap self.cap
} }
@ -405,6 +401,26 @@ macro_rules! def_small_vector(
} }
} }
impl<T: 'static> FromIterator<T> for $name<T> {
fn from_iter<I: Iterator<T>>(mut iter: I) -> $name<T> {
let mut v = $name::new();
for elem in iter {
v.push(elem);
}
v
}
}
impl<T: 'static> Extendable<T> for $name<T> {
fn extend<I: Iterator<T>>(&mut self, mut iter: I) {
for elem in iter {
self.push(elem);
}
}
}
impl<T: 'static> $name<T> { impl<T: 'static> $name<T> {
#[inline] #[inline]
pub fn new() -> $name<T> { pub fn new() -> $name<T> {
@ -444,13 +460,9 @@ macro_rules! def_small_vector_drop_impl(
*ptr.offset(i as int) = mem::uninitialized(); *ptr.offset(i as int) = mem::uninitialized();
} }
if intrinsics::owns_managed::<T>() { heap::deallocate(self.mut_ptr() as *mut u8,
local_heap::local_free(self.ptr() as *mut u8) mem::size_of::<T>() * self.cap(),
} else { mem::min_align_of::<T>())
heap::deallocate(self.mut_ptr() as *mut u8,
mem::size_of::<T>() * self.cap(),
mem::min_align_of::<T>())
}
} }
} }
} }
@ -527,4 +539,3 @@ pub mod tests {
].as_slice()); ].as_slice());
} }
} }