Skip to content

Remove FromIterator impl for ~[T], and the ensuing fallout #13963

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
bf1e065
Remove FromIterator impl for ~[T]
lilyball May 3, 2014
4af8431
Rewrite &[T].to_owned() to allocate directly
lilyball May 3, 2014
21dae8e
More fallout from removing FromIterator on ~[T]
lilyball May 3, 2014
3296bd7
Rename slice::unzip() to vec::unzip()
lilyball May 4, 2014
189dc5f
Move slice::raw::from_buf_raw() to vec::raw::from_buf()
lilyball May 4, 2014
8e42fde
More fallout from removing FromIterator on ~[T]
lilyball May 4, 2014
44e8021
Even more fallout, this time in std::str
lilyball May 4, 2014
bbc35ea
Handle fallout in std::strbuf
lilyball May 4, 2014
11613fc
Handle fallout in std::ascii and std::strconv
lilyball May 4, 2014
001a874
Handle fallout in iter, option, result, and sync::arc
lilyball May 4, 2014
f340fb9
Handle fallout in os
lilyball May 4, 2014
cc42b61
Handle fallout in io::net::addrinfo, io::process, and rt::rtio
lilyball May 4, 2014
2a0dac6
Handle fallout for vector addition
lilyball May 4, 2014
cd3f31d
Handle fallout in libserialize
lilyball May 4, 2014
a99eff3
Handle fallout in librustc
lilyball May 4, 2014
1d57da7
Handle fallout in libnative
lilyball May 4, 2014
fac0d4e
Clean up unused imports
lilyball May 4, 2014
fa82ef2
Handle fallout in librustuv
lilyball May 4, 2014
752048a
Handle more fallout
lilyball May 4, 2014
eab6bb2
Handle fallout in documentation
lilyball May 5, 2014
dbbb847
Handle fallout in bench tests
lilyball May 5, 2014
300d865
Add a mechanism to convert from Vec<T> to ~[T]
lilyball May 7, 2014
9fb8741
Handle breakage after libcore split
lilyball May 8, 2014
f582150
Restore Decodable impl for ~[T]
lilyball May 8, 2014
dc30c48
Move partition/partitioned/concat/connect tests back into slice
lilyball May 8, 2014
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/compiletest/runtest.rs
Original file line number Diff line number Diff line change
@@ -997,7 +997,7 @@ fn make_lib_name(config: &config, auxfile: &Path, testfile: &Path) -> Path {
fn make_exe_name(config: &config, testfile: &Path) -> Path {
let mut f = output_base_name(config, testfile);
if !os::consts::EXE_SUFFIX.is_empty() {
match f.filename().map(|s| s + os::consts::EXE_SUFFIX.as_bytes()) {
match f.filename().map(|s| Vec::from_slice(s).append(os::consts::EXE_SUFFIX.as_bytes())) {
Some(v) => f.set_filename(v),
None => ()
}
@@ -1091,7 +1091,7 @@ fn make_out_name(config: &config, testfile: &Path, extension: &str) -> Path {

fn aux_output_dir_name(config: &config, testfile: &Path) -> Path {
let mut f = output_base_name(config, testfile);
match f.filename().map(|s| s + bytes!(".libaux")) {
match f.filename().map(|s| Vec::from_slice(s).append(bytes!(".libaux"))) {
Some(v) => f.set_filename(v),
None => ()
}
@@ -1273,7 +1273,7 @@ fn append_suffix_to_stem(p: &Path, suffix: &str) -> Path {
(*p).clone()
} else {
let stem = p.filestem().unwrap();
p.with_filename(stem + bytes!("-") + suffix.as_bytes())
p.with_filename(Vec::from_slice(stem).append(bytes!("-")).append(suffix.as_bytes()))
}
}

16 changes: 8 additions & 8 deletions src/doc/guide-container.md
Original file line number Diff line number Diff line change
@@ -266,8 +266,8 @@ Iterators offer generic conversion to containers with the `collect` adaptor:

~~~
let xs = [0, 1, 1, 2, 3, 5, 8];
let ys = xs.iter().rev().skip(1).map(|&x| x * 2).collect::<~[int]>();
assert_eq!(ys, ~[10, 6, 4, 2, 2, 0]);
let ys = xs.iter().rev().skip(1).map(|&x| x * 2).collect::<Vec<int>>();
assert_eq!(ys, vec![10, 6, 4, 2, 2, 0]);
~~~

The method requires a type hint for the container type, if the surrounding code
@@ -278,14 +278,14 @@ implementing the `FromIterator` trait. For example, the implementation for
vectors is as follows:

~~~ {.ignore}
impl<A> FromIterator<A> for ~[A] {
pub fn from_iter<T: Iterator<A>>(iterator: &mut T) -> ~[A] {
impl<T> FromIterator<T> for Vec<T> {
fn from_iter<I:Iterator<A>>(mut iterator: I) -> Vec<T> {
let (lower, _) = iterator.size_hint();
let mut xs = with_capacity(lower);
for x in iterator {
xs.push(x);
let mut vector = Vec::with_capacity(lower);
for element in iterator {
vector.push(element);
}
xs
vector
}
}
~~~
10 changes: 5 additions & 5 deletions src/doc/rust.md
Original file line number Diff line number Diff line change
@@ -3598,18 +3598,18 @@ and the cast expression in `main`.
Within the body of an item that has type parameter declarations, the names of its type parameters are types:

~~~~
fn map<A: Clone, B: Clone>(f: |A| -> B, xs: &[A]) -> ~[B] {
fn map<A: Clone, B: Clone>(f: |A| -> B, xs: &[A]) -> Vec<B> {
if xs.len() == 0 {
return ~[];
return vec![];
}
let first: B = f(xs[0].clone());
let rest: ~[B] = map(f, xs.slice(1, xs.len()));
return ~[first] + rest;
let rest: Vec<B> = map(f, xs.slice(1, xs.len()));
return vec![first].append(rest.as_slice());
}
~~~~

Here, `first` has type `B`, referring to `map`'s `B` type parameter;
and `rest` has type `~[B]`, a vector type with element type `B`.
and `rest` has type `Vec<B>`, a vector type with element type `B`.

### Self types

11 changes: 6 additions & 5 deletions src/doc/tutorial.md
Original file line number Diff line number Diff line change
@@ -1588,8 +1588,8 @@ let mut numbers = vec![1, 2, 3];
numbers.push(4);
numbers.push(5);

// The type of a unique vector is written as `~[int]`
let more_numbers: ~[int] = numbers.move_iter().collect();
// The type of a unique vector is written as `Vec<int>`
let more_numbers: Vec<int> = numbers.move_iter().map(|i| i+1).collect();

// The original `numbers` value can no longer be used, due to move semantics.

@@ -1633,7 +1633,7 @@ view[0] = 5;
let ys: &mut [int] = &mut [1, 2, 3];
~~~

Square brackets denote indexing into a vector:
Square brackets denote indexing into a slice or fixed-size vector:

~~~~
# enum Crayon { Almond, AntiqueBrass, Apricot,
@@ -1647,7 +1647,7 @@ match crayons[0] {
}
~~~~

A vector can be destructured using pattern matching:
A slice or fixed-size vector can be destructured using pattern matching:

~~~~
let numbers: &[int] = &[1, 2, 3];
@@ -1660,9 +1660,10 @@ let score = match numbers {
~~~~

Both vectors and strings support a number of useful [methods](#methods),
defined in [`std::vec`] and [`std::str`].
defined in [`std::vec`], [`std::slice`], and [`std::str`].

[`std::vec`]: std/vec/index.html
[`std::slice`]: std/slice/index.html
[`std::str`]: std/str/index.html

# Ownership escape hatches
73 changes: 37 additions & 36 deletions src/libcore/iter.rs
Original file line number Diff line number Diff line change
@@ -455,8 +455,8 @@ pub trait Iterator<A> {
///
/// ```rust
/// let a = [1, 2, 3, 4, 5];
/// let b: ~[int] = a.iter().map(|&x| x).collect();
/// assert!(a == b);
/// let b: Vec<int> = a.iter().map(|&x| x).collect();
/// assert!(a.as_slice() == b.as_slice());
/// ```
#[inline]
fn collect<B: FromIterator<A>>(&mut self) -> B {
@@ -2340,8 +2340,8 @@ mod tests {
#[test]
fn test_counter_from_iter() {
let it = count(0, 5).take(10);
let xs: ~[int] = FromIterator::from_iter(it);
assert_eq!(xs, box [0, 5, 10, 15, 20, 25, 30, 35, 40, 45]);
let xs: Vec<int> = FromIterator::from_iter(it);
assert_eq!(xs, vec![0, 5, 10, 15, 20, 25, 30, 35, 40, 45]);
}

#[test]
@@ -2371,7 +2371,7 @@ mod tests {
fn test_filter_map() {
let mut it = count(0u, 1u).take(10)
.filter_map(|x| if x % 2 == 0 { Some(x*x) } else { None });
assert_eq!(it.collect::<~[uint]>(), box [0*0, 2*2, 4*4, 6*6, 8*8]);
assert_eq!(it.collect::<Vec<uint>>(), vec![0*0, 2*2, 4*4, 6*6, 8*8]);
}

#[test]
@@ -2493,7 +2493,7 @@ mod tests {
let ys = xs.iter()
.map(|&x| x)
.inspect(|_| n += 1)
.collect::<~[uint]>();
.collect::<Vec<uint>>();

assert_eq!(n, xs.len());
assert_eq!(xs.as_slice(), ys.as_slice());
@@ -2628,8 +2628,8 @@ mod tests {

#[test]
fn test_collect() {
let a = box [1, 2, 3, 4, 5];
let b: ~[int] = a.iter().map(|&x| x).collect();
let a = vec![1, 2, 3, 4, 5];
let b: Vec<int> = a.iter().map(|&x| x).collect();
assert_eq!(a, b);
}

@@ -2702,7 +2702,7 @@ mod tests {
let mut it = xs.iter();
it.next();
it.next();
assert_eq!(it.rev().map(|&x| x).collect::<~[int]>(), box [16, 14, 12, 10, 8, 6]);
assert_eq!(it.rev().map(|&x| x).collect::<Vec<int>>(), vec![16, 14, 12, 10, 8, 6]);
}

#[test]
@@ -2940,12 +2940,12 @@ mod tests {

#[test]
fn test_double_ended_range() {
assert_eq!(range(11i, 14).rev().collect::<~[int]>(), box [13i, 12, 11]);
assert_eq!(range(11i, 14).rev().collect::<Vec<int>>(), vec![13i, 12, 11]);
for _ in range(10i, 0).rev() {
fail!("unreachable");
}

assert_eq!(range(11u, 14).rev().collect::<~[uint]>(), box [13u, 12, 11]);
assert_eq!(range(11u, 14).rev().collect::<Vec<uint>>(), vec![13u, 12, 11]);
for _ in range(10u, 0).rev() {
fail!("unreachable");
}
@@ -2997,13 +2997,14 @@ mod tests {
}
}

assert_eq!(range(0i, 5).collect::<~[int]>(), box [0i, 1, 2, 3, 4]);
assert_eq!(range(-10i, -1).collect::<~[int]>(), box [-10, -9, -8, -7, -6, -5, -4, -3, -2]);
assert_eq!(range(0i, 5).rev().collect::<~[int]>(), box [4, 3, 2, 1, 0]);
assert_eq!(range(200, -5).collect::<~[int]>(), box []);
assert_eq!(range(200, -5).rev().collect::<~[int]>(), box []);
assert_eq!(range(200, 200).collect::<~[int]>(), box []);
assert_eq!(range(200, 200).rev().collect::<~[int]>(), box []);
assert_eq!(range(0i, 5).collect::<Vec<int>>(), vec![0i, 1, 2, 3, 4]);
assert_eq!(range(-10i, -1).collect::<Vec<int>>(),
vec![-10, -9, -8, -7, -6, -5, -4, -3, -2]);
assert_eq!(range(0i, 5).rev().collect::<Vec<int>>(), vec![4, 3, 2, 1, 0]);
assert_eq!(range(200, -5).collect::<Vec<int>>(), vec![]);
assert_eq!(range(200, -5).rev().collect::<Vec<int>>(), vec![]);
assert_eq!(range(200, 200).collect::<Vec<int>>(), vec![]);
assert_eq!(range(200, 200).rev().collect::<Vec<int>>(), vec![]);

assert_eq!(range(0i, 100).size_hint(), (100, Some(100)));
// this test is only meaningful when sizeof uint < sizeof u64
@@ -3014,32 +3015,32 @@ mod tests {

#[test]
fn test_range_inclusive() {
assert_eq!(range_inclusive(0i, 5).collect::<~[int]>(), box [0i, 1, 2, 3, 4, 5]);
assert_eq!(range_inclusive(0i, 5).rev().collect::<~[int]>(), box [5i, 4, 3, 2, 1, 0]);
assert_eq!(range_inclusive(200, -5).collect::<~[int]>(), box []);
assert_eq!(range_inclusive(200, -5).rev().collect::<~[int]>(), box []);
assert_eq!(range_inclusive(200, 200).collect::<~[int]>(), box [200]);
assert_eq!(range_inclusive(200, 200).rev().collect::<~[int]>(), box [200]);
assert_eq!(range_inclusive(0i, 5).collect::<Vec<int>>(), vec![0i, 1, 2, 3, 4, 5]);
assert_eq!(range_inclusive(0i, 5).rev().collect::<Vec<int>>(), vec![5i, 4, 3, 2, 1, 0]);
assert_eq!(range_inclusive(200, -5).collect::<Vec<int>>(), vec![]);
assert_eq!(range_inclusive(200, -5).rev().collect::<Vec<int>>(), vec![]);
assert_eq!(range_inclusive(200, 200).collect::<Vec<int>>(), vec![200]);
assert_eq!(range_inclusive(200, 200).rev().collect::<Vec<int>>(), vec![200]);
}

#[test]
fn test_range_step() {
assert_eq!(range_step(0i, 20, 5).collect::<~[int]>(), box [0, 5, 10, 15]);
assert_eq!(range_step(20i, 0, -5).collect::<~[int]>(), box [20, 15, 10, 5]);
assert_eq!(range_step(20i, 0, -6).collect::<~[int]>(), box [20, 14, 8, 2]);
assert_eq!(range_step(200u8, 255, 50).collect::<~[u8]>(), box [200u8, 250]);
assert_eq!(range_step(200, -5, 1).collect::<~[int]>(), box []);
assert_eq!(range_step(200, 200, 1).collect::<~[int]>(), box []);
assert_eq!(range_step(0i, 20, 5).collect::<Vec<int>>(), vec![0, 5, 10, 15]);
assert_eq!(range_step(20i, 0, -5).collect::<Vec<int>>(), vec![20, 15, 10, 5]);
assert_eq!(range_step(20i, 0, -6).collect::<Vec<int>>(), vec![20, 14, 8, 2]);
assert_eq!(range_step(200u8, 255, 50).collect::<Vec<u8>>(), vec![200u8, 250]);
assert_eq!(range_step(200, -5, 1).collect::<Vec<int>>(), vec![]);
assert_eq!(range_step(200, 200, 1).collect::<Vec<int>>(), vec![]);
}

#[test]
fn test_range_step_inclusive() {
assert_eq!(range_step_inclusive(0i, 20, 5).collect::<~[int]>(), box [0, 5, 10, 15, 20]);
assert_eq!(range_step_inclusive(20i, 0, -5).collect::<~[int]>(), box [20, 15, 10, 5, 0]);
assert_eq!(range_step_inclusive(20i, 0, -6).collect::<~[int]>(), box [20, 14, 8, 2]);
assert_eq!(range_step_inclusive(200u8, 255, 50).collect::<~[u8]>(), box [200u8, 250]);
assert_eq!(range_step_inclusive(200, -5, 1).collect::<~[int]>(), box []);
assert_eq!(range_step_inclusive(200, 200, 1).collect::<~[int]>(), box [200]);
assert_eq!(range_step_inclusive(0i, 20, 5).collect::<Vec<int>>(), vec![0, 5, 10, 15, 20]);
assert_eq!(range_step_inclusive(20i, 0, -5).collect::<Vec<int>>(), vec![20, 15, 10, 5, 0]);
assert_eq!(range_step_inclusive(20i, 0, -6).collect::<Vec<int>>(), vec![20, 14, 8, 2]);
assert_eq!(range_step_inclusive(200u8, 255, 50).collect::<Vec<u8>>(), vec![200u8, 250]);
assert_eq!(range_step_inclusive(200, -5, 1).collect::<Vec<int>>(), vec![]);
assert_eq!(range_step_inclusive(200, 200, 1).collect::<Vec<int>>(), vec![200]);
}

#[test]
2 changes: 2 additions & 0 deletions src/libcore/lib.rs
Original file line number Diff line number Diff line change
@@ -111,4 +111,6 @@ mod std {
#[cfg(test)] pub use realstd::rt; // needed for fail!()
#[cfg(test)] pub use realstd::option; // needed for assert!()
#[cfg(test)] pub use realstd::os; // needed for tests
#[cfg(test)] pub use realstd::slice; // needed for tests
#[cfg(test)] pub use realstd::vec; // needed for vec![]
}
18 changes: 9 additions & 9 deletions src/libcore/option.rs
Original file line number Diff line number Diff line change
@@ -844,22 +844,22 @@ mod tests {

#[test]
fn test_collect() {
let v: Option<~[int]> = collect(range(0, 0)
.map(|_| Some(0)));
assert_eq!(v, Some(box []));
let v: Option<Vec<int>> = collect(range(0, 0)
.map(|_| Some(0)));
assert_eq!(v, Some(vec![]));

let v: Option<~[int]> = collect(range(0, 3)
.map(|x| Some(x)));
assert_eq!(v, Some(box [0, 1, 2]));
let v: Option<Vec<int>> = collect(range(0, 3)
.map(|x| Some(x)));
assert_eq!(v, Some(vec![0, 1, 2]));

let v: Option<~[int]> = collect(range(0, 3)
.map(|x| if x > 1 { None } else { Some(x) }));
let v: Option<Vec<int>> = collect(range(0, 3)
.map(|x| if x > 1 { None } else { Some(x) }));
assert_eq!(v, None);

// test that it does not take more elements than it needs
let mut functions = [|| Some(()), || None, || fail!()];

let v: Option<~[()]> = collect(functions.mut_iter().map(|f| (*f)()));
let v: Option<Vec<()>> = collect(functions.mut_iter().map(|f| (*f)()));

assert_eq!(v, None);
}
14 changes: 7 additions & 7 deletions src/libcore/result.rs
Original file line number Diff line number Diff line change
@@ -653,20 +653,20 @@ mod tests {

#[test]
fn test_collect() {
let v: Result<~[int], ()> = collect(range(0, 0).map(|_| Ok::<int, ()>(0)));
assert_eq!(v, Ok(box []));
let v: Result<Vec<int>, ()> = collect(range(0, 0).map(|_| Ok::<int, ()>(0)));
assert_eq!(v, Ok(vec![]));

let v: Result<~[int], ()> = collect(range(0, 3).map(|x| Ok::<int, ()>(x)));
assert_eq!(v, Ok(box [0, 1, 2]));
let v: Result<Vec<int>, ()> = collect(range(0, 3).map(|x| Ok::<int, ()>(x)));
assert_eq!(v, Ok(vec![0, 1, 2]));

let v: Result<~[int], int> = collect(range(0, 3)
.map(|x| if x > 1 { Err(x) } else { Ok(x) }));
let v: Result<Vec<int>, int> = collect(range(0, 3)
.map(|x| if x > 1 { Err(x) } else { Ok(x) }));
assert_eq!(v, Err(2));

// test that it does not take more elements than it needs
let mut functions = [|| Ok(()), || Err(1), || fail!()];

let v: Result<~[()], int> = collect(functions.mut_iter().map(|f| (*f)()));
let v: Result<Vec<()>, int> = collect(functions.mut_iter().map(|f| (*f)()));
assert_eq!(v, Err(1));
}

Loading