Skip to content

Commit bf1e065

Browse files
committed
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]
1 parent aa67254 commit bf1e065

File tree

1 file changed

+0
-33
lines changed

1 file changed

+0
-33
lines changed

src/libcore/should_not_exist.rs

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -151,39 +151,6 @@ impl<A: Clone> Clone for ~[A] {
151151
}
152152
}
153153

154-
impl<A> FromIterator<A> for ~[A] {
155-
fn from_iter<T: Iterator<A>>(mut iterator: T) -> ~[A] {
156-
let (lower, _) = iterator.size_hint();
157-
let cap = if lower == 0 {16} else {lower};
158-
let mut cap = cap.checked_mul(&mem::size_of::<A>()).unwrap();
159-
let mut len = 0;
160-
161-
unsafe {
162-
let mut ptr = alloc(cap) as *mut Vec<A>;
163-
let mut ret = cast::transmute(ptr);
164-
for elt in iterator {
165-
if len * mem::size_of::<A>() >= cap {
166-
cap = cap.checked_mul(&2).unwrap();
167-
let ptr2 = alloc(cap) as *mut Vec<A>;
168-
ptr::copy_nonoverlapping_memory(&mut (*ptr2).data,
169-
&(*ptr).data,
170-
len);
171-
free(ptr as *u8);
172-
cast::forget(ret);
173-
ret = cast::transmute(ptr2);
174-
ptr = ptr2;
175-
}
176-
177-
let base = &mut (*ptr).data as *mut A;
178-
intrinsics::move_val_init(&mut *base.offset(len as int), elt);
179-
len += 1;
180-
(*ptr).fill = len * mem::nonzero_size_of::<A>();
181-
}
182-
ret
183-
}
184-
}
185-
}
186-
187154
#[cfg(not(test))]
188155
impl<'a,T:Clone, V: Vector<T>> Add<V, ~[T]> for &'a [T] {
189156
#[inline]

0 commit comments

Comments
 (0)