From d0512b1055eac15db86d83c994fb546cbfa62676 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Thu, 27 Jun 2013 19:48:50 +1000 Subject: [PATCH 01/10] Convert vec::[mut_]slice to methods, remove vec::const_slice. --- src/compiletest/runtest.rs | 3 +- src/libextra/crypto/sha2.rs | 79 +++++++------- src/libextra/ebml.rs | 3 +- src/libextra/net_tcp.rs | 4 +- src/libextra/num/bigint.rs | 17 ++- src/libextra/sort.rs | 24 ++--- src/librustc/back/rpath.rs | 2 +- src/librustc/metadata/decoder.rs | 4 +- src/librustc/metadata/tydecode.rs | 5 +- src/librustc/middle/dataflow.rs | 22 ++-- src/librustc/middle/kind.rs | 1 - src/librustc/middle/trans/_match.rs | 14 +-- src/librustc/middle/trans/common.rs | 7 +- src/librustc/middle/trans/meth.rs | 2 +- src/librusti/rusti.rs | 4 +- src/librustpkg/package_source.rs | 5 +- src/libstd/io.rs | 11 +- src/libstd/os.rs | 2 +- src/libstd/rt/io/extensions.rs | 2 +- src/libstd/rt/io/mem.rs | 2 +- src/libstd/vec.rs | 100 +++++++----------- src/libsyntax/diagnostic.rs | 3 +- src/test/bench/core-std.rs | 2 +- src/test/bench/shootout-fasta-redux.rs | 2 +- src/test/bench/shootout-k-nucleotide-pipes.rs | 6 +- src/test/bench/shootout-k-nucleotide.rs | 10 +- src/test/bench/shootout-pfib.rs | 3 +- src/test/run-pass/issue-3888-2.rs | 6 +- src/test/run-pass/vec-slice.rs | 4 +- 29 files changed, 151 insertions(+), 198 deletions(-) diff --git a/src/compiletest/runtest.rs b/src/compiletest/runtest.rs index 3e2f484ee53d4..a31e0b961f7f2 100644 --- a/src/compiletest/runtest.rs +++ b/src/compiletest/runtest.rs @@ -321,8 +321,7 @@ fn check_error_patterns(props: &TestProps, if done { return; } let missing_patterns = - vec::slice(props.error_patterns, next_err_idx, - props.error_patterns.len()); + props.error_patterns.slice(next_err_idx, props.error_patterns.len()); if missing_patterns.len() == 1u { fatal_ProcRes(fmt!("error pattern '%s' not found!", missing_patterns[0]), ProcRes); diff --git a/src/libextra/crypto/sha2.rs b/src/libextra/crypto/sha2.rs index dd179fde70f5b..e9c6ac722124d 100644 --- a/src/libextra/crypto/sha2.rs +++ b/src/libextra/crypto/sha2.rs @@ -11,7 +11,6 @@ use core::prelude::*; use core::uint; -use core::vec; use digest::Digest; @@ -118,7 +117,7 @@ impl Engine512 { } while in.len() - i >= 8 { - let w = to_u64(vec::slice(in, i, i + 8)); + let w = to_u64(in.slice(i, i + 8)); self.process_word(w); self.bit_counter.add_bytes(8); i += 8; @@ -274,43 +273,43 @@ impl Engine512 { fn result_512(&mut self, out: &mut [u8]) { self.finish(); - from_u64(self.H0, vec::mut_slice(out, 0, 8)); - from_u64(self.H1, vec::mut_slice(out, 8, 16)); - from_u64(self.H2, vec::mut_slice(out, 16, 24)); - from_u64(self.H3, vec::mut_slice(out, 24, 32)); - from_u64(self.H4, vec::mut_slice(out, 32, 40)); - from_u64(self.H5, vec::mut_slice(out, 40, 48)); - from_u64(self.H6, vec::mut_slice(out, 48, 56)); - from_u64(self.H7, vec::mut_slice(out, 56, 64)); + from_u64(self.H0, out.mut_slice(0, 8)); + from_u64(self.H1, out.mut_slice(8, 16)); + from_u64(self.H2, out.mut_slice(16, 24)); + from_u64(self.H3, out.mut_slice(24, 32)); + from_u64(self.H4, out.mut_slice(32, 40)); + from_u64(self.H5, out.mut_slice(40, 48)); + from_u64(self.H6, out.mut_slice(48, 56)); + from_u64(self.H7, out.mut_slice(56, 64)); } fn result_384(&mut self, out: &mut [u8]) { self.finish(); - from_u64(self.H0, vec::mut_slice(out, 0, 8)); - from_u64(self.H1, vec::mut_slice(out, 8, 16)); - from_u64(self.H2, vec::mut_slice(out, 16, 24)); - from_u64(self.H3, vec::mut_slice(out, 24, 32)); - from_u64(self.H4, vec::mut_slice(out, 32, 40)); - from_u64(self.H5, vec::mut_slice(out, 40, 48)); + from_u64(self.H0, out.mut_slice(0, 8)); + from_u64(self.H1, out.mut_slice(8, 16)); + from_u64(self.H2, out.mut_slice(16, 24)); + from_u64(self.H3, out.mut_slice(24, 32)); + from_u64(self.H4, out.mut_slice(32, 40)); + from_u64(self.H5, out.mut_slice(40, 48)); } fn result_256(&mut self, out: &mut [u8]) { self.finish(); - from_u64(self.H0, vec::mut_slice(out, 0, 8)); - from_u64(self.H1, vec::mut_slice(out, 8, 16)); - from_u64(self.H2, vec::mut_slice(out, 16, 24)); - from_u64(self.H3, vec::mut_slice(out, 24, 32)); + from_u64(self.H0, out.mut_slice(0, 8)); + from_u64(self.H1, out.mut_slice(8, 16)); + from_u64(self.H2, out.mut_slice(16, 24)); + from_u64(self.H3, out.mut_slice(24, 32)); } fn result_224(&mut self, out: &mut [u8]) { self.finish(); - from_u64(self.H0, vec::mut_slice(out, 0, 8)); - from_u64(self.H1, vec::mut_slice(out, 8, 16)); - from_u64(self.H2, vec::mut_slice(out, 16, 24)); - from_u32((self.H3 >> 32) as u32, vec::mut_slice(out, 24, 28)); + from_u64(self.H0, out.mut_slice(0, 8)); + from_u64(self.H1, out.mut_slice(8, 16)); + from_u64(self.H2, out.mut_slice(16, 24)); + from_u32((self.H3 >> 32) as u32, out.mut_slice(24, 28)); } } @@ -400,7 +399,7 @@ impl Engine256 { } while in.len() - i >= 4 { - let w = to_u32(vec::slice(in, i, i + 4)); + let w = to_u32(in.slice(i, i + 4)); self.process_word(w); self.length_bytes += 4; i += 4; @@ -556,26 +555,26 @@ impl Engine256 { fn result_256(&mut self, out: &mut [u8]) { self.finish(); - from_u32(self.H0, vec::mut_slice(out, 0, 4)); - from_u32(self.H1, vec::mut_slice(out, 4, 8)); - from_u32(self.H2, vec::mut_slice(out, 8, 12)); - from_u32(self.H3, vec::mut_slice(out, 12, 16)); - from_u32(self.H4, vec::mut_slice(out, 16, 20)); - from_u32(self.H5, vec::mut_slice(out, 20, 24)); - from_u32(self.H6, vec::mut_slice(out, 24, 28)); - from_u32(self.H7, vec::mut_slice(out, 28, 32)); + from_u32(self.H0, out.mut_slice(0, 4)); + from_u32(self.H1, out.mut_slice(4, 8)); + from_u32(self.H2, out.mut_slice(8, 12)); + from_u32(self.H3, out.mut_slice(12, 16)); + from_u32(self.H4, out.mut_slice(16, 20)); + from_u32(self.H5, out.mut_slice(20, 24)); + from_u32(self.H6, out.mut_slice(24, 28)); + from_u32(self.H7, out.mut_slice(28, 32)); } fn result_224(&mut self, out: &mut [u8]) { self.finish(); - from_u32(self.H0, vec::mut_slice(out, 0, 4)); - from_u32(self.H1, vec::mut_slice(out, 4, 8)); - from_u32(self.H2, vec::mut_slice(out, 8, 12)); - from_u32(self.H3, vec::mut_slice(out, 12, 16)); - from_u32(self.H4, vec::mut_slice(out, 16, 20)); - from_u32(self.H5, vec::mut_slice(out, 20, 24)); - from_u32(self.H6, vec::mut_slice(out, 24, 28)); + from_u32(self.H0, out.mut_slice(0, 4)); + from_u32(self.H1, out.mut_slice(4, 8)); + from_u32(self.H2, out.mut_slice(8, 12)); + from_u32(self.H3, out.mut_slice(12, 16)); + from_u32(self.H4, out.mut_slice(16, 20)); + from_u32(self.H5, out.mut_slice(20, 24)); + from_u32(self.H6, out.mut_slice(24, 28)); } } diff --git a/src/libextra/ebml.rs b/src/libextra/ebml.rs index dd08f23a7a10f..92a027100daf4 100644 --- a/src/libextra/ebml.rs +++ b/src/libextra/ebml.rs @@ -85,7 +85,6 @@ pub mod reader { use core::ptr::offset; use core::str; use core::unstable::intrinsics::bswap32; - use core::vec; // ebml reading @@ -248,7 +247,7 @@ pub mod reader { } pub fn with_doc_data(d: Doc, f: &fn(x: &[u8]) -> T) -> T { - f(vec::slice(*d.data, d.start, d.end)) + f(d.data.slice(d.start, d.end)) } diff --git a/src/libextra/net_tcp.rs b/src/libextra/net_tcp.rs index 6ad51931c6768..f8ecac373a6c7 100644 --- a/src/libextra/net_tcp.rs +++ b/src/libextra/net_tcp.rs @@ -976,9 +976,7 @@ impl io::Writer for TcpSocketBuf { let socket_data_ptr: *TcpSocketData = &(*((*(self.data)).sock).socket_data); let w_result = write_common_impl(socket_data_ptr, - vec::slice(data, - 0, - data.len()).to_owned()); + data.slice(0, data.len()).to_owned()); if w_result.is_err() { let err_data = w_result.get_err(); debug!( diff --git a/src/libextra/num/bigint.rs b/src/libextra/num/bigint.rs index 4b080e0153ccc..1ac913e8a00d1 100644 --- a/src/libextra/num/bigint.rs +++ b/src/libextra/num/bigint.rs @@ -298,9 +298,8 @@ impl Mul for BigUint { fn cut_at(a: &BigUint, n: uint) -> (BigUint, BigUint) { let mid = uint::min(a.data.len(), n); - return (BigUint::from_slice(vec::slice(a.data, mid, - a.data.len())), - BigUint::from_slice(vec::slice(a.data, 0, mid))); + return (BigUint::from_slice(a.data.slice(mid, a.data.len())), + BigUint::from_slice(a.data.slice(0, mid))); } @@ -413,7 +412,7 @@ impl Integer for BigUint { return (Zero::zero(), Zero::zero(), copy *a); } - let an = vec::slice(a.data, a.data.len() - n, a.data.len()); + let an = a.data.slice(a.data.len() - n, a.data.len()); let bn = *b.data.last(); let mut d = ~[]; let mut carry = 0; @@ -578,7 +577,7 @@ impl BigUint { let mut power: BigUint = One::one(); loop { let start = uint::max(end, unit_len) - unit_len; - match uint::parse_bytes(vec::slice(buf, start, end), radix) { + match uint::parse_bytes(buf.slice(start, end), radix) { // FIXME(#6102): Assignment operator for BigInt causes ICE // Some(d) => n += BigUint::from_uint(d) * power, Some(d) => n = n + BigUint::from_uint(d) * power, @@ -634,7 +633,7 @@ impl BigUint { if n_unit == 0 { return copy *self; } if self.data.len() < n_unit { return Zero::zero(); } return BigUint::from_slice( - vec::slice(self.data, n_unit, self.data.len()) + self.data.slice(n_unit, self.data.len()) ); } @@ -1132,7 +1131,7 @@ impl BigInt { sign = Minus; start = 1; } - return BigUint::parse_bytes(vec::slice(buf, start, buf.len()), radix) + return BigUint::parse_bytes(buf.slice(start, buf.len()), radix) .map_consume(|bu| BigInt::from_biguint(sign, bu)); } @@ -1176,7 +1175,7 @@ mod biguint_tests { let data = [ &[], &[1], &[2], &[-1], &[0, 1], &[2, 1], &[1, 1, 1] ] .map(|v| BigUint::from_slice(*v)); for data.iter().enumerate().advance |(i, ni)| { - for vec::slice(data, i, data.len()).iter().enumerate().advance |(j0, nj)| { + for data.slice(i, data.len()).iter().enumerate().advance |(j0, nj)| { let j = j0 + i; if i == j { assert_eq!(ni.cmp(nj), Equal); @@ -1654,7 +1653,7 @@ mod bigint_tests { nums.push_all_move(vs.map(|s| BigInt::from_slice(Plus, *s))); for nums.iter().enumerate().advance |(i, ni)| { - for vec::slice(nums, i, nums.len()).iter().enumerate().advance |(j0, nj)| { + for nums.slice(i, nums.len()).iter().enumerate().advance |(j0, nj)| { let j = i + j0; if i == j { assert_eq!(ni.cmp(nj), Equal); diff --git a/src/libextra/sort.rs b/src/libextra/sort.rs index 3c872d64ca052..5377dfadbaa11 100644 --- a/src/libextra/sort.rs +++ b/src/libextra/sort.rs @@ -57,8 +57,8 @@ pub fn merge_sort(v: &[T], le: Le) -> ~[T] { a_ix += 1; } else { rs.push(copy b[b_ix]); b_ix += 1; } } - rs.push_all(vec::slice(a, a_ix, a_len)); - rs.push_all(vec::slice(b, b_ix, b_len)); + rs.push_all(a.slice(a_ix, a_len)); + rs.push_all(b.slice(b_ix, b_len)); rs } } @@ -201,12 +201,12 @@ pub fn tim_sort(array: &mut [T]) { loop { let run_len: uint = { // This scope contains the slice `arr` here: - let arr = vec::mut_slice(array, idx, size); + let arr = array.mut_slice(idx, size); let mut run_len: uint = count_run_ascending(arr); if run_len < min_run { let force = if remaining <= min_run {remaining} else {min_run}; - let slice = vec::mut_slice(arr, 0, force); + let slice = arr.mut_slice(0, force); binarysort(slice, run_len); run_len = force; } @@ -443,14 +443,14 @@ impl MergeState { } let k = { // constrain lifetime of slice below - let slice = vec::slice(array, b1, b1+l1); + let slice = array.slice(b1, b1+l1); gallop_right(&array[b2], slice, 0) }; b1 += k; l1 -= k; if l1 != 0 { let l2 = { // constrain lifetime of slice below - let slice = vec::slice(array, b2, b2+l2); + let slice = array.slice(b2, b2+l2); gallop_left(&array[b1+l1-1],slice,l2-1) }; if l2 > 0 { @@ -526,7 +526,7 @@ impl MergeState { assert!(len1 > 1 && len2 != 0); count1 = { - let tmp_view = vec::slice(tmp, c1, c1+len1); + let tmp_view = tmp.slice(c1, c1+len1); gallop_right(&array[c2], tmp_view, 0) }; if count1 != 0 { @@ -539,7 +539,7 @@ impl MergeState { if len2 == 0 { break_outer = true; break; } count2 = { - let tmp_view = vec::slice(array, c2, c2+len2); + let tmp_view = array.slice(c2, c2+len2); gallop_left(&tmp[c1], tmp_view, 0) }; if count2 != 0 { @@ -638,7 +638,7 @@ impl MergeState { assert!(len2 > 1 && len1 != 0); { // constrain scope of tmp_view: - let tmp_view = vec::mut_slice (array, base1, base1+len1); + let tmp_view = array.mut_slice(base1, base1+len1); count1 = len1 - gallop_right( &tmp[c2], tmp_view, len1-1); } @@ -655,7 +655,7 @@ impl MergeState { let count2; { // constrain scope of tmp_view - let tmp_view = vec::mut_slice(tmp, 0, len2); + let tmp_view = tmp.mut_slice(0, len2); count2 = len2 - gallop_left(&array[c1], tmp_view, len2-1); @@ -1111,7 +1111,7 @@ mod big_tests { isSorted(arr); let mut arr = if n > 4 { - let part = vec::slice(arr, 0, 4); + let part = arr.slice(0, 4); multiplyVec(part, n) } else { arr }; tim_sort(arr); // ~sort @@ -1183,7 +1183,7 @@ mod big_tests { isSorted(arr); let mut arr = if n > 4 { - let part = vec::slice(arr, 0, 4); + let part = arr.slice(0, 4); multiplyVec(part, n) } else { arr }; tim_sort(arr); // ~sort diff --git a/src/librustc/back/rpath.rs b/src/librustc/back/rpath.rs index 4657c069c21e3..dce2b7fe3dfb0 100644 --- a/src/librustc/back/rpath.rs +++ b/src/librustc/back/rpath.rs @@ -154,7 +154,7 @@ pub fn get_relative_to(abs1: &Path, abs2: &Path) -> Path { let mut path = ~[]; for uint::range(start_idx, len1 - 1) |_i| { path.push(~".."); }; - path.push_all(vec::slice(split2, start_idx, len2 - 1)); + path.push_all(split2.slice(start_idx, len2 - 1)); return if !path.is_empty() { Path("").push_many(path) diff --git a/src/librustc/metadata/decoder.rs b/src/librustc/metadata/decoder.rs index 824f67b074c29..39cce41b38692 100644 --- a/src/librustc/metadata/decoder.rs +++ b/src/librustc/metadata/decoder.rs @@ -60,7 +60,7 @@ fn lookup_hash(d: ebml::Doc, eq_fn: &fn(x:&[u8]) -> bool, hash: uint) -> let belt = tag_index_buckets_bucket_elt; for reader::tagged_docs(tagged_doc.doc, belt) |elt| { let pos = io::u64_from_be_bytes(*elt.data, elt.start, 4u) as uint; - if eq_fn(vec::slice(*elt.data, elt.start + 4u, elt.end)) { + if eq_fn(elt.data.slice(elt.start + 4u, elt.end)) { return Some(reader::doc_at(d.data, pos).doc); } }; @@ -72,7 +72,7 @@ pub type GetCrateDataCb<'self> = &'self fn(ast::crate_num) -> cmd; pub fn maybe_find_item(item_id: int, items: ebml::Doc) -> Option { fn eq_item(bytes: &[u8], item_id: int) -> bool { return io::u64_from_be_bytes( - vec::slice(bytes, 0u, 4u), 0u, 4u) as int + bytes.slice(0u, 4u), 0u, 4u) as int == item_id; } lookup_hash(items, diff --git a/src/librustc/metadata/tydecode.rs b/src/librustc/metadata/tydecode.rs index b53bdcc9bbe0b..58fac69659ce5 100644 --- a/src/librustc/metadata/tydecode.rs +++ b/src/librustc/metadata/tydecode.rs @@ -20,7 +20,6 @@ use middle::ty; use core::str; use core::uint; -use core::vec; use syntax::abi::AbiSet; use syntax::abi; use syntax::ast; @@ -519,8 +518,8 @@ pub fn parse_def_id(buf: &[u8]) -> ast::def_id { fail!(); } - let crate_part = vec::slice(buf, 0u, colon_idx); - let def_part = vec::slice(buf, colon_idx + 1u, len); + let crate_part = buf.slice(0u, colon_idx); + let def_part = buf.slice(colon_idx + 1u, len); let crate_num = match uint::parse_bytes(crate_part, 10u) { Some(cn) => cn as int, diff --git a/src/librustc/middle/dataflow.rs b/src/librustc/middle/dataflow.rs index acd47eca7265c..93202f3fd558b 100644 --- a/src/librustc/middle/dataflow.rs +++ b/src/librustc/middle/dataflow.rs @@ -132,7 +132,7 @@ impl DataFlowContext { debug!("add_gen(id=%?, bit=%?)", id, bit); let (start, end) = self.compute_id_range(id); { - let gens = vec::mut_slice(self.gens, start, end); + let gens = self.gens.mut_slice(start, end); set_bit(gens, bit); } } @@ -143,7 +143,7 @@ impl DataFlowContext { debug!("add_kill(id=%?, bit=%?)", id, bit); let (start, end) = self.compute_id_range(id); { - let kills = vec::mut_slice(self.kills, start, end); + let kills = self.kills.mut_slice(start, end); set_bit(kills, bit); } } @@ -216,7 +216,7 @@ impl DataFlowContext { return true; } let (start, end) = self.compute_id_range_frozen(id); - let on_entry = vec::slice(self.on_entry, start, end); + let on_entry = self.on_entry.slice(start, end); debug!("each_bit_on_entry_frozen(id=%?, on_entry=%s)", id, bits_to_str(on_entry)); self.each_bit(on_entry, f) @@ -229,7 +229,7 @@ impl DataFlowContext { //! Only useful after `propagate()` has been called. let (start, end) = self.compute_id_range(id); - let on_entry = vec::slice(self.on_entry, start, end); + let on_entry = self.on_entry.slice(start, end); debug!("each_bit_on_entry(id=%?, on_entry=%s)", id, bits_to_str(on_entry)); self.each_bit(on_entry, f) @@ -241,7 +241,7 @@ impl DataFlowContext { //! Iterates through each bit in the gen set for `id`. let (start, end) = self.compute_id_range(id); - let gens = vec::slice(self.gens, start, end); + let gens = self.gens.slice(start, end); debug!("each_gen_bit(id=%?, gens=%s)", id, bits_to_str(gens)); self.each_bit(gens, f) @@ -255,7 +255,7 @@ impl DataFlowContext { return true; } let (start, end) = self.compute_id_range_frozen(id); - let gens = vec::slice(self.gens, start, end); + let gens = self.gens.slice(start, end); debug!("each_gen_bit(id=%?, gens=%s)", id, bits_to_str(gens)); self.each_bit(gens, f) @@ -338,17 +338,17 @@ impl DataFlowContext { if self.nodeid_to_bitset.contains_key(&id) { let (start, end) = self.compute_id_range_frozen(id); - let on_entry = vec::slice(self.on_entry, start, end); + let on_entry = self.on_entry.slice(start, end); let entry_str = bits_to_str(on_entry); - let gens = vec::slice(self.gens, start, end); + let gens = self.gens.slice(start, end); let gens_str = if gens.iter().any_(|&u| u != 0) { fmt!(" gen: %s", bits_to_str(gens)) } else { ~"" }; - let kills = vec::slice(self.kills, start, end); + let kills = self.kills.slice(start, end); let kills_str = if kills.iter().any_(|&u| u != 0) { fmt!(" kill: %s", bits_to_str(kills)) } else { @@ -953,7 +953,7 @@ impl<'self, O:DataFlowOperator> PropagationContext<'self, O> { id, bits_to_str(pred_bits)); let (start, end) = self.dfcx.compute_id_range(id); let changed = { // FIXME(#5074) awkward construction - let on_entry = vec::mut_slice(self.dfcx.on_entry, start, end); + let on_entry = self.dfcx.on_entry.mut_slice(start, end); join_bits(&self.dfcx.oper, pred_bits, on_entry) }; if changed { @@ -970,7 +970,7 @@ impl<'self, O:DataFlowOperator> PropagationContext<'self, O> { id, mut_bits_to_str(pred_bits)); let (start, end) = self.dfcx.compute_id_range(id); let changed = { // FIXME(#5074) awkward construction - let on_entry = vec::mut_slice(self.dfcx.on_entry, start, end); + let on_entry = self.dfcx.on_entry.mut_slice(start, end); let changed = join_bits(&self.dfcx.oper, reslice(pred_bits), on_entry); copy_bits(reslice(on_entry), pred_bits); changed diff --git a/src/librustc/middle/kind.rs b/src/librustc/middle/kind.rs index a1f595fc8969f..ca0d544198e8f 100644 --- a/src/librustc/middle/kind.rs +++ b/src/librustc/middle/kind.rs @@ -17,7 +17,6 @@ use middle::typeck; use util::ppaux::{Repr, ty_to_str}; use util::ppaux::UserString; -use core::vec; use syntax::ast::*; use syntax::attr::attrs_contains_name; use syntax::codemap::span; diff --git a/src/librustc/middle/trans/_match.rs b/src/librustc/middle/trans/_match.rs index 63b39b8fe763e..7e17a947d303f 100644 --- a/src/librustc/middle/trans/_match.rs +++ b/src/librustc/middle/trans/_match.rs @@ -388,9 +388,9 @@ pub fn expand_nested_bindings<'r>(bcx: block, match br.pats[col].node { ast::pat_ident(_, path, Some(inner)) => { let pats = vec::append( - vec::slice(br.pats, 0u, col).to_owned(), + br.pats.slice(0u, col).to_owned(), vec::append(~[inner], - vec::slice(br.pats, col + 1u, + br.pats.slice(col + 1u, br.pats.len()))); let binding_info = @@ -437,8 +437,8 @@ pub fn enter_match<'r>(bcx: block, Some(sub) => { let pats = vec::append( - vec::append(sub, vec::slice(br.pats, 0u, col)), - vec::slice(br.pats, col + 1u, br.pats.len())); + vec::append(sub, br.pats.slice(0u, col)), + br.pats.slice(col + 1u, br.pats.len())); let this = br.pats[col]; match this.node { @@ -1290,7 +1290,7 @@ pub fn compile_submatch(bcx: block, match data.arm.guard { Some(guard_expr) => { bcx = compile_guard(bcx, guard_expr, m[0].data, - vec::slice(m, 1, m.len()), + m.slice(1, m.len()), vals, chk); } _ => () @@ -1309,8 +1309,8 @@ pub fn compile_submatch(bcx: block, } }; - let vals_left = vec::append(vec::slice(vals, 0u, col).to_owned(), - vec::slice(vals, col + 1u, vals.len())); + let vals_left = vec::append(vals.slice(0u, col).to_owned(), + vals.slice(col + 1u, vals.len())); let ccx = bcx.fcx.ccx; let mut pat_id = 0; let mut pat_span = dummy_sp(); diff --git a/src/librustc/middle/trans/common.rs b/src/librustc/middle/trans/common.rs index 272d7b234aa57..fd3ebd3d95159 100644 --- a/src/librustc/middle/trans/common.rs +++ b/src/librustc/middle/trans/common.rs @@ -419,10 +419,9 @@ pub fn revoke_clean(cx: block, val: ValueRef) { }); for cleanup_pos.iter().advance |i| { scope_info.cleanups = - vec::append(vec::slice(scope_info.cleanups, 0u, *i).to_owned(), - vec::slice(scope_info.cleanups, - *i + 1u, - scope_info.cleanups.len())); + vec::append(scope_info.cleanups.slice(0u, *i).to_owned(), + scope_info.cleanups.slice(*i + 1u, + scope_info.cleanups.len())); shrink_scope_clean(scope_info, *i); } } diff --git a/src/librustc/middle/trans/meth.rs b/src/librustc/middle/trans/meth.rs index 96f8a1976a68c..38e4f087b0ecd 100644 --- a/src/librustc/middle/trans/meth.rs +++ b/src/librustc/middle/trans/meth.rs @@ -528,7 +528,7 @@ pub fn combine_impl_and_methods_origins(bcx: block, } = ty::lookup_item_type(tcx, mth_did); let n_r_m_tps = r_m_generics.type_param_defs.len(); // rcvr + method tps let m_type_param_defs = - vec::slice(*r_m_generics.type_param_defs, n_r_m_tps - n_m_tps, n_r_m_tps); + r_m_generics.type_param_defs.slice(n_r_m_tps - n_m_tps, n_r_m_tps); // Flatten out to find the number of vtables the method expects. let m_vtables = ty::count_traits_and_supertraits(tcx, m_type_param_defs); diff --git a/src/librusti/rusti.rs b/src/librusti/rusti.rs index 54a404d971eec..a586cb83fa180 100644 --- a/src/librusti/rusti.rs +++ b/src/librusti/rusti.rs @@ -55,7 +55,7 @@ extern mod extra; extern mod rustc; extern mod syntax; -use std::{libc, io, os, task, vec}; +use std::{libc, io, os, task}; use std::cell::Cell; use extra::rl; @@ -430,7 +430,7 @@ pub fn run_line(repl: &mut Repl, in: @io::Reader, out: @io::Writer, line: ~str, if !cmd.is_empty() { let args = if len > 1 { - vec::slice(split, 1, len).to_owned() + split.slice(1, len).to_owned() } else { ~[] }; match run_cmd(repl, in, out, cmd, args, use_rl) { diff --git a/src/librustpkg/package_source.rs b/src/librustpkg/package_source.rs index 9b727e9d3e059..ebdea2537ac38 100644 --- a/src/librustpkg/package_source.rs +++ b/src/librustpkg/package_source.rs @@ -12,7 +12,7 @@ use target::*; use package_id::PkgId; use core::path::Path; use core::option::*; -use core::{os, run, str, vec}; +use core::{os, run, str}; use context::*; use crate::Crate; use messages::*; @@ -146,8 +146,7 @@ impl PkgSrc { fn push_crate(cs: &mut ~[Crate], prefix: uint, p: &Path) { assert!(p.components.len() > prefix); let mut sub = Path(""); - for vec::slice(p.components, prefix, - p.components.len()).iter().advance |c| { + for p.components.slice(prefix, p.components.len()).iter().advance |c| { sub = sub.push(*c); } debug!("found crate %s", sub.to_str()); diff --git a/src/libstd/io.rs b/src/libstd/io.rs index a78be9c8b2b1d..4d9c08f25da74 100644 --- a/src/libstd/io.rs +++ b/src/libstd/io.rs @@ -65,7 +65,7 @@ use str::StrSlice; use to_str::ToStr; use uint; use vec; -use vec::{ImmutableVector, OwnedVector, OwnedCopyableVector, CopyableVector}; +use vec::{MutableVector, ImmutableVector, OwnedVector, OwnedCopyableVector, CopyableVector}; #[allow(non_camel_case_types)] // not sure what to do about this pub type fd_t = c_int; @@ -698,7 +698,7 @@ impl ReaderUtil for T { // over-read by reading 1-byte per char needed nbread = if ncreq > nbreq { ncreq } else { nbreq }; if nbread > 0 { - bytes = vec::slice(bytes, offset, bytes.len()).to_owned(); + bytes = bytes.slice(offset, bytes.len()).to_owned(); } } chars @@ -1053,7 +1053,7 @@ impl Reader for BytesReader { fn read(&self, bytes: &mut [u8], len: uint) -> uint { let count = uint::min(len, self.bytes.len() - *self.pos); - let view = vec::slice(self.bytes, *self.pos, self.bytes.len()); + let view = self.bytes.slice(*self.pos, self.bytes.len()); vec::bytes::copy_memory(bytes, view, count); *self.pos += count; @@ -1663,7 +1663,7 @@ impl Writer for BytesWriter { unsafe { vec::raw::set_len(bytes, count); - let view = vec::mut_slice(*bytes, *self.pos, count); + let view = bytes.mut_slice(*self.pos, count); vec::bytes::copy_memory(view, v, v_len); } @@ -1909,8 +1909,7 @@ mod tests { if len <= ivals.len() { assert_eq!(res.len(), len); } - assert!(vec::slice(ivals, 0u, res.len()) == - vec::map(res, |x| *x as int)); + assert!(ivals.slice(0u, res.len()) == vec::map(res, |x| *x as int)); } } let mut i = 0; diff --git a/src/libstd/os.rs b/src/libstd/os.rs index 112540c405da7..5534c5befc2b2 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -134,7 +134,7 @@ pub mod win32 { } } if k != 0 && done { - let sub = vec::slice(buf, 0u, k as uint); + let sub = buf.slice(0, k as uint); res = option::Some(str::from_utf16(sub)); } } diff --git a/src/libstd/rt/io/extensions.rs b/src/libstd/rt/io/extensions.rs index 55861f127bb44..5320bd0f42ee1 100644 --- a/src/libstd/rt/io/extensions.rs +++ b/src/libstd/rt/io/extensions.rs @@ -298,7 +298,7 @@ impl ReaderUtil for T { do (|| { while total_read < len { let len = buf.len(); - let slice = vec::mut_slice(*buf, start_len + total_read, len); + let slice = buf.mut_slice(start_len + total_read, len); match self.read(slice) { Some(nread) => { total_read += nread; diff --git a/src/libstd/rt/io/mem.rs b/src/libstd/rt/io/mem.rs index bd9cff76e5763..c93945a6a9aa9 100644 --- a/src/libstd/rt/io/mem.rs +++ b/src/libstd/rt/io/mem.rs @@ -86,7 +86,7 @@ impl Reader for MemReader { let write_len = min(buf.len(), self.buf.len() - self.pos); { let input = self.buf.slice(self.pos, self.pos + write_len); - let output = vec::mut_slice(buf, 0, write_len); + let output = buf.mut_slice(0, write_len); assert_eq!(input.len(), output.len()); vec::bytes::copy_memory(output, input, write_len); } diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 56e6bacf93e30..c8dc5aa7f7992 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -251,17 +251,17 @@ pub fn head_opt<'r,T>(v: &'r [T]) -> Option<&'r T> { } /// Returns a vector containing all but the first element of a slice -pub fn tail<'r,T>(v: &'r [T]) -> &'r [T] { slice(v, 1, v.len()) } +pub fn tail<'r,T>(v: &'r [T]) -> &'r [T] { v.slice(1, v.len()) } /// Returns a vector containing all but the first `n` elements of a slice -pub fn tailn<'r,T>(v: &'r [T], n: uint) -> &'r [T] { slice(v, n, v.len()) } +pub fn tailn<'r,T>(v: &'r [T], n: uint) -> &'r [T] { v.slice(n, v.len()) } /// Returns a vector containing all but the last element of a slice -pub fn init<'r,T>(v: &'r [T]) -> &'r [T] { slice(v, 0, v.len() - 1) } +pub fn init<'r,T>(v: &'r [T]) -> &'r [T] { v.slice(0, v.len() - 1) } /// Returns a vector containing all but the last `n' elements of a slice pub fn initn<'r,T>(v: &'r [T], n: uint) -> &'r [T] { - slice(v, 0, v.len() - n) + v.slice(0, v.len() - n) } /// Returns the last element of the slice `v`, failing if the slice is empty. @@ -276,47 +276,6 @@ pub fn last_opt<'r,T>(v: &'r [T]) -> Option<&'r T> { if v.len() == 0 { None } else { Some(&v[v.len() - 1]) } } -/// Return a slice that points into another slice. -#[inline] -pub fn slice<'r,T>(v: &'r [T], start: uint, end: uint) -> &'r [T] { - assert!(start <= end); - assert!(end <= v.len()); - do as_imm_buf(v) |p, _len| { - unsafe { - transmute((ptr::offset(p, start), - (end - start) * sys::nonzero_size_of::())) - } - } -} - -/// Return a slice that points into another slice. -#[inline] -pub fn mut_slice<'r,T>(v: &'r mut [T], start: uint, end: uint) - -> &'r mut [T] { - assert!(start <= end); - assert!(end <= v.len()); - do as_mut_buf(v) |p, _len| { - unsafe { - transmute((ptr::mut_offset(p, start), - (end - start) * sys::nonzero_size_of::())) - } - } -} - -/// Return a slice that points into another slice. -#[inline] -pub fn const_slice<'r,T>(v: &'r const [T], start: uint, end: uint) - -> &'r const [T] { - assert!(start <= end); - assert!(end <= v.len()); - do as_const_buf(v) |p, _len| { - unsafe { - transmute((ptr::const_offset(p, start), - (end - start) * sys::nonzero_size_of::())) - } - } -} - /// Copies /// Split the vector `v` by applying each element against the predicate `f`. @@ -330,12 +289,12 @@ pub fn split(v: &[T], f: &fn(t: &T) -> bool) -> ~[~[T]] { match position_between(v, start, ln, f) { None => break, Some(i) => { - result.push(slice(v, start, i).to_owned()); + result.push(v.slice(start, i).to_owned()); start = i + 1u; } } } - result.push(slice(v, start, ln).to_owned()); + result.push(v.slice(start, ln).to_owned()); result } @@ -354,14 +313,14 @@ pub fn splitn(v: &[T], n: uint, f: &fn(t: &T) -> bool) -> ~[~[T]] { match position_between(v, start, ln, f) { None => break, Some(i) => { - result.push(slice(v, start, i).to_owned()); + result.push(v.slice(start, i).to_owned()); // Make sure to skip the separator. start = i + 1u; count -= 1u; } } } - result.push(slice(v, start, ln).to_owned()); + result.push(v.slice(start, ln).to_owned()); result } @@ -379,12 +338,12 @@ pub fn rsplit(v: &[T], f: &fn(t: &T) -> bool) -> ~[~[T]] { match rposition_between(v, 0, end, f) { None => break, Some(i) => { - result.push(slice(v, i + 1, end).to_owned()); + result.push(v.slice(i + 1, end).to_owned()); end = i; } } } - result.push(slice(v, 0u, end).to_owned()); + result.push(v.slice(0u, end).to_owned()); reverse(result); result } @@ -404,14 +363,14 @@ pub fn rsplitn(v: &[T], n: uint, f: &fn(t: &T) -> bool) -> ~[~[T]] { match rposition_between(v, 0u, end, f) { None => break, Some(i) => { - result.push(slice(v, i + 1u, end).to_owned()); + result.push(v.slice(i + 1u, end).to_owned()); // Make sure to skip the separator. end = i; count -= 1u; } } } - result.push(slice(v, 0u, end).to_owned()); + result.push(v.slice(0u, end).to_owned()); reverse(result); result } @@ -487,15 +446,15 @@ pub fn shift(v: &mut ~[T]) -> T { // popped. For the moment it unsafely exists at both the head and last // positions { - let first_slice = slice(*v, 0, 1); - let last_slice = slice(*v, next_ln, ln); + let first_slice = v.slice(0, 1); + let last_slice = v.slice(next_ln, ln); raw::copy_memory(transmute(last_slice), first_slice, 1); } // Memcopy everything to the left one element { - let init_slice = slice(*v, 0, next_ln); - let tail_slice = slice(*v, 1, ln); + let init_slice = v.slice(0, next_ln); + let tail_slice = v.slice(1, ln); raw::copy_memory(transmute(init_slice), tail_slice, next_ln); @@ -1689,7 +1648,14 @@ impl<'self,T> ImmutableVector<'self, T> for &'self [T] { /// Return a slice that points into another slice. #[inline] fn slice(&self, start: uint, end: uint) -> &'self [T] { - slice(*self, start, end) + assert!(start <= end); + assert!(end <= self.len()); + do as_imm_buf(*self) |p, _len| { + unsafe { + transmute((ptr::offset(p, start), + (end - start) * sys::nonzero_size_of::())) + } + } } #[inline] @@ -2042,9 +2008,17 @@ pub trait MutableVector<'self, T> { } impl<'self,T> MutableVector<'self, T> for &'self mut [T] { + /// Return a slice that points into another slice. #[inline] fn mut_slice(self, start: uint, end: uint) -> &'self mut [T] { - mut_slice(self, start, end) + assert!(start <= end); + assert!(end <= self.len()); + do as_mut_buf(self) |p, _len| { + unsafe { + transmute((ptr::mut_offset(p, start), + (end - start) * sys::nonzero_size_of::())) + } + } } #[inline] @@ -2713,7 +2687,7 @@ mod tests { fn test_slice() { // Test fixed length vector. let vec_fixed = [1, 2, 3, 4]; - let v_a = slice(vec_fixed, 1u, vec_fixed.len()).to_owned(); + let v_a = vec_fixed.slice(1u, vec_fixed.len()).to_owned(); assert_eq!(v_a.len(), 3u); assert_eq!(v_a[0], 2); assert_eq!(v_a[1], 3); @@ -2721,14 +2695,14 @@ mod tests { // Test on stack. let vec_stack = &[1, 2, 3]; - let v_b = slice(vec_stack, 1u, 3u).to_owned(); + let v_b = vec_stack.slice(1u, 3u).to_owned(); assert_eq!(v_b.len(), 2u); assert_eq!(v_b[0], 2); assert_eq!(v_b[1], 3); // Test on managed heap. let vec_managed = @[1, 2, 3, 4, 5]; - let v_c = slice(vec_managed, 0u, 3u).to_owned(); + let v_c = vec_managed.slice(0u, 3u).to_owned(); assert_eq!(v_c.len(), 3u); assert_eq!(v_c[0], 1); assert_eq!(v_c[1], 2); @@ -2736,7 +2710,7 @@ mod tests { // Test on exchange heap. let vec_unique = ~[1, 2, 3, 4, 5, 6]; - let v_d = slice(vec_unique, 1u, 6u).to_owned(); + let v_d = vec_unique.slice(1u, 6u).to_owned(); assert_eq!(v_d.len(), 5u); assert_eq!(v_d[0], 2); assert_eq!(v_d[1], 3); diff --git a/src/libsyntax/diagnostic.rs b/src/libsyntax/diagnostic.rs index 89867922b25bd..c7f33587f3157 100644 --- a/src/libsyntax/diagnostic.rs +++ b/src/libsyntax/diagnostic.rs @@ -13,7 +13,6 @@ use codemap; use std::io; use std::uint; -use std::vec; use extra::term; pub type Emitter = @fn(cmsp: Option<(@codemap::CodeMap, span)>, @@ -250,7 +249,7 @@ fn highlight_lines(cm: @codemap::CodeMap, let mut elided = false; let mut display_lines = /* FIXME (#2543) */ copy lines.lines; if display_lines.len() > max_lines { - display_lines = vec::slice(display_lines, 0u, max_lines).to_owned(); + display_lines = display_lines.slice(0u, max_lines).to_owned(); elided = true; } // Print the offending lines diff --git a/src/test/bench/core-std.rs b/src/test/bench/core-std.rs index 4b5880de8a52e..787cf696cc348 100644 --- a/src/test/bench/core-std.rs +++ b/src/test/bench/core-std.rs @@ -29,7 +29,7 @@ macro_rules! bench ( fn main() { let argv = os::args(); - let tests = vec::slice(argv, 1, argv.len()); + let tests = argv.slice(1, argv.len()); bench!(shift_push); bench!(read_line); diff --git a/src/test/bench/shootout-fasta-redux.rs b/src/test/bench/shootout-fasta-redux.rs index 670e77bc3a0ec..5ebcfe164ce77 100644 --- a/src/test/bench/shootout-fasta-redux.rs +++ b/src/test/bench/shootout-fasta-redux.rs @@ -96,7 +96,7 @@ impl RepeatFasta { copy_memory(buf, alu, alu_len); let buf_len = buf.len(); - copy_memory(vec::mut_slice(buf, alu_len, buf_len), + copy_memory(buf.mut_slice(alu_len, buf_len), alu, LINE_LEN); diff --git a/src/test/bench/shootout-k-nucleotide-pipes.rs b/src/test/bench/shootout-k-nucleotide-pipes.rs index 20042aa0e918a..57683fa3dbf0c 100644 --- a/src/test/bench/shootout-k-nucleotide-pipes.rs +++ b/src/test/bench/shootout-k-nucleotide-pipes.rs @@ -90,7 +90,7 @@ fn find(mm: &HashMap<~[u8], uint>, key: ~str) -> uint { // given a map, increment the counter for a key fn update_freq(mm: &mut HashMap<~[u8], uint>, key: &[u8]) { - let key = vec::slice(key, 0, key.len()).to_owned(); + let key = key.to_owned(); let newval = match mm.pop(&key) { Some(v) => v + 1, None => 1 @@ -107,11 +107,11 @@ fn windows_with_carry(bb: &[u8], nn: uint, let len = bb.len(); while ii < len - (nn - 1u) { - it(vec::slice(bb, ii, ii+nn)); + it(bb.slice(ii, ii+nn)); ii += 1u; } - return vec::slice(bb, len - (nn - 1u), len).to_owned(); + return bb.slice(len - (nn - 1u), len).to_owned(); } fn make_sequence_processor(sz: uint, diff --git a/src/test/bench/shootout-k-nucleotide.rs b/src/test/bench/shootout-k-nucleotide.rs index 646b9788f706a..405aa68c483ba 100644 --- a/src/test/bench/shootout-k-nucleotide.rs +++ b/src/test/bench/shootout-k-nucleotide.rs @@ -8,7 +8,7 @@ use std::libc::{STDIN_FILENO, c_int, fdopen, fgets, fileno, fopen, fstat}; use std::libc::{stat, strlen}; use std::ptr::null; use std::unstable::intrinsics::init; -use std::vec::{reverse, slice}; +use std::vec::{reverse}; use extra::sort::quick_sort3; static LINE_LEN: uint = 80; @@ -194,7 +194,7 @@ fn unpack_symbol(c: u8) -> u8 { fn next_char<'a>(mut buf: &'a [u8]) -> &'a [u8] { loop { - buf = slice(buf, 1, buf.len()); + buf = buf.slice(1, buf.len()); if buf.len() == 0 { break; } @@ -226,7 +226,7 @@ fn read_stdin() -> ~[u8] { fgets(transmute(&mut window[0]), LINE_LEN as c_int, stdin); { - if vec::slice(window, 0, 6) == header { + if window.slice(0, 6) == header { break; } } @@ -235,9 +235,7 @@ fn read_stdin() -> ~[u8] { while fgets(transmute(&mut window[0]), LINE_LEN as c_int, stdin) != null() { - window = vec::mut_slice(window, - strlen(transmute(&window[0])) as uint, - window.len()); + window = window.mut_slice(strlen(transmute(&window[0])) as uint, window.len()); } } diff --git a/src/test/bench/shootout-pfib.rs b/src/test/bench/shootout-pfib.rs index 01425d82db296..295211e03a14b 100644 --- a/src/test/bench/shootout-pfib.rs +++ b/src/test/bench/shootout-pfib.rs @@ -32,7 +32,6 @@ use std::str; use std::task; use std::u64; use std::uint; -use std::vec; fn fib(n: int) -> int { fn pfib(c: &Chan, n: int) { @@ -62,7 +61,7 @@ struct Config { fn parse_opts(argv: ~[~str]) -> Config { let opts = ~[getopts::optflag(~"stress")]; - let opt_args = vec::slice(argv, 1, argv.len()); + let opt_args = argv.slice(1, argv.len()); match getopts::getopts(opt_args, opts) { Ok(ref m) => { diff --git a/src/test/run-pass/issue-3888-2.rs b/src/test/run-pass/issue-3888-2.rs index 60c5062443514..c9f6733fa2587 100644 --- a/src/test/run-pass/issue-3888-2.rs +++ b/src/test/run-pass/issue-3888-2.rs @@ -8,12 +8,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::vec; - fn vec_peek<'r, T>(v: &'r [T]) -> &'r [T] { -// This doesn't work, and should. -// v.slice(1, 5) - vec::slice(v, 1, 5) + v.slice(1, 5) } pub fn main() {} diff --git a/src/test/run-pass/vec-slice.rs b/src/test/run-pass/vec-slice.rs index 8448e4e0532c6..e3012b0862145 100644 --- a/src/test/run-pass/vec-slice.rs +++ b/src/test/run-pass/vec-slice.rs @@ -8,11 +8,9 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::vec; - pub fn main() { let v = ~[1,2,3,4,5]; - let v2 = vec::slice(v, 1, 3); + let v2 = v.slice(1, 3); assert_eq!(v2[0], 2); assert_eq!(v2[1], 3); } From d2e3e1e52ba008c58ecfa801cb5d127365d20ae5 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Thu, 27 Jun 2013 22:36:27 +1000 Subject: [PATCH 02/10] Convert vec::{head, tail, init, last} (and similar fns) to methods. --- src/compiletest/compiletest.rs | 2 +- src/libextra/test.rs | 2 +- src/librustc/middle/trans/cabi_x86_64.rs | 2 +- src/librustc/middle/trans/callee.rs | 3 +- src/librustc/middle/trans/meth.rs | 5 +- src/librustc/middle/ty.rs | 2 +- src/libstd/vec.rs | 76 ++++++++---------------- 7 files changed, 33 insertions(+), 59 deletions(-) diff --git a/src/compiletest/compiletest.rs b/src/compiletest/compiletest.rs index e8876c4851bc7..b2ba05c550def 100644 --- a/src/compiletest/compiletest.rs +++ b/src/compiletest/compiletest.rs @@ -75,7 +75,7 @@ pub fn parse_config(args: ~[~str]) -> config { ]; assert!(!args.is_empty()); - let args_ = vec::tail(args); + let args_ = args.tail(); let matches = &match getopts::getopts(args_, opts) { Ok(m) => m, diff --git a/src/libextra/test.rs b/src/libextra/test.rs index 72e70943ce1cc..bb03e3ab9bb27 100644 --- a/src/libextra/test.rs +++ b/src/libextra/test.rs @@ -139,7 +139,7 @@ type OptRes = Either; // Parses command line arguments into test options pub fn parse_opts(args: &[~str]) -> OptRes { - let args_ = vec::tail(args); + let args_ = args.tail(); let opts = ~[getopts::optflag("ignored"), getopts::optflag("test"), getopts::optflag("bench"), diff --git a/src/librustc/middle/trans/cabi_x86_64.rs b/src/librustc/middle/trans/cabi_x86_64.rs index 14ab17f5030d6..73323634c2b6c 100644 --- a/src/librustc/middle/trans/cabi_x86_64.rs +++ b/src/librustc/middle/trans/cabi_x86_64.rs @@ -312,7 +312,7 @@ fn llreg_ty(cls: &[RegClass]) -> Type { tys.push(Type::i64()); } SSEFv => { - let vec_len = llvec_len(vec::tailn(cls, i + 1u)) * 2u; + let vec_len = llvec_len(cls.tailn(i + 1u)) * 2u; let vec_ty = Type::vector(&Type::f32(), vec_len as u64); tys.push(vec_ty); i += vec_len; diff --git a/src/librustc/middle/trans/callee.rs b/src/librustc/middle/trans/callee.rs index cb47555063843..064a457c71270 100644 --- a/src/librustc/middle/trans/callee.rs +++ b/src/librustc/middle/trans/callee.rs @@ -47,7 +47,6 @@ use util::ppaux::Repr; use middle::trans::type_::Type; -use core::vec; use syntax::ast; use syntax::ast_map; use syntax::visit; @@ -503,7 +502,7 @@ pub fn trans_call_inner(in_cx: block, do base::with_scope(in_cx, call_info, "call") |cx| { let ret_in_loop = match args { ArgExprs(args) => { - args.len() > 0u && match vec::last(args).node { + args.len() > 0u && match args.last().node { ast::expr_loop_body(@ast::expr { node: ast::expr_fn_block(_, ref body), _ diff --git a/src/librustc/middle/trans/meth.rs b/src/librustc/middle/trans/meth.rs index 38e4f087b0ecd..b6911a7eb96d5 100644 --- a/src/librustc/middle/trans/meth.rs +++ b/src/librustc/middle/trans/meth.rs @@ -492,8 +492,7 @@ pub fn combine_impl_and_methods_tps(bcx: block, debug!("rcvr_substs=%?", rcvr_substs.map(|t| bcx.ty_to_str(*t))); let ty_substs = vec::append(rcvr_substs.to_owned(), - vec::tailn(node_substs, - node_substs.len() - n_m_tps)); + node_substs.tailn(node_substs.len() - n_m_tps)); debug!("n_m_tps=%?", n_m_tps); debug!("node_substs=%?", node_substs.map(|t| bcx.ty_to_str(*t))); debug!("ty_substs=%?", ty_substs.map(|t| bcx.ty_to_str(*t))); @@ -540,7 +539,7 @@ pub fn combine_impl_and_methods_origins(bcx: block, }; // Extract those that belong to method: - let m_origins = vec::tailn(*r_m_origins, r_m_origins.len() - m_vtables); + let m_origins = r_m_origins.tailn(r_m_origins.len() - m_vtables); // Combine rcvr + method to find the final result: @vec::append(/*bad*/copy *rcvr_origins, m_origins) diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index 4cea4c9497240..939fecfe40aec 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -3954,7 +3954,7 @@ pub fn item_path(cx: ctxt, id: ast::def_id) -> ast_map::path { } ast_map::node_variant(ref variant, _, path) => { - vec::append_one(vec::to_owned(vec::init(*path)), + vec::append_one(vec::to_owned(path.init()), ast_map::path_name((*variant).node.name)) } diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index c8dc5aa7f7992..3dae32d717de0 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -238,44 +238,6 @@ pub fn build_sized_opt(size: Option, // Accessors -/// Returns the first element of a vector -pub fn head<'r,T>(v: &'r [T]) -> &'r T { - if v.len() == 0 { fail!("head: empty vector") } - &v[0] -} - -/// Returns `Some(x)` where `x` is the first element of the slice `v`, -/// or `None` if the vector is empty. -pub fn head_opt<'r,T>(v: &'r [T]) -> Option<&'r T> { - if v.len() == 0 { None } else { Some(&v[0]) } -} - -/// Returns a vector containing all but the first element of a slice -pub fn tail<'r,T>(v: &'r [T]) -> &'r [T] { v.slice(1, v.len()) } - -/// Returns a vector containing all but the first `n` elements of a slice -pub fn tailn<'r,T>(v: &'r [T], n: uint) -> &'r [T] { v.slice(n, v.len()) } - -/// Returns a vector containing all but the last element of a slice -pub fn init<'r,T>(v: &'r [T]) -> &'r [T] { v.slice(0, v.len() - 1) } - -/// Returns a vector containing all but the last `n' elements of a slice -pub fn initn<'r,T>(v: &'r [T], n: uint) -> &'r [T] { - v.slice(0, v.len() - n) -} - -/// Returns the last element of the slice `v`, failing if the slice is empty. -pub fn last<'r,T>(v: &'r [T]) -> &'r T { - if v.len() == 0 { fail!("last: empty vector") } - &v[v.len() - 1] -} - -/// Returns `Some(x)` where `x` is the last element of the slice `v`, or -/// `None` if the vector is empty. -pub fn last_opt<'r,T>(v: &'r [T]) -> Option<&'r T> { - if v.len() == 0 { None } else { Some(&v[v.len() - 1]) } -} - /// Copies /// Split the vector `v` by applying each element against the predicate `f`. @@ -1678,35 +1640,49 @@ impl<'self,T> ImmutableVector<'self, T> for &'self [T] { /// Returns the first element of a vector, failing if the vector is empty. #[inline] - fn head(&self) -> &'self T { head(*self) } + fn head(&self) -> &'self T { + if self.len() == 0 { fail!("head: empty vector") } + &self[0] + } - /// Returns the first element of a vector + /// Returns the first element of a vector, or `None` if it is empty #[inline] - fn head_opt(&self) -> Option<&'self T> { head_opt(*self) } + fn head_opt(&self) -> Option<&'self T> { + if self.len() == 0 { None } else { Some(&self[0]) } + } /// Returns all but the first element of a vector #[inline] - fn tail(&self) -> &'self [T] { tail(*self) } + fn tail(&self) -> &'self [T] { self.slice(1, self.len()) } /// Returns all but the first `n' elements of a vector #[inline] - fn tailn(&self, n: uint) -> &'self [T] { tailn(*self, n) } + fn tailn(&self, n: uint) -> &'self [T] { self.slice(n, self.len()) } - /// Returns all but the last elemnt of a vector + /// Returns all but the last element of a vector #[inline] - fn init(&self) -> &'self [T] { init(*self) } + fn init(&self) -> &'self [T] { + self.slice(0, self.len() - 1) + } /// Returns all but the last `n' elemnts of a vector #[inline] - fn initn(&self, n: uint) -> &'self [T] { initn(*self, n) } + fn initn(&self, n: uint) -> &'self [T] { + self.slice(0, self.len() - n) + } - /// Returns the last element of a `v`, failing if the vector is empty. + /// Returns the last element of a vector, failing if the vector is empty. #[inline] - fn last(&self) -> &'self T { last(*self) } + fn last(&self) -> &'self T { + if self.len() == 0 { fail!("last: empty vector") } + &self[self.len() - 1] + } - /// Returns the last element of a `v`, failing if the vector is empty. + /// Returns the last element of a vector, or `None` if it is empty. #[inline] - fn last_opt(&self) -> Option<&'self T> { last_opt(*self) } + fn last_opt(&self) -> Option<&'self T> { + if self.len() == 0 { None } else { Some(&self[self.len() - 1]) } + } /** * Find the last index matching some predicate From 1cb0a567d1209855b476689b0e449a832035f05b Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Thu, 27 Jun 2013 22:59:52 +1000 Subject: [PATCH 03/10] Convert vec::{pop, shift, unshift, insert, remove, swap_remove} to methods. --- src/libextra/treemap.rs | 3 +- src/librustc/middle/resolve.rs | 2 +- src/libstd/vec.rs | 234 ++++++++++++++------------------- 3 files changed, 103 insertions(+), 136 deletions(-) diff --git a/src/libextra/treemap.rs b/src/libextra/treemap.rs index 33ec4ae94ba52..4622b8c728484 100644 --- a/src/libextra/treemap.rs +++ b/src/libextra/treemap.rs @@ -695,7 +695,6 @@ mod test_treemap { use core::rand::RngUtil; use core::rand; - use core::vec; #[test] fn find_empty() { @@ -848,7 +847,7 @@ mod test_treemap { for 30.times { let r = rng.gen_uint_range(0, ctrl.len()); - let (key, _) = vec::remove(&mut ctrl, r); + let (key, _) = ctrl.remove(r); assert!(map.remove(&key)); check_structure(&map); check_equal(ctrl, &map); diff --git a/src/librustc/middle/resolve.rs b/src/librustc/middle/resolve.rs index e06fd8f971791..7cd64e863d2b1 100644 --- a/src/librustc/middle/resolve.rs +++ b/src/librustc/middle/resolve.rs @@ -4882,7 +4882,7 @@ impl Resolver { values[smallest] <= max_distance && name != maybes[smallest] { - Some(vec::swap_remove(&mut maybes, smallest)) + Some(maybes.swap_remove(smallest)) } else { None diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 3dae32d717de0..bc933e70b3733 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -377,96 +377,6 @@ pub fn partitioned(v: &[T], f: &fn(&T) -> bool) -> (~[T], ~[T]) { (lefts, rights) } -// Mutators - -/// Removes the first element from a vector and return it -pub fn shift(v: &mut ~[T]) -> T { - unsafe { - assert!(!v.is_empty()); - - if v.len() == 1 { return v.pop() } - - if v.len() == 2 { - let last = v.pop(); - let first = v.pop(); - v.push(last); - return first; - } - - let ln = v.len(); - let next_ln = v.len() - 1; - - // Save the last element. We're going to overwrite its position - let work_elt = v.pop(); - // We still should have room to work where what last element was - assert!(capacity(v) >= ln); - // Pretend like we have the original length so we can use - // the vector copy_memory to overwrite the hole we just made - raw::set_len(&mut *v, ln); - - // Memcopy the head element (the one we want) to the location we just - // popped. For the moment it unsafely exists at both the head and last - // positions - { - let first_slice = v.slice(0, 1); - let last_slice = v.slice(next_ln, ln); - raw::copy_memory(transmute(last_slice), first_slice, 1); - } - - // Memcopy everything to the left one element - { - let init_slice = v.slice(0, next_ln); - let tail_slice = v.slice(1, ln); - raw::copy_memory(transmute(init_slice), - tail_slice, - next_ln); - } - - // Set the new length. Now the vector is back to normal - raw::set_len(&mut *v, next_ln); - - // Swap out the element we want from the end - let vp = raw::to_mut_ptr(*v); - let vp = ptr::mut_offset(vp, next_ln - 1); - - ptr::replace_ptr(vp, work_elt) - } -} - -/// Prepend an element to the vector -pub fn unshift(v: &mut ~[T], x: T) { - let vv = util::replace(v, ~[x]); - v.push_all_move(vv); -} - -/// Insert an element at position i within v, shifting all -/// elements after position i one position to the right. -pub fn insert(v: &mut ~[T], i: uint, x: T) { - let len = v.len(); - assert!(i <= len); - - v.push(x); - let mut j = len; - while j > i { - swap(*v, j, j - 1); - j -= 1; - } -} - -/// Remove and return the element at position i within v, shifting -/// all elements after position i one position to the left. -pub fn remove(v: &mut ~[T], i: uint) -> T { - let len = v.len(); - assert!(i < len); - - let mut j = i; - while j < len - 1 { - swap(*v, j, j + 1); - j += 1; - } - v.pop() -} - /// Consumes all elements, in a vector, moving them out into the / closure /// provided. The vector is traversed from the start to the end. /// @@ -528,37 +438,6 @@ pub fn consume_reverse(mut v: ~[T], f: &fn(uint, v: T)) { } } -/// Remove the last element from a vector and return it -pub fn pop(v: &mut ~[T]) -> T { - let ln = v.len(); - if ln == 0 { - fail!("sorry, cannot vec::pop an empty vector") - } - let valptr = ptr::to_mut_unsafe_ptr(&mut v[ln - 1u]); - unsafe { - let val = ptr::replace_ptr(valptr, intrinsics::init()); - raw::set_len(v, ln - 1u); - val - } -} - -/** - * Remove an element from anywhere in the vector and return it, replacing it - * with the last element. This does not preserve ordering, but is O(1). - * - * Fails if index >= length. - */ -pub fn swap_remove(v: &mut ~[T], index: uint) -> T { - let ln = v.len(); - if index >= ln { - fail!("vec::swap_remove - index %u >= length %u", index, ln); - } - if index < ln - 1 { - swap(*v, index, ln - 1); - } - v.pop() -} - /// Append an element to a vector #[inline] pub fn push(v: &mut ~[T], initval: T) { @@ -1847,34 +1726,123 @@ impl OwnedVector for ~[T] { push_all_move(self, rhs); } - #[inline] + /// Remove the last element from a vector and return it fn pop(&mut self) -> T { - pop(self) + let ln = self.len(); + if ln == 0 { + fail!("sorry, cannot pop an empty vector") + } + let valptr = ptr::to_mut_unsafe_ptr(&mut self[ln - 1u]); + unsafe { + let val = ptr::replace_ptr(valptr, intrinsics::init()); + raw::set_len(self, ln - 1u); + val + } } - #[inline] + /// Removes the first element from a vector and return it fn shift(&mut self) -> T { - shift(self) + unsafe { + assert!(!self.is_empty()); + + if self.len() == 1 { return self.pop() } + + if self.len() == 2 { + let last = self.pop(); + let first = self.pop(); + self.push(last); + return first; + } + + let ln = self.len(); + let next_ln = self.len() - 1; + + // Save the last element. We're going to overwrite its position + let work_elt = self.pop(); + // We still should have room to work where what last element was + assert!(capacity(self) >= ln); + // Pretend like we have the original length so we can use + // the vector copy_memory to overwrite the hole we just made + raw::set_len(self, ln); + + // Memcopy the head element (the one we want) to the location we just + // popped. For the moment it unsafely exists at both the head and last + // positions + { + let first_slice = self.slice(0, 1); + let last_slice = self.slice(next_ln, ln); + raw::copy_memory(transmute(last_slice), first_slice, 1); + } + + // Memcopy everything to the left one element + { + let init_slice = self.slice(0, next_ln); + let tail_slice = self.slice(1, ln); + raw::copy_memory(transmute(init_slice), + tail_slice, + next_ln); + } + + // Set the new length. Now the vector is back to normal + raw::set_len(self, next_ln); + + // Swap out the element we want from the end + let vp = raw::to_mut_ptr(*self); + let vp = ptr::mut_offset(vp, next_ln - 1); + + ptr::replace_ptr(vp, work_elt) + } } - #[inline] + /// Prepend an element to the vector fn unshift(&mut self, x: T) { - unshift(self, x) + let v = util::replace(self, ~[x]); + self.push_all_move(v); } - #[inline] + /// Insert an element at position i within v, shifting all + /// elements after position i one position to the right. fn insert(&mut self, i: uint, x:T) { - insert(self, i, x) + let len = self.len(); + assert!(i <= len); + + self.push(x); + let mut j = len; + while j > i { + swap(*self, j, j - 1); + j -= 1; + } } - #[inline] + /// Remove and return the element at position i within v, shifting + /// all elements after position i one position to the left. fn remove(&mut self, i: uint) -> T { - remove(self, i) + let len = self.len(); + assert!(i < len); + + let mut j = i; + while j < len - 1 { + swap(*self, j, j + 1); + j += 1; + } + self.pop() } - #[inline] + /** + * Remove an element from anywhere in the vector and return it, replacing it + * with the last element. This does not preserve ordering, but is O(1). + * + * Fails if index >= length. + */ fn swap_remove(&mut self, index: uint) -> T { - swap_remove(self, index) + let ln = self.len(); + if index >= ln { + fail!("vec::swap_remove - index %u >= length %u", index, ln); + } + if index < ln - 1 { + swap(*self, index, ln - 1); + } + self.pop() } #[inline] From 29b0649a6af8c4821f0d69c544569a9529a68431 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Thu, 27 Jun 2013 23:53:37 +1000 Subject: [PATCH 04/10] Convert vec::{push, push_all, push_all_move} to methods. --- doc/rust.md | 2 +- src/librustc/metadata/decoder.rs | 3 +- src/librustc/middle/resolve.rs | 4 +- src/librustc/middle/typeck/coherence.rs | 2 +- src/libstd/os.rs | 5 +- src/libstd/vec.rs | 160 +++++++++++------------- 6 files changed, 82 insertions(+), 94 deletions(-) diff --git a/doc/rust.md b/doc/rust.md index 0939664fc79fa..8f742d0d2100e 100644 --- a/doc/rust.md +++ b/doc/rust.md @@ -2620,7 +2620,7 @@ assert!(b != "world"); The vector type constructor represents a homogeneous array of values of a given type. A vector has a fixed size. -(Operations like `vec::push` operate solely on owned vectors.) +(Operations like `vec.push` operate solely on owned vectors.) A vector type can be annotated with a _definite_ size, written with a trailing asterisk and integer literal, such as `[int * 10]`. Such a definite-sized vector type is a first-class type, since its size is known statically. diff --git a/src/librustc/metadata/decoder.rs b/src/librustc/metadata/decoder.rs index 39cce41b38692..d7c20ed2d50bb 100644 --- a/src/librustc/metadata/decoder.rs +++ b/src/librustc/metadata/decoder.rs @@ -763,8 +763,7 @@ pub fn get_provided_trait_methods(intr: @ident_interner, cdata: cmd, if item_method_sort(mth) != 'p' { loop; } - vec::push(&mut result, - @get_method(intr, cdata, did.node, tcx)); + result.push(@get_method(intr, cdata, did.node, tcx)); } return result; diff --git a/src/librustc/middle/resolve.rs b/src/librustc/middle/resolve.rs index 7cd64e863d2b1..b839e22f9060f 100644 --- a/src/librustc/middle/resolve.rs +++ b/src/librustc/middle/resolve.rs @@ -4862,8 +4862,8 @@ impl Resolver { while j != 0 { j -= 1; for this.value_ribs[j].bindings.each_key |&k| { - vec::push(&mut maybes, this.session.str_of(k)); - vec::push(&mut values, uint::max_value); + maybes.push(this.session.str_of(k)); + values.push(uint::max_value); } } diff --git a/src/librustc/middle/typeck/coherence.rs b/src/librustc/middle/typeck/coherence.rs index bd99a8e150b7c..5d0fbfcb1bae9 100644 --- a/src/librustc/middle/typeck/coherence.rs +++ b/src/librustc/middle/typeck/coherence.rs @@ -788,7 +788,7 @@ impl CoherenceChecker { `%s` to impl", provided_method.method_info .ident.repr(self.crate_context.tcx)); - vec::push(all_methods, provided_method.method_info); + all_methods.push(provided_method.method_info); } } } diff --git a/src/libstd/os.rs b/src/libstd/os.rs index 5534c5befc2b2..165996f935e45 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -1140,7 +1140,7 @@ pub fn set_exit_status(code: int) { unsafe fn load_argc_and_argv(argc: c_int, argv: **c_char) -> ~[~str] { let mut args = ~[]; for uint::range(0, argc as uint) |i| { - vec::push(&mut args, str::raw::from_c_str(*argv.offset(i))); + args.push(str::raw::from_c_str(*argv.offset(i))); } args } @@ -1186,8 +1186,7 @@ pub fn real_args() -> ~[~str] { while *ptr.offset(len) != 0 { len += 1; } // Push it onto the list. - vec::push(&mut args, - vec::raw::buf_as_slice(ptr, len, + args.push(vec::raw::buf_as_slice(ptr, len, str::from_utf16)); } } diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index bc933e70b3733..f0c81f9c04bb6 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -438,86 +438,6 @@ pub fn consume_reverse(mut v: ~[T], f: &fn(uint, v: T)) { } } -/// Append an element to a vector -#[inline] -pub fn push(v: &mut ~[T], initval: T) { - unsafe { - let repr: **raw::VecRepr = transmute(&mut *v); - let fill = (**repr).unboxed.fill; - if (**repr).unboxed.alloc > fill { - push_fast(v, initval); - } - else { - push_slow(v, initval); - } - } -} - -// This doesn't bother to make sure we have space. -#[inline] // really pretty please -unsafe fn push_fast(v: &mut ~[T], initval: T) { - let repr: **mut raw::VecRepr = transmute(v); - let fill = (**repr).unboxed.fill; - (**repr).unboxed.fill += sys::nonzero_size_of::(); - let p = to_unsafe_ptr(&((**repr).unboxed.data)); - let p = ptr::offset(p, fill) as *mut T; - intrinsics::move_val_init(&mut(*p), initval); -} - -#[inline(never)] -fn push_slow(v: &mut ~[T], initval: T) { - let new_len = v.len() + 1; - reserve_at_least(&mut *v, new_len); - unsafe { push_fast(v, initval) } -} - -/// Iterates over the slice `rhs`, copies each element, and then appends it to -/// the vector provided `v`. The `rhs` vector is traversed in-order. -/// -/// # Example -/// -/// ~~~ {.rust} -/// let mut a = ~[1]; -/// vec::push_all(&mut a, [2, 3, 4]); -/// assert!(a == ~[1, 2, 3, 4]); -/// ~~~ -#[inline] -pub fn push_all(v: &mut ~[T], rhs: &const [T]) { - let new_len = v.len() + rhs.len(); - reserve(&mut *v, new_len); - - for uint::range(0u, rhs.len()) |i| { - push(&mut *v, unsafe { raw::get(rhs, i) }) - } -} - -/// Takes ownership of the vector `rhs`, moving all elements into the specified -/// vector `v`. This does not copy any elements, and it is illegal to use the -/// `rhs` vector after calling this method (because it is moved here). -/// -/// # Example -/// -/// ~~~ {.rust} -/// let mut a = ~[~1]; -/// vec::push_all_move(&mut a, ~[~2, ~3, ~4]); -/// assert!(a == ~[~1, ~2, ~3, ~4]); -/// ~~~ -#[inline] -pub fn push_all_move(v: &mut ~[T], mut rhs: ~[T]) { - let new_len = v.len() + rhs.len(); - reserve(&mut *v, new_len); - unsafe { - do as_mut_buf(rhs) |p, len| { - for uint::range(0, len) |i| { - let x = ptr::replace_ptr(ptr::mut_offset(p, i), - intrinsics::uninit()); - push(&mut *v, x); - } - } - raw::set_len(&mut rhs, 0); - } -} - /// Shorten a vector, dropping excess elements. pub fn truncate(v: &mut ~[T], newlen: uint) { do as_mut_buf(*v) |p, oldlen| { @@ -1699,6 +1619,8 @@ impl<'self,T:Copy> ImmutableCopyableVector for &'self [T] { #[allow(missing_doc)] pub trait OwnedVector { fn push(&mut self, t: T); + unsafe fn push_fast(&mut self, t: T); + fn push_all_move(&mut self, rhs: ~[T]); fn pop(&mut self) -> T; fn shift(&mut self) -> T; @@ -1716,14 +1638,67 @@ pub trait OwnedVector { } impl OwnedVector for ~[T] { + /// Append an element to a vector #[inline] fn push(&mut self, t: T) { - push(self, t); + unsafe { + let repr: **raw::VecRepr = transmute(&mut *self); + let fill = (**repr).unboxed.fill; + if (**repr).unboxed.alloc <= fill { + // need more space + reserve_no_inline(self); + } + + self.push_fast(t); + } + + // this peculiar function is because reserve_at_least is very + // large (because of reserve), and will be inlined, which + // makes push too large. + #[inline(never)] + fn reserve_no_inline(v: &mut ~[T]) { + let new_len = v.len() + 1; + reserve_at_least(v, new_len); + } } - #[inline] - fn push_all_move(&mut self, rhs: ~[T]) { - push_all_move(self, rhs); + // This doesn't bother to make sure we have space. + #[inline] // really pretty please + unsafe fn push_fast(&mut self, t: T) { + let repr: **mut raw::VecRepr = transmute(self); + let fill = (**repr).unboxed.fill; + (**repr).unboxed.fill += sys::nonzero_size_of::(); + let p = to_unsafe_ptr(&((**repr).unboxed.data)); + let p = ptr::offset(p, fill) as *mut T; + intrinsics::move_val_init(&mut(*p), t); + } + + /// Takes ownership of the vector `rhs`, moving all elements into + /// the current vector. This does not copy any elements, and it is + /// illegal to use the `rhs` vector after calling this method + /// (because it is moved here). + /// + /// # Example + /// + /// ~~~ {.rust} + /// let mut a = ~[~1]; + /// a.push_all_move(~[~2, ~3, ~4]); + /// assert!(a == ~[~1, ~2, ~3, ~4]); + /// ~~~ + #[inline] + fn push_all_move(&mut self, mut rhs: ~[T]) { + let new_len = self.len() + rhs.len(); + reserve(self, new_len); + unsafe { + do as_mut_buf(rhs) |p, len| { + for uint::range(0, len) |i| { + let x = ptr::replace_ptr(ptr::mut_offset(p, i), + intrinsics::uninit()); + self.push(x); + } + } + raw::set_len(&mut rhs, 0); + } } /// Remove the last element from a vector and return it @@ -1898,9 +1873,24 @@ pub trait OwnedCopyableVector { } impl OwnedCopyableVector for ~[T] { + /// Iterates over the slice `rhs`, copies each element, and then appends it to + /// the vector provided `v`. The `rhs` vector is traversed in-order. + /// + /// # Example + /// + /// ~~~ {.rust} + /// let mut a = ~[1]; + /// a.push_all([2, 3, 4]); + /// assert!(a == ~[1, 2, 3, 4]); + /// ~~~ #[inline] fn push_all(&mut self, rhs: &const [T]) { - push_all(self, rhs); + let new_len = self.len() + rhs.len(); + reserve(self, new_len); + + for uint::range(0u, rhs.len()) |i| { + self.push(unsafe { raw::get(rhs, i) }) + } } #[inline] From 4470d14388b1637a1e4862c0650baddf6ed7c430 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Thu, 27 Jun 2013 23:58:07 +1000 Subject: [PATCH 05/10] Convert vec::truncate to a method. --- src/libstd/vec.rs | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index f0c81f9c04bb6..2cc2786120766 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -438,20 +438,6 @@ pub fn consume_reverse(mut v: ~[T], f: &fn(uint, v: T)) { } } -/// Shorten a vector, dropping excess elements. -pub fn truncate(v: &mut ~[T], newlen: uint) { - do as_mut_buf(*v) |p, oldlen| { - assert!(newlen <= oldlen); - unsafe { - // This loop is optimized out for non-drop types. - for uint::range(newlen, oldlen) |i| { - ptr::replace_ptr(ptr::mut_offset(p, i), intrinsics::uninit()); - } - } - } - unsafe { raw::set_len(&mut *v, newlen); } -} - /** * Remove consecutive repeated elements from a vector; if the vector is * sorted, this removes all duplicates. @@ -1820,9 +1806,18 @@ impl OwnedVector for ~[T] { self.pop() } - #[inline] + /// Shorten a vector, dropping excess elements. fn truncate(&mut self, newlen: uint) { - truncate(self, newlen); + do as_mut_buf(*self) |p, oldlen| { + assert!(newlen <= oldlen); + unsafe { + // This loop is optimized out for non-drop types. + for uint::range(newlen, oldlen) |i| { + ptr::replace_ptr(ptr::mut_offset(p, i), intrinsics::uninit()); + } + } + } + unsafe { raw::set_len(self, newlen); } } #[inline] From 206d4f00dc628cf0c7680ae2dc5b3ab6771c32e4 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Fri, 28 Jun 2013 00:01:21 +1000 Subject: [PATCH 06/10] Convert vec::retain to a method. --- src/libstd/vec.rs | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 2cc2786120766..65c394f032c57 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -687,26 +687,6 @@ pub fn filtered(v: &[T], f: &fn(t: &T) -> bool) -> ~[T] { result } -/** - * Like `filter()`, but in place. Preserves order of `v`. Linear time. - */ -pub fn retain(v: &mut ~[T], f: &fn(t: &T) -> bool) { - let len = v.len(); - let mut deleted: uint = 0; - - for uint::range(0, len) |i| { - if !f(&v[i]) { - deleted += 1; - } else if deleted > 0 { - swap(*v, i - deleted, i); - } - } - - if deleted > 0 { - v.truncate(len - deleted); - } -} - /// Flattens a vector of vectors of T into a single vector of T. pub fn concat(v: &[~[T]]) -> ~[T] { v.concat_vec() } @@ -1820,9 +1800,25 @@ impl OwnedVector for ~[T] { unsafe { raw::set_len(self, newlen); } } - #[inline] + + /** + * Like `filter()`, but in place. Preserves order of `v`. Linear time. + */ fn retain(&mut self, f: &fn(t: &T) -> bool) { - retain(self, f); + let len = self.len(); + let mut deleted: uint = 0; + + for uint::range(0, len) |i| { + if !f(&self[i]) { + deleted += 1; + } else if deleted > 0 { + swap(*self, i - deleted, i); + } + } + + if deleted > 0 { + self.truncate(len - deleted); + } } #[inline] From ae2f1853491540b9e70be2209b235f6c920706a8 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Fri, 28 Jun 2013 00:10:18 +1000 Subject: [PATCH 07/10] Convert vec::{partition, partitioned} to methods. --- src/libextra/test.rs | 2 +- src/libstd/vec.rs | 75 +++++++++++++++++--------------------------- 2 files changed, 29 insertions(+), 48 deletions(-) diff --git a/src/libextra/test.rs b/src/libextra/test.rs index bb03e3ab9bb27..7b68298a8dddd 100644 --- a/src/libextra/test.rs +++ b/src/libextra/test.rs @@ -431,7 +431,7 @@ fn run_tests(opts: &TestOpts, callback(TeFiltered(filtered_descs)); let (filtered_tests, filtered_benchs) = - do vec::partition(filtered_tests) |e| { + do filtered_tests.partition |e| { match e.testfn { StaticTestFn(_) | DynTestFn(_) => true, StaticBenchFn(_) | DynBenchFn(_) => false diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 65c394f032c57..4dbc0c4e3e04e 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -337,46 +337,6 @@ pub fn rsplitn(v: &[T], n: uint, f: &fn(t: &T) -> bool) -> ~[~[T]] { result } -/** - * Partitions a vector into two new vectors: those that satisfies the - * predicate, and those that do not. - */ -pub fn partition(v: ~[T], f: &fn(&T) -> bool) -> (~[T], ~[T]) { - let mut lefts = ~[]; - let mut rights = ~[]; - - // FIXME (#4355 maybe): using v.consume here crashes - // do v.consume |_, elt| { - do consume(v) |_, elt| { - if f(&elt) { - lefts.push(elt); - } else { - rights.push(elt); - } - } - - (lefts, rights) -} - -/** - * Partitions a vector into two new vectors: those that satisfies the - * predicate, and those that do not. - */ -pub fn partitioned(v: &[T], f: &fn(&T) -> bool) -> (~[T], ~[T]) { - let mut lefts = ~[]; - let mut rights = ~[]; - - for v.iter().advance |elt| { - if f(elt) { - lefts.push(copy *elt); - } else { - rights.push(copy *elt); - } - } - - (lefts, rights) -} - /// Consumes all elements, in a vector, moving them out into the / closure /// provided. The vector is traversed from the start to the end. /// @@ -1572,7 +1532,18 @@ impl<'self,T:Copy> ImmutableCopyableVector for &'self [T] { */ #[inline] fn partitioned(&self, f: &fn(&T) -> bool) -> (~[T], ~[T]) { - partitioned(*self, f) + let mut lefts = ~[]; + let mut rights = ~[]; + + for self.iter().advance |elt| { + if f(elt) { + lefts.push(copy *elt); + } else { + rights.push(copy *elt); + } + } + + (lefts, rights) } /// Returns the element at the given index, without doing bounds checking. @@ -1842,7 +1813,18 @@ impl OwnedVector for ~[T] { */ #[inline] fn partition(self, f: &fn(&T) -> bool) -> (~[T], ~[T]) { - partition(self, f) + let mut lefts = ~[]; + let mut rights = ~[]; + + do self.consume |_, elt| { + if f(&elt) { + lefts.push(elt); + } else { + rights.push(elt); + } + } + + (lefts, rights) } #[inline] @@ -3228,11 +3210,10 @@ mod tests { #[test] fn test_partition() { - // FIXME (#4355 maybe): using v.partition here crashes - assert_eq!(partition(~[], |x: &int| *x < 3), (~[], ~[])); - assert_eq!(partition(~[1, 2, 3], |x: &int| *x < 4), (~[1, 2, 3], ~[])); - assert_eq!(partition(~[1, 2, 3], |x: &int| *x < 2), (~[1], ~[2, 3])); - assert_eq!(partition(~[1, 2, 3], |x: &int| *x < 0), (~[], ~[1, 2, 3])); + assert_eq!((~[]).partition(|x: &int| *x < 3), (~[], ~[])); + assert_eq!((~[1, 2, 3]).partition(|x: &int| *x < 4), (~[1, 2, 3], ~[])); + assert_eq!((~[1, 2, 3]).partition(|x: &int| *x < 2), (~[1], ~[2, 3])); + assert_eq!((~[1, 2, 3]).partition(|x: &int| *x < 0), (~[], ~[1, 2, 3])); } #[test] From 32d655916f1c3365a521616b57d9d0efc2bae643 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Fri, 28 Jun 2013 00:40:47 +1000 Subject: [PATCH 08/10] Convert vec::{reserve, reserve_at_least, capacity} to methods. --- src/libextra/deque.rs | 13 +- src/libextra/priority_queue.rs | 6 +- src/libstd/io.rs | 2 +- src/libstd/rt/io/extensions.rs | 2 +- src/libstd/str.rs | 6 +- src/libstd/vec.rs | 142 +++++++++--------- src/test/bench/shootout-reverse-complement.rs | 7 +- 7 files changed, 88 insertions(+), 90 deletions(-) diff --git a/src/libextra/deque.rs b/src/libextra/deque.rs index e6a7dd6483730..c70c87b6ea13b 100644 --- a/src/libextra/deque.rs +++ b/src/libextra/deque.rs @@ -137,7 +137,7 @@ impl Deque { /// /// * n - The number of elements to reserve space for pub fn reserve(&mut self, n: uint) { - vec::reserve(&mut self.elts, n); + self.elts.reserve(n); } /// Reserve capacity for at least `n` elements in the given deque, @@ -151,7 +151,7 @@ impl Deque { /// /// * n - The number of elements to reserve space for pub fn reserve_at_least(&mut self, n: uint) { - vec::reserve_at_least(&mut self.elts, n); + self.elts.reserve_at_least(n); } /// Front-to-back iterator. @@ -256,7 +256,6 @@ mod tests { use super::*; use core::cmp::Eq; use core::kinds::Copy; - use core::vec::capacity; use core; #[test] @@ -442,11 +441,11 @@ mod tests { let mut d = Deque::new(); d.add_back(0u64); d.reserve(50); - assert_eq!(capacity(&mut d.elts), 50); + assert_eq!(d.elts.capacity(), 50); let mut d = Deque::new(); d.add_back(0u32); d.reserve(50); - assert_eq!(capacity(&mut d.elts), 50); + assert_eq!(d.elts.capacity(), 50); } #[test] @@ -454,11 +453,11 @@ mod tests { let mut d = Deque::new(); d.add_back(0u64); d.reserve_at_least(50); - assert_eq!(capacity(&mut d.elts), 64); + assert_eq!(d.elts.capacity(), 64); let mut d = Deque::new(); d.add_back(0u32); d.reserve_at_least(50); - assert_eq!(capacity(&mut d.elts), 64); + assert_eq!(d.elts.capacity(), 64); } #[test] diff --git a/src/libextra/priority_queue.rs b/src/libextra/priority_queue.rs index af891edf9e5fd..fbb4be0febb3d 100644 --- a/src/libextra/priority_queue.rs +++ b/src/libextra/priority_queue.rs @@ -52,12 +52,12 @@ impl PriorityQueue { } /// Returns the number of elements the queue can hold without reallocating - pub fn capacity(&self) -> uint { vec::capacity(&self.data) } + pub fn capacity(&self) -> uint { self.data.capacity() } - pub fn reserve(&mut self, n: uint) { vec::reserve(&mut self.data, n) } + pub fn reserve(&mut self, n: uint) { self.data.reserve(n) } pub fn reserve_at_least(&mut self, n: uint) { - vec::reserve_at_least(&mut self.data, n) + self.data.reserve_at_least(n) } /// Pop the greatest item from the queue - fails if empty diff --git a/src/libstd/io.rs b/src/libstd/io.rs index 4d9c08f25da74..36920bd248879 100644 --- a/src/libstd/io.rs +++ b/src/libstd/io.rs @@ -1658,7 +1658,7 @@ impl Writer for BytesWriter { let bytes = &mut *self.bytes; let count = uint::max(bytes.len(), *self.pos + v_len); - vec::reserve(bytes, count); + bytes.reserve(count); unsafe { vec::raw::set_len(bytes, count); diff --git a/src/libstd/rt/io/extensions.rs b/src/libstd/rt/io/extensions.rs index 5320bd0f42ee1..1f82a9cd96335 100644 --- a/src/libstd/rt/io/extensions.rs +++ b/src/libstd/rt/io/extensions.rs @@ -292,7 +292,7 @@ impl ReaderUtil for T { let start_len = buf.len(); let mut total_read = 0; - vec::reserve_at_least(buf, start_len + len); + buf.reserve_at_least(start_len + len); vec::raw::set_len(buf, start_len + len); do (|| { diff --git a/src/libstd/str.rs b/src/libstd/str.rs index 16c287c1da823..58cdc6631f0b5 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -2081,7 +2081,7 @@ impl OwnedStr for ~str { pub fn reserve(&mut self, n: uint) { unsafe { let v: *mut ~[u8] = cast::transmute(self); - vec::reserve(&mut *v, n + 1); + (*v).reserve(n + 1); } } @@ -2115,8 +2115,8 @@ impl OwnedStr for ~str { * reallocating */ fn capacity(&self) -> uint { - let buf: &const ~[u8] = unsafe { cast::transmute(self) }; - let vcap = vec::capacity(buf); + let buf: &~[u8] = unsafe { cast::transmute(self) }; + let vcap = buf.capacity(); assert!(vcap > 0u); vcap - 1u } diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 4dbc0c4e3e04e..5dfea811c2331 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -68,63 +68,6 @@ pub fn same_length(xs: &const [T], ys: &const [U]) -> bool { xs.len() == ys.len() } -/** - * Reserves capacity for exactly `n` elements in the given vector. - * - * If the capacity for `v` is already equal to or greater than the requested - * capacity, then no action is taken. - * - * # Arguments - * - * * v - A vector - * * n - The number of elements to reserve space for - */ -#[inline] -pub fn reserve(v: &mut ~[T], n: uint) { - // Only make the (slow) call into the runtime if we have to - use managed; - if capacity(v) < n { - unsafe { - let ptr: **raw::VecRepr = cast::transmute(v); - let td = get_tydesc::(); - if ((**ptr).box_header.ref_count == - managed::raw::RC_MANAGED_UNIQUE) { - rustrt::vec_reserve_shared_actual(td, ptr, n as libc::size_t); - } else { - rustrt::vec_reserve_shared(td, ptr, n as libc::size_t); - } - } - } -} - -/** - * Reserves capacity for at least `n` elements in the given vector. - * - * This function will over-allocate in order to amortize the allocation costs - * in scenarios where the caller may need to repeatedly reserve additional - * space. - * - * If the capacity for `v` is already equal to or greater than the requested - * capacity, then no action is taken. - * - * # Arguments - * - * * v - A vector - * * n - The number of elements to reserve space for - */ -pub fn reserve_at_least(v: &mut ~[T], n: uint) { - reserve(v, uint::next_power_of_two(n)); -} - -/// Returns the number of elements the vector can hold without reallocating -#[inline] -pub fn capacity(v: &const ~[T]) -> uint { - unsafe { - let repr: **raw::VecRepr = transmute(v); - (**repr).unboxed.alloc / sys::nonzero_size_of::() - } -} - /** * Creates and initializes an owned vector. * @@ -179,7 +122,7 @@ pub fn to_owned(t: &[T]) -> ~[T] { /// Creates a new vector with a capacity of `capacity` pub fn with_capacity(capacity: uint) -> ~[T] { let mut vec = ~[]; - reserve(&mut vec, capacity); + vec.reserve(capacity); vec } @@ -466,7 +409,7 @@ pub fn append_one(lhs: ~[T], x: T) -> ~[T] { */ pub fn grow(v: &mut ~[T], n: uint, initval: &T) { let new_len = v.len() + n; - reserve_at_least(&mut *v, new_len); + v.reserve_at_least(new_len); let mut i: uint = 0u; while i < n { @@ -490,7 +433,7 @@ pub fn grow(v: &mut ~[T], n: uint, initval: &T) { */ pub fn grow_fn(v: &mut ~[T], n: uint, op: &fn(uint) -> T) { let new_len = v.len() + n; - reserve_at_least(&mut *v, new_len); + v.reserve_at_least(new_len); let mut i: uint = 0u; while i < n { v.push(op(i)); @@ -1298,13 +1241,11 @@ impl<'self,T:Copy> CopyableVector for &'self [T] { /// Returns a copy of `v`. #[inline] fn to_owned(&self) -> ~[T] { - let mut result = ~[]; - reserve(&mut result, self.len()); + let mut result = with_capacity(self.len()); for self.iter().advance |e| { result.push(copy *e); } result - } } @@ -1555,6 +1496,10 @@ impl<'self,T:Copy> ImmutableCopyableVector for &'self [T] { #[allow(missing_doc)] pub trait OwnedVector { + fn reserve(&mut self, n: uint); + fn reserve_at_least(&mut self, n: uint); + fn capacity(&self) -> uint; + fn push(&mut self, t: T); unsafe fn push_fast(&mut self, t: T); @@ -1575,6 +1520,61 @@ pub trait OwnedVector { } impl OwnedVector for ~[T] { + /** + * Reserves capacity for exactly `n` elements in the given vector. + * + * If the capacity for `self` is already equal to or greater than the requested + * capacity, then no action is taken. + * + * # Arguments + * + * * n - The number of elements to reserve space for + */ + #[inline] + fn reserve(&mut self, n: uint) { + // Only make the (slow) call into the runtime if we have to + use managed; + if self.capacity() < n { + unsafe { + let ptr: **raw::VecRepr = cast::transmute(self); + let td = get_tydesc::(); + if ((**ptr).box_header.ref_count == + managed::raw::RC_MANAGED_UNIQUE) { + rustrt::vec_reserve_shared_actual(td, ptr, n as libc::size_t); + } else { + rustrt::vec_reserve_shared(td, ptr, n as libc::size_t); + } + } + } + } + + /** + * Reserves capacity for at least `n` elements in the given vector. + * + * This function will over-allocate in order to amortize the allocation costs + * in scenarios where the caller may need to repeatedly reserve additional + * space. + * + * If the capacity for `self` is already equal to or greater than the requested + * capacity, then no action is taken. + * + * # Arguments + * + * * n - The number of elements to reserve space for + */ + fn reserve_at_least(&mut self, n: uint) { + self.reserve(uint::next_power_of_two(n)); + } + + /// Returns the number of elements the vector can hold without reallocating. + #[inline] + fn capacity(&self) -> uint { + unsafe { + let repr: **raw::VecRepr = transmute(self); + (**repr).unboxed.alloc / sys::nonzero_size_of::() + } + } + /// Append an element to a vector #[inline] fn push(&mut self, t: T) { @@ -1595,7 +1595,7 @@ impl OwnedVector for ~[T] { #[inline(never)] fn reserve_no_inline(v: &mut ~[T]) { let new_len = v.len() + 1; - reserve_at_least(v, new_len); + v.reserve_at_least(new_len); } } @@ -1625,7 +1625,7 @@ impl OwnedVector for ~[T] { #[inline] fn push_all_move(&mut self, mut rhs: ~[T]) { let new_len = self.len() + rhs.len(); - reserve(self, new_len); + self.reserve(new_len); unsafe { do as_mut_buf(rhs) |p, len| { for uint::range(0, len) |i| { @@ -1672,7 +1672,7 @@ impl OwnedVector for ~[T] { // Save the last element. We're going to overwrite its position let work_elt = self.pop(); // We still should have room to work where what last element was - assert!(capacity(self) >= ln); + assert!(self.capacity() >= ln); // Pretend like we have the original length so we can use // the vector copy_memory to overwrite the hole we just made raw::set_len(self, ln); @@ -1859,7 +1859,7 @@ impl OwnedCopyableVector for ~[T] { #[inline] fn push_all(&mut self, rhs: &const [T]) { let new_len = self.len() + rhs.len(); - reserve(self, new_len); + self.reserve(new_len); for uint::range(0u, rhs.len()) |i| { self.push(unsafe { raw::get(rhs, i) }) @@ -3333,11 +3333,11 @@ mod tests { #[test] fn test_capacity() { let mut v = ~[0u64]; - reserve(&mut v, 10u); - assert_eq!(capacity(&v), 10u); + v.reserve(10u); + assert_eq!(v.capacity(), 10u); let mut v = ~[0u32]; - reserve(&mut v, 10u); - assert_eq!(capacity(&v), 10u); + v.reserve(10u); + assert_eq!(v.capacity(), 10u); } #[test] diff --git a/src/test/bench/shootout-reverse-complement.rs b/src/test/bench/shootout-reverse-complement.rs index 9893785ecfa22..e57dee06c75bd 100644 --- a/src/test/bench/shootout-reverse-complement.rs +++ b/src/test/bench/shootout-reverse-complement.rs @@ -5,7 +5,6 @@ use std::cast::transmute; use std::libc::{STDOUT_FILENO, c_int, fdopen, fgets, fopen, fputc, fwrite}; use std::libc::{size_t}; use std::ptr::null; -use std::vec::{capacity, reserve, reserve_at_least}; use std::vec::raw::set_len; static LINE_LEN: u32 = 80; @@ -103,13 +102,13 @@ fn main() { let stdout = fdopen(STDOUT_FILENO as c_int, transmute(&mode[0])); let mut out: ~[u8] = ~[]; - reserve(&mut out, 12777888); + out.reserve(12777888); let mut pos = 0; loop { let needed = pos + (LINE_LEN as uint) + 1; - if capacity(&out) < needed { - reserve_at_least(&mut out, needed); + if out.capacity() < needed { + out.reserve_at_least(needed); } let mut ptr = out.unsafe_mut_ref(pos); From d8087cae442ba3ca5070c7f3865e798f3786d28c Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Fri, 28 Jun 2013 00:50:48 +1000 Subject: [PATCH 09/10] extra: silence some test warnings. --- src/libextra/arc.rs | 1 - src/libextra/c_vec.rs | 3 +-- src/libextra/dlist.rs | 2 -- src/libextra/net_tcp.rs | 20 +++++--------------- src/libextra/sync.rs | 1 - src/libextra/uv_global_loop.rs | 34 +++++++++++++++------------------- src/libextra/uv_ll.rs | 4 +--- 7 files changed, 22 insertions(+), 43 deletions(-) diff --git a/src/libextra/arc.rs b/src/libextra/arc.rs index 8e4a45fadd33e..c076b7f8fbe06 100644 --- a/src/libextra/arc.rs +++ b/src/libextra/arc.rs @@ -517,7 +517,6 @@ mod tests { use arc::*; - use core::vec; use core::cell::Cell; use core::comm; use core::task; diff --git a/src/libextra/c_vec.rs b/src/libextra/c_vec.rs index 84593630cabd7..79ef5bf2b7eaf 100644 --- a/src/libextra/c_vec.rs +++ b/src/libextra/c_vec.rs @@ -159,8 +159,7 @@ mod tests { assert!(mem as int != 0); - return c_vec_with_dtor(mem as *mut u8, n as uint, - || unsafe { free(mem) }); + c_vec_with_dtor(mem as *mut u8, n as uint, || free(mem)) } } diff --git a/src/libextra/dlist.rs b/src/libextra/dlist.rs index 1767aa8c39725..ee86340e47bed 100644 --- a/src/libextra/dlist.rs +++ b/src/libextra/dlist.rs @@ -526,8 +526,6 @@ mod tests { use super::*; - use core::vec; - #[test] fn test_dlist_concat() { let a = from_vec([1,2]); diff --git a/src/libextra/net_tcp.rs b/src/libextra/net_tcp.rs index f8ecac373a6c7..f3f6ffde6603e 100644 --- a/src/libextra/net_tcp.rs +++ b/src/libextra/net_tcp.rs @@ -1457,33 +1457,23 @@ mod test { #[test] fn test_gl_tcp_server_and_client_ipv4() { - unsafe { - impl_gl_tcp_ipv4_server_and_client(); - } + impl_gl_tcp_ipv4_server_and_client(); } #[test] fn test_gl_tcp_get_peer_addr() { - unsafe { - impl_gl_tcp_ipv4_get_peer_addr(); - } + impl_gl_tcp_ipv4_get_peer_addr(); } #[test] fn test_gl_tcp_ipv4_client_error_connection_refused() { - unsafe { - impl_gl_tcp_ipv4_client_error_connection_refused(); - } + impl_gl_tcp_ipv4_client_error_connection_refused(); } #[test] fn test_gl_tcp_server_address_in_use() { - unsafe { - impl_gl_tcp_ipv4_server_address_in_use(); - } + impl_gl_tcp_ipv4_server_address_in_use(); } #[test] fn test_gl_tcp_server_access_denied() { - unsafe { - impl_gl_tcp_ipv4_server_access_denied(); - } + impl_gl_tcp_ipv4_server_access_denied(); } // Strange failure on Windows. --pcwalton #[test] diff --git a/src/libextra/sync.rs b/src/libextra/sync.rs index 8cfe39c5ef25e..817e1ab122610 100644 --- a/src/libextra/sync.rs +++ b/src/libextra/sync.rs @@ -808,7 +808,6 @@ mod tests { use core::comm; use core::result; use core::task; - use core::vec; /************************************************************************ * Semaphore tests diff --git a/src/libextra/uv_global_loop.rs b/src/libextra/uv_global_loop.rs index 286863bef641d..f1dde1b8cb41d 100644 --- a/src/libextra/uv_global_loop.rs +++ b/src/libextra/uv_global_loop.rs @@ -150,9 +150,7 @@ mod test { let hl_loop = &get_gl(); do iotask::interact(hl_loop) |_loop_ptr| { debug!(~"closing timer"); - unsafe { - ll::close(timer_ptr, simple_timer_close_cb); - } + ll::close(timer_ptr, simple_timer_close_cb); debug!(~"about to deref exit_ch_ptr"); debug!(~"after msg sent on deref'd exit_ch"); }; @@ -169,24 +167,22 @@ mod test { let timer_handle = ll::timer_t(); let timer_ptr: *ll::uv_timer_t = &timer_handle; do iotask::interact(iotask) |loop_ptr| { - unsafe { - debug!(~"user code inside interact loop!!!"); - let init_status = ll::timer_init(loop_ptr, timer_ptr); - if(init_status == 0i32) { - ll::set_data_for_uv_handle( - timer_ptr as *libc::c_void, - exit_ch_ptr); - let start_status = ll::timer_start(timer_ptr, - simple_timer_cb, - 1u, 0u); - if(start_status != 0i32) { - fail!("failure on ll::timer_start()"); - } - } - else { - fail!("failure on ll::timer_init()"); + debug!(~"user code inside interact loop!!!"); + let init_status = ll::timer_init(loop_ptr, timer_ptr); + if(init_status == 0i32) { + ll::set_data_for_uv_handle( + timer_ptr as *libc::c_void, + exit_ch_ptr); + let start_status = ll::timer_start(timer_ptr, + simple_timer_cb, + 1u, 0u); + if(start_status != 0i32) { + fail!("failure on ll::timer_start()"); } } + else { + fail!("failure on ll::timer_init()"); + } }; exit_po.recv(); debug!( diff --git a/src/libextra/uv_ll.rs b/src/libextra/uv_ll.rs index 744f4555d5cbd..58b477d4ccf46 100644 --- a/src/libextra/uv_ll.rs +++ b/src/libextra/uv_ll.rs @@ -1767,9 +1767,7 @@ mod test { mod impl64 { #[test] fn test_uv_ll_tcp_server_and_request() { - unsafe { - super::super::impl_uv_tcp_server_and_request(); - } + super::super::impl_uv_tcp_server_and_request(); } } #[cfg(target_arch="x86")] From 366ca44cc8f79704f8781adb15e74d3c2a0e5572 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Fri, 28 Jun 2013 01:45:24 +1000 Subject: [PATCH 10/10] std: silence some test warnings. --- src/libstd/iterator.rs | 1 - src/libstd/local_data.rs | 23 ++++++++++------------- src/libstd/rt/uv/timer.rs | 4 ++-- src/libstd/str.rs | 3 ++- src/libstd/task/mod.rs | 14 ++++++-------- src/libstd/vec.rs | 4 ++-- 6 files changed, 22 insertions(+), 27 deletions(-) diff --git a/src/libstd/iterator.rs b/src/libstd/iterator.rs index ab433a9a79d0c..7de02a9f815bf 100644 --- a/src/libstd/iterator.rs +++ b/src/libstd/iterator.rs @@ -980,7 +980,6 @@ mod tests { use super::*; use prelude::*; - use iter; use uint; #[test] diff --git a/src/libstd/local_data.rs b/src/libstd/local_data.rs index 82c01c998cf1e..33b4e3f1963a9 100644 --- a/src/libstd/local_data.rs +++ b/src/libstd/local_data.rs @@ -92,14 +92,12 @@ fn test_tls_multitask() { fn my_key(_x: @~str) { } local_data_set(my_key, @~"parent data"); do task::spawn { - unsafe { - // TLS shouldn't carry over. - assert!(local_data_get(my_key).is_none()); - local_data_set(my_key, @~"child data"); - assert!(*(local_data_get(my_key).get()) == + // TLS shouldn't carry over. + assert!(local_data_get(my_key).is_none()); + local_data_set(my_key, @~"child data"); + assert!(*(local_data_get(my_key).get()) == ~"child data"); - // should be cleaned up for us - } + // should be cleaned up for us } // Must work multiple times assert!(*(local_data_get(my_key).get()) == ~"parent data"); @@ -206,12 +204,11 @@ fn test_tls_cleanup_on_failure() { local_data_set(str_key, @~"parent data"); local_data_set(box_key, @@()); do task::spawn { - unsafe { // spawn_linked - local_data_set(str_key, @~"string data"); - local_data_set(box_key, @@()); - local_data_set(int_key, @42); - fail!(); - } + // spawn_linked + local_data_set(str_key, @~"string data"); + local_data_set(box_key, @@()); + local_data_set(int_key, @42); + fail!(); } // Not quite nondeterministic. local_data_set(int_key, @31337); diff --git a/src/libstd/rt/uv/timer.rs b/src/libstd/rt/uv/timer.rs index cd6fc5c0a250e..14465eb7dfd3a 100644 --- a/src/libstd/rt/uv/timer.rs +++ b/src/libstd/rt/uv/timer.rs @@ -160,14 +160,14 @@ mod test { let mut timer2 = TimerWatcher::new(&mut loop_); do timer2.start(10, 0) |timer2, _| { - unsafe { *count_ptr += 1; } + *count_ptr += 1; timer2.close(||()); // Restart the original timer let mut timer = timer; do timer.start(1, 0) |timer, _| { - unsafe { *count_ptr += 1; } + *count_ptr += 1; timer.close(||()); } } diff --git a/src/libstd/str.rs b/src/libstd/str.rs index 58cdc6631f0b5..b4292a30541da 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -2249,7 +2249,7 @@ mod tests { assert!("" <= ""); assert!("" <= "foo"); assert!("foo" <= "foo"); - assert!("foo" != ~"bar"); + assert!("foo" != "bar"); } #[test] @@ -3156,6 +3156,7 @@ mod tests { #[test] fn test_add() { + #[allow(unnecessary_allocation)]; macro_rules! t ( ($s1:expr, $s2:expr, $e:expr) => { assert_eq!($s1 + $s2, $e); diff --git a/src/libstd/task/mod.rs b/src/libstd/task/mod.rs index 223afbce091b8..b558b9d53a3cc 100644 --- a/src/libstd/task/mod.rs +++ b/src/libstd/task/mod.rs @@ -934,17 +934,15 @@ fn test_spawn_sched_blocking() { let lock = testrt::rust_dbg_lock_create(); do spawn_sched(SingleThreaded) { - unsafe { - testrt::rust_dbg_lock_lock(lock); + testrt::rust_dbg_lock_lock(lock); - start_ch.send(()); + start_ch.send(()); - // Block the scheduler thread - testrt::rust_dbg_lock_wait(lock); - testrt::rust_dbg_lock_unlock(lock); + // Block the scheduler thread + testrt::rust_dbg_lock_wait(lock); + testrt::rust_dbg_lock_unlock(lock); - fin_ch.send(()); - } + fin_ch.send(()); }; // Wait until the other task has its lock diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 5dfea811c2331..8cbd9309cc6b1 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -3861,11 +3861,11 @@ mod tests { fn test_vec_zero() { use num::Zero; macro_rules! t ( - ($ty:ty) => { + ($ty:ty) => {{ let v: $ty = Zero::zero(); assert!(v.is_empty()); assert!(v.is_zero()); - } + }} ); t!(&[int]);