Skip to content

Add container::{NewContainer, Seq, MutableSeq} and other cleanup #10989

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
wants to merge 8 commits into from
Closed
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
5 changes: 4 additions & 1 deletion src/etc/vim/syntax/rust.vim
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ syn keyword rustTrait Char
syn keyword rustTrait Clone DeepClone
syn keyword rustTrait Eq ApproxEq Ord TotalEq TotalOrd Ordering Equiv
syn keyword rustEnumVariant Less Equal Greater
syn keyword rustTrait Container Mutable Map MutableMap Set MutableSet
syn keyword rustTrait Container NewContainer Mutable
syn keyword rustTrait Seq MutableSeq
syn keyword rustTrait Map MutableMap
syn keyword rustTrait Set MutableSet
syn keyword rustTrait Default
syn keyword rustTrait Hash
syn keyword rustTrait FromStr
Expand Down
5 changes: 5 additions & 0 deletions src/libextra/dlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ impl<T> Container for DList<T> {
}
}

impl<T> NewContainer for DList<T> {
#[inline]
fn with_capacity(_capacity: uint) -> DList<T> { DList::new() }
}

impl<T> Mutable for DList<T> {
/// Remove all elements from the DList
///
Expand Down
3 changes: 2 additions & 1 deletion src/libextra/flate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ pub fn inflate_bytes_zlib(bytes: &[u8]) -> ~[u8] {
#[cfg(test)]
mod tests {
use super::*;
use container::MutableSeq;
use std::rand;
use std::rand::Rng;

Expand All @@ -112,7 +113,7 @@ mod tests {
20.times(|| {
let mut input = ~[];
2000.times(|| {
input.push_all(r.choose(words));
input.push_all(r.choose(words).move_iter());
});
debug!("de/inflate of {} bytes of random word-sequences",
input.len());
Expand Down
15 changes: 14 additions & 1 deletion src/libextra/priority_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ impl<T:Ord> Container for PriorityQueue<T> {
fn len(&self) -> uint { self.data.len() }
}

impl<T:Ord> NewContainer for PriorityQueue<T> {
/// Create an empty Priority Queue with capacity `capacity`
#[inline]
fn with_capacity(capacity: uint) -> PriorityQueue<T> {
PriorityQueue::with_capacity(capacity)
}
}

impl<T:Ord> Mutable for PriorityQueue<T> {
/// Drop all items from the queue
fn clear(&mut self) { self.data.truncate(0) }
Expand Down Expand Up @@ -112,7 +120,12 @@ impl<T:Ord> PriorityQueue<T> {
}

/// Create an empty PriorityQueue
pub fn new() -> PriorityQueue<T> { PriorityQueue{data: ~[],} }
pub fn new() -> PriorityQueue<T> { PriorityQueue{data: ~[]} }

/// Create an empty PriorityQueue
pub fn with_capacity(capacity: uint) -> PriorityQueue<T> {
PriorityQueue{data: vec::with_capacity(capacity)}
}

/// Create a PriorityQueue from a vector (heapify)
pub fn from_vec(xs: ~[T]) -> PriorityQueue<T> {
Expand Down
6 changes: 6 additions & 0 deletions src/libextra/ringbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ impl<T> Container for RingBuf<T> {
fn len(&self) -> uint { self.nelts }
}

impl<T> NewContainer for RingBuf<T> {
fn new() -> RingBuf<T> { RingBuf::new() }

fn with_capacity(capacity: uint) -> RingBuf<T> { RingBuf::with_capacity(capacity) }
}

impl<T> Mutable for RingBuf<T> {
/// Clear the RingBuf, removing all values.
fn clear(&mut self) {
Expand Down
12 changes: 12 additions & 0 deletions src/libextra/smallintmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ impl<V> Container for SmallIntMap<V> {
}
}

impl<V> NewContainer for SmallIntMap<V> {
/// Create an empty SmallIntMap with capacity `capacity`
fn with_capacity(capacity: uint) -> SmallIntMap<V> {
SmallIntMap::with_capacity(capacity)
}
}

impl<V> Mutable for SmallIntMap<V> {
/// Clear the map, removing all key-value pairs.
fn clear(&mut self) { self.v.clear() }
Expand Down Expand Up @@ -113,6 +120,11 @@ impl<V> SmallIntMap<V> {
/// Create an empty SmallIntMap
pub fn new() -> SmallIntMap<V> { SmallIntMap{v: ~[]} }

/// Create an empty SmallIntMap with capacity `capacity`
pub fn with_capacity(capacity: uint) -> SmallIntMap<V> {
SmallIntMap{v: vec::with_capacity(capacity)}
}

pub fn get<'a>(&'a self, key: &uint) -> &'a V {
self.find(key).expect("key not present")
}
Expand Down
4 changes: 2 additions & 2 deletions src/libextra/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ pub fn merge_sort<T:Clone>(v: &[T], le: Le<T>) -> ~[T] {
b_ix += 1;
}
}
rs.push_all(a.slice(a_ix, a_len));
rs.push_all(b.slice(b_ix, b_len));
rs.push_all(a.slice(a_ix, a_len).clone_iter());
rs.push_all(b.slice(b_ix, b_len).clone_iter());
rs
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/libextra/terminfo/parm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ pub fn expand(cap: &[u8], params: &[Param], vars: &mut Variables)
let flags = Flags::new();
let res = format(stack.pop(), FormatOp::from_char(cur), flags);
if res.is_err() { return res }
output.push_all(res.unwrap())
output.push_all(res.unwrap().move_iter())
} else { return Err(~"stack is empty") },
':'|'#'|' '|'.'|'0'..'9' => {
let mut flags = Flags::new();
Expand Down Expand Up @@ -343,7 +343,7 @@ pub fn expand(cap: &[u8], params: &[Param], vars: &mut Variables)
(_,'d')|(_,'o')|(_,'x')|(_,'X')|(_,'s') => if stack.len() > 0 {
let res = format(stack.pop(), FormatOp::from_char(cur), *flags);
if res.is_err() { return res }
output.push_all(res.unwrap());
output.push_all(res.unwrap().move_iter());
old_state = state; // will cause state to go to Nothing
} else { return Err(~"stack is empty") },
(FormatStateFlags,'#') => {
Expand Down
2 changes: 1 addition & 1 deletion src/libextra/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ pub fn strftime(format: &str, tm: &Tm) -> ~str {
'%' => {
rdr.read(b);
let s = parse_type(b[0] as char, tm);
buf.push_all(s.as_bytes());
buf.push_all(s.into_bytes().move_iter());
}
ch => buf.push(ch as u8)
}
Expand Down
8 changes: 8 additions & 0 deletions src/libextra/treemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ impl<K: TotalOrd, V> Container for TreeMap<K, V> {
fn is_empty(&self) -> bool { self.root.is_none() }
}

impl<K: TotalOrd, V> NewContainer for TreeMap<K, V> {
fn with_capacity(_capacity: uint) -> TreeMap<K, V> { TreeMap::new() }
}

impl<K: TotalOrd, V> Mutable for TreeMap<K, V> {
/// Clear the map, removing all key-value pairs.
fn clear(&mut self) {
Expand Down Expand Up @@ -441,6 +445,10 @@ impl<T: TotalOrd> Container for TreeSet<T> {
fn is_empty(&self) -> bool { self.map.is_empty() }
}

impl<T: TotalOrd> NewContainer for TreeSet<T> {
fn with_capacity(_capacity: uint) -> TreeSet<T> { TreeSet::new() }
}

impl<T: TotalOrd> Mutable for TreeSet<T> {
/// Clear the set, removing all values.
#[inline]
Expand Down
16 changes: 8 additions & 8 deletions src/librustc/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -998,9 +998,9 @@ fn link_args(sess: Session,
let mut args = ~[stage];

// FIXME (#9639): This needs to handle non-utf8 paths
args.push_all([
~"-o", out_filename.as_str().unwrap().to_owned(),
obj_filename.as_str().unwrap().to_owned()]);
args.push(~"-o");
args.push(out_filename.as_str().unwrap().to_owned());
args.push(obj_filename.as_str().unwrap().to_owned());

// When linking a dynamic library, we put the metadata into a section of the
// executable. This metadata is in a separate object file from the main
Expand Down Expand Up @@ -1046,9 +1046,9 @@ fn link_args(sess: Session,
}

if sess.targ_cfg.os == abi::OsFreebsd {
args.push_all([~"-L/usr/local/lib",
~"-L/usr/local/lib/gcc46",
~"-L/usr/local/lib/gcc44"]);
args.push(~"-L/usr/local/lib");
args.push(~"-L/usr/local/lib/gcc46");
args.push(~"-L/usr/local/lib/gcc44");
}

// Stack growth requires statically linking a __morestack function
Expand All @@ -1057,11 +1057,11 @@ fn link_args(sess: Session,
// FIXME (#2397): At some point we want to rpath our guesses as to
// where extern libraries might live, based on the
// addl_lib_search_paths
args.push_all(rpath::get_rpath_flags(sess, out_filename));
args.push_all(rpath::get_rpath_flags(sess, out_filename).move_iter());

// Finally add all the linker arguments provided on the command line along
// with any #[link_args] attributes found inside the crate
args.push_all(sess.opts.linker_args);
args.push_all(sess.opts.linker_args.clone_iter());
for arg in cstore::get_used_link_args(sess.cstore).iter() {
args.push(arg.clone());
}
Expand Down
12 changes: 6 additions & 6 deletions src/librustc/back/rpath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ pub fn get_rpath_flags(sess: session::Session, out_filename: &Path) -> ~[~str] {
let mut flags = ~[];

if sess.targ_cfg.os == abi::OsFreebsd {
flags.push_all([~"-Wl,-rpath,/usr/local/lib/gcc46",
~"-Wl,-rpath,/usr/local/lib/gcc44",
~"-Wl,-z,origin"]);
flags.push(~"-Wl,-rpath,/usr/local/lib/gcc46");
flags.push(~"-Wl,-rpath,/usr/local/lib/gcc44");
flags.push(~"-Wl,-z,origin");
}

debug!("preparing the RPATH!");
Expand All @@ -49,7 +49,7 @@ pub fn get_rpath_flags(sess: session::Session, out_filename: &Path) -> ~[~str] {

let rpaths = get_rpaths(os, sysroot, output, libs,
sess.opts.target_triple);
flags.push_all(rpaths_to_flags(rpaths));
flags.push_all(rpaths_to_flags(rpaths).move_iter());
flags
}

Expand Down Expand Up @@ -105,8 +105,8 @@ fn get_rpaths(os: abi::Os,
log_rpaths("fallback", fallback_rpaths);

let mut rpaths = rel_rpaths;
rpaths.push_all(abs_rpaths);
rpaths.push_all(fallback_rpaths);
rpaths.push_all(abs_rpaths.move_iter());
rpaths.push_all(fallback_rpaths.move_iter());

// Remove duplicates
let rpaths = minimize_rpaths(rpaths);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/front/std_inject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl fold::ast_fold for StandardLibraryInjector {
});
}

vis.push_all(crate.module.view_items);
vis.push_all(crate.module.view_items.clone_iter());
let mut new_module = ast::_mod {
view_items: vis,
..crate.module.clone()
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/trans/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ pub fn trans_args(cx: @mut Block,
}
}
ArgVals(vs) => {
llargs.push_all(vs);
llargs.push_all(vs.clone_iter());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/trans/type_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub fn type_of_rust_fn(cx: &mut CrateContext,
atys.push(Type::opaque_box(cx).ptr_to());

// ... then explicit args.
atys.push_all(type_of_explicit_args(cx, inputs));
atys.push_all(type_of_explicit_args(cx, inputs).move_iter());

// Use the output as the actual return value if it's immediate.
if !use_out_pointer && !ty::type_is_voidish(cx.tcx, output) {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -957,8 +957,8 @@ pub fn compare_impl_method(tcx: ty::ctxt,
}

// Add in the normal arguments.
trait_fn_args.push_all(trait_m.fty.sig.inputs);
impl_fn_args.push_all(impl_m.fty.sig.inputs);
trait_fn_args.push_all(trait_m.fty.sig.inputs.clone_iter());
impl_fn_args.push_all(impl_m.fty.sig.inputs.clone_iter());

// Create a bare fn type for trait/impl that includes self argument
let trait_fty =
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ pub fn ensure_trait_methods(ccx: &CrateCtxt,
let mut new_type_param_defs = ~[];
let substd_type_param_defs =
trait_ty_generics.type_param_defs.subst(tcx, &substs);
new_type_param_defs.push_all(*substd_type_param_defs);
new_type_param_defs.push_all(substd_type_param_defs.clone_iter());

// add in the "self" type parameter
let self_trait_def = get_trait_def(ccx, local_def(trait_id));
Expand All @@ -338,7 +338,7 @@ pub fn ensure_trait_methods(ccx: &CrateCtxt,

// add in the type parameters from the method
let substd_type_param_defs = m.generics.type_param_defs.subst(tcx, &substs);
new_type_param_defs.push_all(*substd_type_param_defs);
new_type_param_defs.push_all(substd_type_param_defs.clone_iter());

debug!("static method {} type_param_defs={} ty={}, substs={}",
m.def_id.repr(tcx),
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ impl<'a> SourceCollector<'a> {
// read everything
loop {
match r.read(buf) {
Some(n) => contents.push_all(buf.slice_to(n)),
Some(n) => contents.push_all(buf.slice_to(n).clone_iter()),
None => break
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ pub fn unindent(s: &str) -> ~str {
match lines {
[head, .. tail] => {
let mut unindented = ~[ head.trim() ];
unindented.push_all(tail.map(|&line| {
unindented.push_all(tail.iter().map(|&line| {
if line.is_whitespace() {
line
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/librustpkg/path_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ pub fn versionize(p: &Path, v: &Version) -> Path {
let mut q = q.to_owned();
q.push('-' as u8);
let vs = v.to_str();
q.push_all(vs.as_bytes());
q.push_all(vs.into_bytes().move_iter());
p.with_filename(q)
}

Expand Down
21 changes: 14 additions & 7 deletions src/libstd/at_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ pub fn capacity<T>(v: @[T]) -> uint {
}
}

/// Creates an immutable managed vector with a capacity of `capacity`
pub fn with_capacity<T>(capacity: uint) -> @[T] {
let mut av = @[];
unsafe {
raw::reserve(&mut av, capacity);
}
av
}

/**
* Builds a vector by calling a provided function with an argument
* function that pushes an element to the back of a vector.
Expand All @@ -44,8 +53,7 @@ pub fn capacity<T>(v: @[T]) -> uint {
*/
#[inline]
pub fn build<A>(size: Option<uint>, builder: |push: |v: A||) -> @[A] {
let mut vec = @[];
unsafe { raw::reserve(&mut vec, size.unwrap_or(4)); }
let mut vec = with_capacity(size.unwrap_or(4));
builder(|x| unsafe { raw::push(&mut vec, x) });
vec
}
Expand Down Expand Up @@ -110,14 +118,13 @@ pub fn from_elem<T:Clone>(n_elts: uint, t: T) -> @[T] {
* elements from an owned vector.
*/
pub fn to_managed_move<T>(v: ~[T]) -> @[T] {
let mut av = @[];
unsafe {
raw::reserve(&mut av, v.len());
for x in v.move_iter() {
let mut av = with_capacity(v.len());
for x in v.move_iter() {
unsafe {
raw::push(&mut av, x);
}
av
}
av
}

/**
Expand Down
Loading