Skip to content

Commit

Permalink
util: Primarily fixes a bug where SmallVec.into_iter just doesn't work.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Clark Gaebel committed Oct 28, 2014
1 parent 9e94ecf commit 4d610e3
Showing 1 changed file with 35 additions and 24 deletions.
59 changes: 35 additions & 24 deletions components/util/smallvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use std::kinds::marker::ContravariantLifetime;
use std::mem;
use std::ptr;
use std::raw::Slice;
use rustrt::local_heap;
use alloc::heap;

// Generic code for all small vectors
Expand Down Expand Up @@ -60,6 +59,7 @@ pub trait SmallVecPrivate<T> {
pub trait SmallVec<T> : SmallVecPrivate<T> where T: 'static {
fn inline_size(&self) -> uint;
fn len(&self) -> uint;
fn is_empty(&self) -> bool;
fn cap(&self) -> uint;

fn spilled(&self) -> bool {
Expand Down Expand Up @@ -110,12 +110,13 @@ pub trait SmallVec<T> : SmallVecPrivate<T> where T: 'static {
} else {
None
};
let cap = self.cap();
let inline_size = self.inline_size();
self.set_cap(inline_size);
self.set_len(0);
SmallVecMoveIterator {
allocation: ptr_opt,
cap: inline_size,
cap: cap,
iter: iter,
lifetime: ContravariantLifetime::<'a>,
}
Expand Down Expand Up @@ -169,13 +170,9 @@ pub trait SmallVec<T> : SmallVecPrivate<T> where T: 'static {
ptr::copy_nonoverlapping_memory(new_alloc, self.begin(), self.len());

if self.spilled() {
if intrinsics::owns_managed::<T>() {
local_heap::local_free(self.ptr() as *mut u8)
} else {
heap::deallocate(self.mut_ptr() as *mut u8,
mem::size_of::<T>() * self.cap(),
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 {
let mut_begin: *mut T = mem::transmute(self.begin());
intrinsics::set_memory(mut_begin, 0, self.len())
Expand Down Expand Up @@ -326,13 +323,9 @@ impl<'a, T: 'static> Drop for SmallVecMoveIterator<'a,T> {
None => {}
Some(allocation) => {
unsafe {
if intrinsics::owns_managed::<T>() {
local_heap::local_free(allocation as *mut u8)
} else {
heap::deallocate(allocation as *mut u8,
mem::size_of::<T>() * self.cap,
mem::min_align_of::<T>())
}
heap::deallocate(allocation as *mut u8,
mem::size_of::<T>() * self.cap,
mem::min_align_of::<T>())
}
}
}
Expand Down Expand Up @@ -383,6 +376,9 @@ macro_rules! def_small_vector(
fn len(&self) -> uint {
self.len
}
fn is_empty(&self) -> bool {
self.len == 0
}
fn cap(&self) -> uint {
self.cap
}
Expand All @@ -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> {
#[inline]
pub fn new() -> $name<T> {
Expand Down Expand Up @@ -444,13 +460,9 @@ macro_rules! def_small_vector_drop_impl(
*ptr.offset(i as int) = mem::uninitialized();
}

if intrinsics::owns_managed::<T>() {
local_heap::local_free(self.ptr() as *mut u8)
} else {
heap::deallocate(self.mut_ptr() as *mut u8,
mem::size_of::<T>() * self.cap(),
mem::min_align_of::<T>())
}
heap::deallocate(self.mut_ptr() as *mut u8,
mem::size_of::<T>() * self.cap(),
mem::min_align_of::<T>())
}
}
}
Expand Down Expand Up @@ -527,4 +539,3 @@ pub mod tests {
].as_slice());
}
}

0 comments on commit 4d610e3

Please sign in to comment.