Skip to content

Make ~[T] no longer a growable vector #13588

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

Merged
merged 2 commits into from
Apr 18, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 3 additions & 4 deletions src/compiletest/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ use std::os;
use std::str;
use std::strbuf::StrBuf;
use std::task;
use std::slice;
use test::MetricMap;

pub fn run(config: config, testfile: ~str) {
Expand Down Expand Up @@ -509,7 +508,7 @@ fn check_expected_errors(expected_errors: Vec<errors::ExpectedError> ,
proc_res: &ProcRes) {

// true if we found the error in question
let mut found_flags = slice::from_elem(
let mut found_flags = Vec::from_elem(
expected_errors.len(), false);

if proc_res.status.success() {
Expand Down Expand Up @@ -554,13 +553,13 @@ fn check_expected_errors(expected_errors: Vec<errors::ExpectedError> ,
for line in proc_res.stderr.lines() {
let mut was_expected = false;
for (i, ee) in expected_errors.iter().enumerate() {
if !found_flags[i] {
if !*found_flags.get(i) {
debug!("prefix={} ee.kind={} ee.msg={} line={}",
*prefixes.get(i), ee.kind, ee.msg, line);
if prefix_matches(line, *prefixes.get(i)) &&
line.contains(ee.kind) &&
line.contains(ee.msg) {
found_flags[i] = true;
*found_flags.get_mut(i) = true;
was_expected = true;
break;
}
Expand Down
26 changes: 10 additions & 16 deletions src/doc/guide-tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,9 @@ might look like the example below.

~~~
# use std::task::spawn;
# use std::slice;

// Create a vector of ports, one for each child task
let rxs = slice::from_fn(3, |init_val| {
let rxs = Vec::from_fn(3, |init_val| {
let (tx, rx) = channel();
spawn(proc() {
tx.send(some_expensive_computation(init_val));
Expand Down Expand Up @@ -304,7 +303,6 @@ be distributed on the available cores.

~~~
# extern crate sync;
# use std::slice;
fn partial_sum(start: uint) -> f64 {
let mut local_sum = 0f64;
for num in range(start*100000, (start+1)*100000) {
Expand All @@ -314,7 +312,7 @@ fn partial_sum(start: uint) -> f64 {
}

fn main() {
let mut futures = slice::from_fn(1000, |ind| sync::Future::spawn( proc() { partial_sum(ind) }));
let mut futures = Vec::from_fn(1000, |ind| sync::Future::spawn( proc() { partial_sum(ind) }));

let mut final_res = 0f64;
for ft in futures.mut_iter() {
Expand Down Expand Up @@ -342,25 +340,24 @@ a single large vector of floats. Each task needs the full vector to perform its
extern crate rand;
extern crate sync;

use std::slice;
use sync::Arc;

fn pnorm(nums: &~[f64], p: uint) -> f64 {
fn pnorm(nums: &[f64], p: uint) -> f64 {
nums.iter().fold(0.0, |a,b| a+(*b).powf(&(p as f64)) ).powf(&(1.0 / (p as f64)))
}

fn main() {
let numbers = slice::from_fn(1000000, |_| rand::random::<f64>());
let numbers = Vec::from_fn(1000000, |_| rand::random::<f64>());
let numbers_arc = Arc::new(numbers);

for num in range(1u, 10) {
let (tx, rx) = channel();
tx.send(numbers_arc.clone());

spawn(proc() {
let local_arc : Arc<~[f64]> = rx.recv();
let local_arc : Arc<Vec<f64>> = rx.recv();
let task_numbers = &*local_arc;
println!("{}-norm = {}", num, pnorm(task_numbers, num));
println!("{}-norm = {}", num, pnorm(task_numbers.as_slice(), num));
});
}
}
Expand All @@ -374,9 +371,8 @@ created by the line
# extern crate sync;
# extern crate rand;
# use sync::Arc;
# use std::slice;
# fn main() {
# let numbers = slice::from_fn(1000000, |_| rand::random::<f64>());
# let numbers = Vec::from_fn(1000000, |_| rand::random::<f64>());
let numbers_arc=Arc::new(numbers);
# }
~~~
Expand All @@ -387,9 +383,8 @@ and a clone of it is sent to each task
# extern crate sync;
# extern crate rand;
# use sync::Arc;
# use std::slice;
# fn main() {
# let numbers=slice::from_fn(1000000, |_| rand::random::<f64>());
# let numbers=Vec::from_fn(1000000, |_| rand::random::<f64>());
# let numbers_arc = Arc::new(numbers);
# let (tx, rx) = channel();
tx.send(numbers_arc.clone());
Expand All @@ -404,13 +399,12 @@ Each task recovers the underlying data by
# extern crate sync;
# extern crate rand;
# use sync::Arc;
# use std::slice;
# fn main() {
# let numbers=slice::from_fn(1000000, |_| rand::random::<f64>());
# let numbers=Vec::from_fn(1000000, |_| rand::random::<f64>());
# let numbers_arc=Arc::new(numbers);
# let (tx, rx) = channel();
# tx.send(numbers_arc.clone());
# let local_arc : Arc<~[f64]> = rx.recv();
# let local_arc : Arc<Vec<f64>> = rx.recv();
let task_numbers = &*local_arc;
# }
~~~
Expand Down
8 changes: 4 additions & 4 deletions src/doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -1582,12 +1582,12 @@ the elements are mutable if the vector is mutable.
use std::strbuf::StrBuf;

// A dynamically sized vector (unique vector)
let mut numbers = ~[1, 2, 3];
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;
let more_numbers: ~[int] = numbers.move_iter().collect();

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

Expand Down Expand Up @@ -1955,8 +1955,8 @@ vector consisting of the result of applying `function` to each element
of `vector`:

~~~~
fn map<T, U>(vector: &[T], function: |v: &T| -> U) -> ~[U] {
let mut accumulator = ~[];
fn map<T, U>(vector: &[T], function: |v: &T| -> U) -> Vec<U> {
let mut accumulator = Vec::new();
for element in vector.iter() {
accumulator.push(function(element));
}
Expand Down
4 changes: 2 additions & 2 deletions src/libnative/io/addrinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl GetAddrInfoRequest {
}

// Collect all the results we found
let mut addrs = ~[];
let mut addrs = Vec::new();
let mut rp = res;
while rp.is_not_null() {
unsafe {
Expand All @@ -80,7 +80,7 @@ impl GetAddrInfoRequest {

unsafe { freeaddrinfo(res); }

Ok(addrs)
Ok(addrs.move_iter().collect())
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/libnative/io/file_unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use libc::{c_int, c_void};
use libc;
use std::mem;
use std::rt::rtio;
use std::slice;

use io::{IoResult, retry, keep_going};

Expand Down Expand Up @@ -416,7 +415,7 @@ pub fn readlink(p: &CString) -> IoResult<Path> {
if len == -1 {
len = 1024; // FIXME: read PATH_MAX from C ffi?
}
let mut buf = slice::with_capacity::<u8>(len as uint);
let mut buf: Vec<u8> = Vec::with_capacity(len as uint);
match retry(|| unsafe {
libc::readlink(p, buf.as_ptr() as *mut libc::c_char,
len as libc::size_t) as libc::c_int
Expand Down
16 changes: 6 additions & 10 deletions src/libnative/io/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl Process {
return Err(super::unimpl());
}

fn get_io(io: p::StdioContainer, ret: &mut ~[Option<file::FileDesc>])
fn get_io(io: p::StdioContainer, ret: &mut Vec<Option<file::FileDesc>>)
-> (Option<os::Pipe>, c_int)
{
match io {
Expand All @@ -93,7 +93,7 @@ impl Process {
}
}

let mut ret_io = ~[];
let mut ret_io = Vec::new();
let (in_pipe, in_fd) = get_io(config.stdin, &mut ret_io);
let (out_pipe, out_fd) = get_io(config.stdout, &mut ret_io);
let (err_pipe, err_fd) = get_io(config.stderr, &mut ret_io);
Expand All @@ -117,7 +117,7 @@ impl Process {
exit_code: None,
exit_signal: None,
},
ret_io))
ret_io.move_iter().collect()))
}
Err(e) => Err(e)
}
Expand Down Expand Up @@ -641,12 +641,10 @@ fn spawn_process_os(config: p::ProcessConfig,

#[cfg(unix)]
fn with_argv<T>(prog: &str, args: &[~str], cb: proc(**libc::c_char) -> T) -> T {
use std::slice;

// We can't directly convert `str`s into `*char`s, as someone needs to hold
// a reference to the intermediary byte buffers. So first build an array to
// hold all the ~[u8] byte strings.
let mut tmps = slice::with_capacity(args.len() + 1);
let mut tmps = Vec::with_capacity(args.len() + 1);

tmps.push(prog.to_c_str());

Expand All @@ -667,14 +665,12 @@ fn with_argv<T>(prog: &str, args: &[~str], cb: proc(**libc::c_char) -> T) -> T {

#[cfg(unix)]
fn with_envp<T>(env: Option<~[(~str, ~str)]>, cb: proc(*c_void) -> T) -> T {
use std::slice;

// On posixy systems we can pass a char** for envp, which is a
// null-terminated array of "k=v\n" strings. Like `with_argv`, we have to
// have a temporary buffer to hold the intermediary `~[u8]` byte strings.
match env {
Some(env) => {
let mut tmps = slice::with_capacity(env.len());
let mut tmps = Vec::with_capacity(env.len());

for pair in env.iter() {
let kv = format!("{}={}", *pair.ref0(), *pair.ref1());
Expand All @@ -700,7 +696,7 @@ fn with_envp<T>(env: Option<~[(~str, ~str)]>, cb: |*mut c_void| -> T) -> T {
// \0 to terminate.
match env {
Some(env) => {
let mut blk = ~[];
let mut blk = Vec::new();

for pair in env.iter() {
let kv = format!("{}={}", *pair.ref0(), *pair.ref1());
Expand Down
2 changes: 0 additions & 2 deletions src/librustc/back/rpath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,6 @@ pub fn minimize_rpaths(rpaths: &[~str]) -> Vec<~str> {

#[cfg(unix, test)]
mod test {
use std::os;

use back::rpath::get_install_prefix_rpath;
use back::rpath::{minimize_rpaths, rpaths_to_flags, get_rpath_relative_to_output};
use syntax::abi;
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,13 @@ pub fn describe_codegen_flags() {
}

pub fn run_compiler(args: &[~str]) {
let mut args = args.to_owned();
let mut args = Vec::from_slice(args);
let binary = args.shift().unwrap();

if args.is_empty() { usage(binary); return; }

let matches =
&match getopts::getopts(args, d::optgroups().as_slice()) {
&match getopts::getopts(args.as_slice(), d::optgroups().as_slice()) {
Ok(m) => m,
Err(f) => {
d::early_error(f.to_err_msg());
Expand Down
7 changes: 3 additions & 4 deletions src/librustc/middle/dataflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@


use std::io;
use std::slice;
use std::strbuf::StrBuf;
use std::uint;
use syntax::ast;
Expand Down Expand Up @@ -308,13 +307,13 @@ impl<'a, O:DataFlowOperator+Clone+'static> DataFlowContext<'a, O> {
changed: true
};

let mut temp = slice::from_elem(self.words_per_id, 0u);
let mut temp = Vec::from_elem(self.words_per_id, 0u);
let mut loop_scopes = Vec::new();

while propcx.changed {
propcx.changed = false;
propcx.reset(temp);
propcx.walk_block(blk, temp, &mut loop_scopes);
propcx.reset(temp.as_mut_slice());
propcx.walk_block(blk, temp.as_mut_slice(), &mut loop_scopes);
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/librustc/middle/trans/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
* closure.
*/

use std::slice;

use back::abi;
use driver::session;
use lib::llvm::{ValueRef, NoAliasAttribute, StructRetAttribute, NoCaptureAttribute};
Expand Down Expand Up @@ -221,11 +219,12 @@ fn resolve_default_method_vtables(bcx: &Block,
Some(vtables) => {
let num_impl_type_parameters =
vtables.len() - num_method_vtables;
vtables.tailn(num_impl_type_parameters).to_owned()
Vec::from_slice(vtables.tailn(num_impl_type_parameters))
},
None => slice::from_elem(num_method_vtables, @Vec::new())
None => Vec::from_elem(num_method_vtables, @Vec::new())
};

let method_vtables = method_vtables.as_slice();
let param_vtables = @((*trait_vtables_fixed).clone().append(method_vtables));

let self_vtables = resolve_param_vtables_under_param_substs(
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/middle/trans/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,11 +593,11 @@ fn const_expr_unadjusted(cx: &CrateContext, e: &ast::Expr,
const_eval::const_uint(i) => i as uint,
_ => cx.sess().span_bug(count.span, "count must be integral const expression.")
};
let vs = slice::from_elem(n, const_expr(cx, elem, is_local).val0());
let vs = Vec::from_elem(n, const_expr(cx, elem, is_local).val0());
let v = if vs.iter().any(|vi| val_ty(*vi) != llunitty) {
C_struct(cx, vs, false)
C_struct(cx, vs.as_slice(), false)
} else {
C_array(llunitty, vs)
C_array(llunitty, vs.as_slice())
};
(v, true)
}
Expand Down
9 changes: 4 additions & 5 deletions src/librustc/middle/trans/debuginfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ use collections::HashMap;
use collections::HashSet;
use libc::{c_uint, c_ulonglong, c_longlong};
use std::ptr;
use std::slice;
use std::strbuf::StrBuf;
use std::sync::atomics;
use syntax::codemap::{Span, Pos};
Expand Down Expand Up @@ -776,7 +775,7 @@ pub fn create_function_debug_context(cx: &CrateContext,
return create_DIArray(DIB(cx), []);
}

let mut signature = slice::with_capacity(fn_decl.inputs.len() + 1);
let mut signature = Vec::with_capacity(fn_decl.inputs.len() + 1);

// Return type -- llvm::DIBuilder wants this at index 0
match fn_decl.output.node {
Expand Down Expand Up @@ -818,7 +817,7 @@ pub fn create_function_debug_context(cx: &CrateContext,
signature.push(type_metadata(cx, arg_type, codemap::DUMMY_SP));
}

return create_DIArray(DIB(cx), signature);
return create_DIArray(DIB(cx), signature.as_slice());
}

fn get_template_parameters(cx: &CrateContext,
Expand Down Expand Up @@ -961,15 +960,15 @@ fn compile_unit_metadata(cx: &CrateContext) {
// prepend "./" if necessary
let dotdot = bytes!("..");
let prefix = &[dotdot[0], ::std::path::SEP_BYTE];
let mut path_bytes = p.as_vec().to_owned();
let mut path_bytes = Vec::from_slice(p.as_vec());

if path_bytes.slice_to(2) != prefix &&
path_bytes.slice_to(2) != dotdot {
path_bytes.insert(0, prefix[0]);
path_bytes.insert(1, prefix[1]);
}

path_bytes.to_c_str()
path_bytes.as_slice().to_c_str()
}
_ => fallback_path(cx)
}
Expand Down
Loading