Remove FromIterator impl for ~[T]

As part of the shift from ~[T] to Vec<T>, recently ~[T] was made
non-growable. However, the FromIterator implementation for ~[T] was left
intact (albeit implemented inefficiently), which basically provided a
loophole to grow a ~[T] despite its non-growable nature. This is a
problem, both for performance reasons and because it encourages APIs to
continue returning ~[T] when they should return Vec<T>. Removing
FromIterator forces these APIs to adopt the correct type.

Furthermore, during today's weekly meeting it was decided that we should
remove all instances of ~[T] from the standard libraries in favor of
Vec<T>. Removing the FromIterator impl makes sense to do as a result.

This commit only includes the removal of the FromIterator impl. The
subsequent commits involve handling all of the breakage that results,
including changing APIs to use Vec<T> instead of ~[T]. The precise API
changes are documented in the subsequent commit messages, but each
commit is not individually marked as a breaking change.

Finally, a new trait FromVec is introduced that provides a mechanism to
convert Vec<T> back into ~[T] if truly necessary. It is a bit awkward to
use by design, and is anticipated that it will be more useful in a
post-DST world to convert to an arbitrary Foo<[T]> smart pointer.

[breaking-change]
This commit is contained in:
Kevin Ballard 2014-05-03 16:11:12 -07:00
parent aa6725407a
commit bf1e065371

View File

@ -151,39 +151,6 @@ impl<A: Clone> Clone for ~[A] {
}
}
impl<A> FromIterator<A> for ~[A] {
fn from_iter<T: Iterator<A>>(mut iterator: T) -> ~[A] {
let (lower, _) = iterator.size_hint();
let cap = if lower == 0 {16} else {lower};
let mut cap = cap.checked_mul(&mem::size_of::<A>()).unwrap();
let mut len = 0;
unsafe {
let mut ptr = alloc(cap) as *mut Vec<A>;
let mut ret = cast::transmute(ptr);
for elt in iterator {
if len * mem::size_of::<A>() >= cap {
cap = cap.checked_mul(&2).unwrap();
let ptr2 = alloc(cap) as *mut Vec<A>;
ptr::copy_nonoverlapping_memory(&mut (*ptr2).data,
&(*ptr).data,
len);
free(ptr as *u8);
cast::forget(ret);
ret = cast::transmute(ptr2);
ptr = ptr2;
}
let base = &mut (*ptr).data as *mut A;
intrinsics::move_val_init(&mut *base.offset(len as int), elt);
len += 1;
(*ptr).fill = len * mem::nonzero_size_of::<A>();
}
ret
}
}
}
#[cfg(not(test))]
impl<'a,T:Clone, V: Vector<T>> Add<V, ~[T]> for &'a [T] {
#[inline]