Skip to content

Commit

Permalink
auto merge of #4969 : nickdesaulniers/rust/issue3869, r=brson
Browse files Browse the repository at this point in the history
Issue #3869
review? @nikomatsakis 

Convert all uses of vec::slice to vec::view Issue #3869
Rename const_view to const_slice
Renamed mut_view to mut_slice
Fix windows build error.  `buf` is borrowed by the call to
`as_mut_buf()` and so we must invoke `slice()` outside of that
call.
  • Loading branch information
bors committed Feb 15, 2013
2 parents 0528329 + 91af527 commit 566bcf2
Show file tree
Hide file tree
Showing 21 changed files with 130 additions and 124 deletions.
3 changes: 2 additions & 1 deletion src/compiletest/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,8 @@ fn make_run_args(config: config, _props: TestProps, testfile: &Path) ->
};

let args = toolargs + ~[make_exe_name(config, testfile).to_str()];
return ProcArgs {prog: args[0], args: vec::slice(args, 1, args.len())};
return ProcArgs {prog: args[0],
args: vec::slice(args, 1, args.len()).to_vec()};
}

fn split_maybe_args(argstr: Option<~str>) -> ~[~str] {
Expand Down
2 changes: 1 addition & 1 deletion src/libcargo/cargo.rc
Original file line number Diff line number Diff line change
Expand Up @@ -1553,7 +1553,7 @@ pub fn cmd_list(c: &Cargo) {
sync(c);

if vec::len(c.opts.free) >= 3u {
let v = vec::view(c.opts.free, 2u, vec::len(c.opts.free));
let v = vec::slice(c.opts.free, 2u, vec::len(c.opts.free));
for vec::each(v) |name| {
if !valid_pkg_name(*name) {
error(fmt!("'%s' is an invalid source name", *name));
Expand Down
6 changes: 3 additions & 3 deletions src/libcore/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ impl<T: Reader> 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());
bytes = vec::slice(bytes, offset, bytes.len()).to_vec();
}
}
chars
Expand Down Expand Up @@ -531,7 +531,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::view(self.bytes, self.pos, self.bytes.len());
let view = vec::slice(self.bytes, self.pos, self.bytes.len());
vec::bytes::copy_memory(bytes, view, count);
self.pos += count;
Expand Down Expand Up @@ -1008,7 +1008,7 @@ impl Writer for BytesWriter {
unsafe { vec::raw::set_len(&mut bytes, count); }
{
let view = vec::mut_view(bytes, self.pos, count);
let view = vec::mut_slice(bytes, self.pos, count);
vec::bytes::copy_memory(view, v, v_len);
}
Expand Down
11 changes: 7 additions & 4 deletions src/libcore/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,24 @@ pub mod win32 {
let mut res = None;
let mut done = false;
while !done {
let mut buf = vec::from_elem(n as uint, 0u16);
let mut k: DWORD = 0;
let buf = vec::cast_to_mut(vec::from_elem(n as uint, 0u16));
do vec::as_mut_buf(buf) |b, _sz| {
let k : DWORD = f(b, TMPBUF_SZ as DWORD);
k = f(b, TMPBUF_SZ as DWORD);
if k == (0 as DWORD) {
done = true;
} else if (k == n &&
libc::GetLastError() ==
libc::ERROR_INSUFFICIENT_BUFFER as DWORD) {
n *= (2 as DWORD);
} else {
let sub = vec::slice(buf, 0u, k as uint);
res = option::Some(str::from_utf16(sub));
done = true;
}
}
if k != 0 && done {
let sub = vec::slice(buf, 0u, k as uint);
res = option::Some(str::from_utf16(sub));
}
}
return res;
}
Expand Down
124 changes: 59 additions & 65 deletions src/libcore/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,21 +220,21 @@ pub pure fn head<T: Copy>(v: &[const T]) -> T { v[0] }

/// Returns a vector containing all but the first element of a slice
pub pure fn tail<T: Copy>(v: &[const T]) -> ~[T] {
return slice(v, 1u, len(v));
slice(v, 1u, len(v)).to_vec()
}

/**
* Returns a vector containing all but the first `n` \
* elements of a slice
*/
pub pure fn tailn<T: Copy>(v: &[const T], n: uint) -> ~[T] {
slice(v, n, len(v))
slice(v, n, len(v)).to_vec()
}

/// Returns a vector containing all but the last element of a slice
pub pure fn init<T: Copy>(v: &[const T]) -> ~[T] {
assert len(v) != 0u;
slice(v, 0u, len(v) - 1u)
slice(v, 0u, len(v) - 1u).to_vec()
}

/// Returns the last element of the slice `v`, failing if the slice is empty.
Expand All @@ -252,20 +252,9 @@ pub pure fn last_opt<T: Copy>(v: &[const T]) -> Option<T> {
Some(v[len(v) - 1u])
}
/// Returns a copy of the elements from [`start`..`end`) from `v`.
pub pure fn slice<T: Copy>(v: &[const T], start: uint, end: uint) -> ~[T] {
assert (start <= end);
assert (end <= len(v));
let mut result = ~[];
unsafe {
for uint::range(start, end) |i| { result.push(v[i]) }
}
result
}
/// Return a slice that points into another slice.
#[inline(always)]
pub pure fn view<T>(v: &r/[T], start: uint, end: uint) -> &r/[T] {
pub pure fn slice<T>(v: &r/[T], start: uint, end: uint) -> &r/[T] {
assert (start <= end);
assert (end <= len(v));
do as_imm_buf(v) |p, _len| {
Expand All @@ -279,7 +268,9 @@ pub pure fn view<T>(v: &r/[T], start: uint, end: uint) -> &r/[T] {
/// Return a slice that points into another slice.
#[inline(always)]
pub pure fn mut_view<T>(v: &r/[mut T], start: uint, end: uint) -> &r/[mut T] {
pub pure fn mut_slice<T>(v: &r/[mut T], start: uint,
end: uint) -> &r/[mut T] {
assert (start <= end);
assert (end <= len(v));
do as_mut_buf(v) |p, _len| {
Expand All @@ -293,7 +284,7 @@ pub pure fn mut_view<T>(v: &r/[mut T], start: uint, end: uint) -> &r/[mut T] {
/// Return a slice that points into another slice.
#[inline(always)]
pub pure fn const_view<T>(v: &r/[const T], start: uint,
pub pure fn const_slice<T>(v: &r/[const T], start: uint,
end: uint) -> &r/[const T] {
assert (start <= end);
assert (end <= len(v));
Expand All @@ -319,12 +310,12 @@ pub fn split<T: Copy>(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));
result.push(slice(v, start, i).to_vec());
start = i + 1u;
}
}
}
result.push(slice(v, start, ln));
result.push(slice(v, start, ln).to_vec());
result
}
Expand All @@ -343,14 +334,14 @@ pub fn splitn<T: Copy>(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));
result.push(slice(v, start, i).to_vec());
// Make sure to skip the separator.
start = i + 1u;
count -= 1u;
}
}
}
result.push(slice(v, start, ln));
result.push(slice(v, start, ln).to_vec());
result
}
Expand All @@ -368,12 +359,12 @@ pub fn rsplit<T: Copy>(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));
result.push(slice(v, i + 1, end).to_vec());
end = i;
}
}
}
result.push(slice(v, 0u, end));
result.push(slice(v, 0u, end).to_vec());
reverse(result);
return result;
}
Expand All @@ -393,14 +384,14 @@ pub fn rsplitn<T: Copy>(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));
result.push(slice(v, i + 1u, end).to_vec());
// Make sure to skip the separator.
end = i;
count -= 1u;
}
}
}
result.push(slice(v, 0u, end));
result.push(slice(v, 0u, end).to_vec());
reverse(result);
result
}
Expand Down Expand Up @@ -478,15 +469,15 @@ pub fn shift<T>(v: &mut ~[T]) -> T {
// popped. For the moment it unsafely exists at both the head and last
// positions
{
let first_slice = view(*v, 0, 1);
let last_slice = view(*v, next_ln, ln);
let first_slice = slice(*v, 0, 1);
let last_slice = slice(*v, next_ln, ln);
raw::copy_memory(::cast::transmute(last_slice), first_slice, 1);
}
// Memcopy everything to the left one element
{
let init_slice = view(*v, 0, next_ln);
let tail_slice = view(*v, 1, ln);
let init_slice = slice(*v, 0, next_ln);
let tail_slice = slice(*v, 1, ln);
raw::copy_memory(::cast::transmute(init_slice),
tail_slice,
next_ln);
Expand Down Expand Up @@ -1464,9 +1455,9 @@ pure fn each_permutation<T: Copy>(v: &[T], put: fn(ts: &[T]) -> bool) {
let mut i = 0u;
while i < ln {
let elt = v[i];
let mut rest = slice(v, 0u, i);
let mut rest = slice(v, 0u, i).to_vec();
unsafe {
rest.push_all(const_view(v, i+1u, ln));
rest.push_all(const_slice(v, i+1u, ln));
for each_permutation(rest) |permutation| {
if !put(append(~[elt], permutation)) {
return;
Expand All @@ -1485,7 +1476,7 @@ pub pure fn windowed<TT: Copy>(nn: uint, xx: &[TT]) -> ~[~[TT]] {
let len = vec::len(xx);
if ii+nn <= len {
unsafe {
ww.push(vec::slice(xx, ii, ii+nn));
ww.push(slice(xx, ii, ii+nn).to_vec());
}
}
}
Expand Down Expand Up @@ -1689,7 +1680,7 @@ impl<T: Copy> CopyableVector<T> for &[const T] {
/// Returns a copy of the elements from [`start`..`end`) from `v`.
#[inline]
pure fn slice(&self, start: uint, end: uint) -> ~[T] {
slice(*self, start, end)
slice(*self, start, end).to_vec()
}

/// Returns all but the first element of a vector
Expand All @@ -1713,7 +1704,7 @@ impl<T> ImmutableVector<T> for &[T] {
/// Return a slice that points into another slice.
#[inline]
pure fn view(&self, start: uint, end: uint) -> &self/[T] {
view(*self, start, end)
slice(*self, start, end)
}

/// Reduce a vector from right to left
Expand Down Expand Up @@ -2566,42 +2557,45 @@ mod tests {

#[test]
fn test_slice() {
// Test on-stack -> on-stack slice.
let mut v = slice(~[1, 2, 3], 1u, 3u);
assert (len(v) == 2u);
assert (v[0] == 2);
assert (v[1] == 3);

// Test on-heap -> on-stack slice.
v = slice(~[1, 2, 3, 4, 5], 0u, 3u);
assert (len(v) == 3u);
assert (v[0] == 1);
assert (v[1] == 2);
assert (v[2] == 3);

// Test on-heap -> on-heap slice.
v = slice(~[1, 2, 3, 4, 5, 6], 1u, 6u);
assert (len(v) == 5u);
assert (v[0] == 2);
assert (v[1] == 3);
assert (v[2] == 4);
assert (v[3] == 5);
assert (v[4] == 6);
// Test fixed length vector.
let vec_fixed = [1, 2, 3, 4];
let v_a = slice(vec_fixed, 1u, len(vec_fixed)).to_vec();
assert (len(v_a) == 3u);
assert (v_a[0] == 2);
assert (v_a[1] == 3);
assert (v_a[2] == 4);

// Test on stack.
let vec_stack = &[1, 2, 3];
let v_b = slice(vec_stack, 1u, 3u).to_vec();
assert (len(v_b) == 2u);
assert (v_b[0] == 2);
assert (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_vec();
assert (len(v_c) == 3u);
assert (v_c[0] == 1);
assert (v_c[1] == 2);
assert (v_c[2] == 3);

// Test on exchange heap.
let vec_unique = ~[1, 2, 3, 4, 5, 6];
let v_d = slice(vec_unique, 1u, 6u).to_vec();
assert (len(v_d) == 5u);
assert (v_d[0] == 2);
assert (v_d[1] == 3);
assert (v_d[2] == 4);
assert (v_d[3] == 5);
assert (v_d[4] == 6);
}

#[test]
fn test_pop() {
// Test on-stack pop.
let mut v = ~[1, 2, 3];
let mut e = v.pop();
assert (len(v) == 2u);
assert (v[0] == 1);
assert (v[1] == 2);
assert (e == 3);

// Test on-heap pop.
v = ~[1, 2, 3, 4, 5];
e = v.pop();
let mut v = ~[1, 2, 3, 4, 5];
let e = v.pop();
assert (len(v) == 4u);
assert (v[0] == 1);
assert (v[1] == 2);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/back/rpath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,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::view(split2, start_idx, len2 - 1));
path.push_all(vec::slice(split2, start_idx, len2 - 1));

if !path.is_empty() {
return Path("").push_many(path);
Expand Down
5 changes: 3 additions & 2 deletions src/librustc/metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,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::view(*elt.data, elt.start + 4u, elt.end)) {
if eq_fn(vec::slice(*elt.data, elt.start + 4u, elt.end)) {
return Some(reader::doc_at(d.data, pos).doc);
}
};
Expand All @@ -75,7 +75,8 @@ pub type GetCrateDataCb = &fn(ast::crate_num) -> cmd;

pub fn maybe_find_item(item_id: int, items: ebml::Doc) -> Option<ebml::Doc> {
fn eq_item(bytes: &[u8], item_id: int) -> bool {
return io::u64_from_be_bytes(vec::view(bytes, 0u, 4u), 0u, 4u) as int
return io::u64_from_be_bytes(
vec::slice(bytes, 0u, 4u), 0u, 4u) as int
== item_id;
}
lookup_hash(items,
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,7 @@ fn encode_crate_deps(ecx: @encode_ctxt,
}
// mut -> immutable hack for vec::map
return vec::slice(deps, 0u, vec::len(deps));
return vec::slice(deps, 0u, vec::len(deps)).to_vec();
}
// We're just going to write a list of crate 'name-hash-version's, with
Expand Down Expand Up @@ -1307,7 +1307,7 @@ pub fn encode_metadata(parms: encode_parms, crate: &crate) -> ~[u8] {
// vec::from_slice(metadata_encoding_version) +
(do str::as_bytes(&~"rust\x00\x00\x00\x01") |bytes| {
vec::slice(*bytes, 0, 8)
vec::slice(*bytes, 0, 8).to_vec()
}) + flate::deflate_bytes(wr.bytes.check_out(|buf| buf))
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustc/metadata/tydecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,8 @@ pub fn parse_def_id(buf: &[u8]) -> ast::def_id {
fail!();
}
let crate_part = vec::view(buf, 0u, colon_idx);
let def_part = vec::view(buf, colon_idx + 1u, len);
let crate_part = vec::slice(buf, 0u, colon_idx);
let def_part = vec::slice(buf, colon_idx + 1u, len);
let crate_num = match uint::parse_bytes(crate_part, 10u) {
Some(cn) => cn as int,
Expand Down
Loading

0 comments on commit 566bcf2

Please sign in to comment.