mirror of
https://github.com/servo/servo.git
synced 2025-08-04 13:10:20 +01:00
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:
parent
9e94ecf99c
commit
4d610e36bd
1 changed files with 35 additions and 24 deletions
|
@ -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
|
||||
|
@ -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 {
|
||||
|
@ -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>,
|
||||
}
|
||||
|
@ -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>())
|
||||
}
|
||||
} else {
|
||||
let mut_begin: *mut T = mem::transmute(self.begin());
|
||||
intrinsics::set_memory(mut_begin, 0, self.len())
|
||||
|
@ -326,9 +323,6 @@ 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>())
|
||||
|
@ -336,7 +330,6 @@ impl<'a, T: 'static> Drop for SmallVecMoveIterator<'a,T> {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Concrete implementations
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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> {
|
||||
|
@ -444,16 +460,12 @@ 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>())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
|
@ -527,4 +539,3 @@ pub mod tests {
|
|||
].as_slice());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue