Fallout from mut slices

This commit is contained in:
Nick Cameron 2014-12-19 12:44:24 +13:00
parent 4e2afb0052
commit 3bf405682d
27 changed files with 65 additions and 66 deletions

View File

@ -94,7 +94,7 @@ use core::iter::{range_step, MultiplicativeIterator};
use core::kinds::Sized;
use core::mem::size_of;
use core::mem;
use core::ops::FnMut;
use core::ops::{FnMut,SliceMut};
use core::prelude::{Clone, Greater, Iterator, IteratorExt, Less, None, Option};
use core::prelude::{Ord, Ordering, RawPtr, Some, range};
use core::ptr;
@ -1110,7 +1110,7 @@ impl<T> SliceExt<T> for [T] {
#[inline]
fn move_from(&mut self, mut src: Vec<T>, start: uint, end: uint) -> uint {
for (a, b) in self.iter_mut().zip(src[mut start..end].iter_mut()) {
for (a, b) in self.iter_mut().zip(src.slice_mut(start, end).iter_mut()) {
mem::swap(a, b);
}
cmp::min(self.len(), end-start)
@ -1326,7 +1326,7 @@ impl<T> BorrowFrom<Vec<T>> for [T] {
#[unstable = "trait is unstable"]
impl<T> BorrowFromMut<Vec<T>> for [T] {
fn borrow_from_mut(owned: &mut Vec<T>) -> &mut [T] { owned[mut] }
fn borrow_from_mut(owned: &mut Vec<T>) -> &mut [T] { owned.as_mut_slice_() }
}
#[unstable = "trait is unstable"]
@ -2491,14 +2491,14 @@ mod tests {
assert!(a == [7i,2,3,4]);
let mut a = [1i,2,3,4,5];
let b = vec![5i,6,7,8,9,0];
assert_eq!(a[mut 2..4].move_from(b,1,6), 2);
assert_eq!(a.slice_mut(2, 4).move_from(b,1,6), 2);
assert!(a == [1i,2,6,7,5]);
}
#[test]
fn test_reverse_part() {
let mut values = [1i,2,3,4,5];
values[mut 1..4].reverse();
values.slice_mut(1, 4).reverse();
assert!(values == [1,4,3,2,5]);
}
@ -2545,9 +2545,9 @@ mod tests {
fn test_bytes_set_memory() {
use slice::bytes::MutableByteVector;
let mut values = [1u8,2,3,4,5];
values[mut 0..5].set_memory(0xAB);
values.slice_mut(0, 5).set_memory(0xAB);
assert!(values == [0xAB, 0xAB, 0xAB, 0xAB, 0xAB]);
values[mut 2..4].set_memory(0xFF);
values.slice_mut(2, 4).set_memory(0xFF);
assert!(values == [0xAB, 0xAB, 0xFF, 0xFF, 0xAB]);
}

View File

@ -179,7 +179,7 @@ pub fn float_to_str_bytes_common<T: Float, U, F>(
_ => ()
}
buf[mut ..end].reverse();
buf.slice_to_mut(end).reverse();
// Remember start of the fractional digits.
// Points one beyond end of buf if none get generated,
@ -316,7 +316,7 @@ pub fn float_to_str_bytes_common<T: Float, U, F>(
impl<'a> fmt::FormatWriter for Filler<'a> {
fn write(&mut self, bytes: &[u8]) -> fmt::Result {
slice::bytes::copy_memory(self.buf[mut *self.end..],
slice::bytes::copy_memory(self.buf.slice_from_mut(*self.end),
bytes);
*self.end += bytes.len();
Ok(())

View File

@ -264,24 +264,26 @@ impl<T> SliceExt<T> for [T] {
fn as_mut_slice(&mut self) -> &mut [T] { self }
fn slice_mut(&mut self, start: uint, end: uint) -> &mut [T] {
self[mut start..end]
ops::SliceMut::slice_or_fail_mut(self, &start, &end)
}
#[inline]
fn slice_from_mut(&mut self, start: uint) -> &mut [T] {
self[mut start..]
ops::SliceMut::slice_from_or_fail_mut(self, &start)
}
#[inline]
fn slice_to_mut(&mut self, end: uint) -> &mut [T] {
self[mut ..end]
ops::SliceMut::slice_to_or_fail_mut(self, &end)
}
#[inline]
fn split_at_mut(&mut self, mid: uint) -> (&mut [T], &mut [T]) {
unsafe {
let self2: &mut [T] = mem::transmute_copy(&self);
(self[mut ..mid], self2[mut mid..])
(ops::SliceMut::slice_to_or_fail_mut(self, &mid),
ops::SliceMut::slice_from_or_fail_mut(self2, &mid))
}
}
@ -315,14 +317,13 @@ impl<T> SliceExt<T> for [T] {
#[inline]
fn tail_mut(&mut self) -> &mut [T] {
let len = self.len();
self[mut 1..len]
self.slice_from_mut(1)
}
#[inline]
fn init_mut(&mut self) -> &mut [T] {
let len = self.len();
self[mut 0..len - 1]
self.slice_to_mut(len-1)
}
#[inline]
@ -560,7 +561,7 @@ impl<T: Ord> OrdSliceExt<T> for [T] {
self.swap(j, i-1);
// Step 4: Reverse the (previously) weakly decreasing part
self[mut i..].reverse();
self.slice_from_mut(i).reverse();
true
}
@ -582,7 +583,7 @@ impl<T: Ord> OrdSliceExt<T> for [T] {
}
// Step 2: Reverse the weakly increasing part
self[mut i..].reverse();
self.slice_from_mut(i).reverse();
// Step 3: Find the rightmost element equal to or bigger than the pivot (i-1)
let mut j = self.len() - 1;

View File

@ -897,6 +897,7 @@ impl<'a> Iterator<&'a str> for SplitStr<'a> {
}
}
/*
Section: Comparing strings
*/

View File

@ -9,7 +9,7 @@
// except according to those terms.
use test::Bencher;
use core::ops::{Range, FullRange, RangeFrom};
use core::ops::{Range, FullRange, RangeFrom, RangeTo};
// Overhead of dtors

View File

@ -102,7 +102,7 @@ impl Writer for SeekableMemWriter {
// Do the necessary writes
if left.len() > 0 {
slice::bytes::copy_memory(self.buf[mut self.pos..], left);
slice::bytes::copy_memory(self.buf.slice_from_mut(self.pos), left);
}
if right.len() > 0 {
self.buf.push_all(right);

View File

@ -485,6 +485,7 @@ fn resolve_expr(visitor: &mut RegionResolutionVisitor, expr: &ast::Expr) {
let prev_cx = visitor.cx;
visitor.cx.parent = Some(expr.id);
{
let region_maps = &mut visitor.region_maps;
let terminating = |id| {

View File

@ -139,14 +139,14 @@ impl FixedBuffer for FixedBuffer64 {
let buffer_remaining = size - self.buffer_idx;
if input.len() >= buffer_remaining {
copy_memory(
self.buffer[mut self.buffer_idx..size],
self.buffer.slice_mut(self.buffer_idx, size),
input[..buffer_remaining]);
self.buffer_idx = 0;
func(&self.buffer);
i += buffer_remaining;
} else {
copy_memory(
self.buffer[mut self.buffer_idx..self.buffer_idx + input.len()],
self.buffer.slice_mut(self.buffer_idx, self.buffer_idx + input.len()),
input);
self.buffer_idx += input.len();
return;
@ -165,7 +165,7 @@ impl FixedBuffer for FixedBuffer64 {
// be empty.
let input_remaining = input.len() - i;
copy_memory(
self.buffer[mut ..input_remaining],
self.buffer.slice_to_mut(input_remaining),
input[i..]);
self.buffer_idx += input_remaining;
}
@ -176,13 +176,13 @@ impl FixedBuffer for FixedBuffer64 {
fn zero_until(&mut self, idx: uint) {
assert!(idx >= self.buffer_idx);
self.buffer[mut self.buffer_idx..idx].set_memory(0);
self.buffer.slice_mut(self.buffer_idx, idx).set_memory(0);
self.buffer_idx = idx;
}
fn next<'s>(&'s mut self, len: uint) -> &'s mut [u8] {
self.buffer_idx += len;
return self.buffer[mut self.buffer_idx - len..self.buffer_idx];
return self.buffer.slice_mut(self.buffer_idx - len, self.buffer_idx);
}
fn full_buffer<'s>(&'s mut self) -> &'s [u8] {
@ -362,7 +362,7 @@ impl Engine256State {
)
);
read_u32v_be(w[mut 0..16], data);
read_u32v_be(w.slice_mut(0, 16), data);
// Putting the message schedule inside the same loop as the round calculations allows for
// the compiler to generate better code.
@ -498,14 +498,14 @@ impl Digest for Sha256 {
fn result(&mut self, out: &mut [u8]) {
self.engine.finish();
write_u32_be(out[mut 0..4], self.engine.state.h0);
write_u32_be(out[mut 4..8], self.engine.state.h1);
write_u32_be(out[mut 8..12], self.engine.state.h2);
write_u32_be(out[mut 12..16], self.engine.state.h3);
write_u32_be(out[mut 16..20], self.engine.state.h4);
write_u32_be(out[mut 20..24], self.engine.state.h5);
write_u32_be(out[mut 24..28], self.engine.state.h6);
write_u32_be(out[mut 28..32], self.engine.state.h7);
write_u32_be(out.slice_mut(0, 4), self.engine.state.h0);
write_u32_be(out.slice_mut(4, 8), self.engine.state.h1);
write_u32_be(out.slice_mut(8, 12), self.engine.state.h2);
write_u32_be(out.slice_mut(12, 16), self.engine.state.h3);
write_u32_be(out.slice_mut(16, 20), self.engine.state.h4);
write_u32_be(out.slice_mut(20, 24), self.engine.state.h5);
write_u32_be(out.slice_mut(24, 28), self.engine.state.h6);
write_u32_be(out.slice_mut(28, 32), self.engine.state.h7);
}
fn reset(&mut self) {

View File

@ -104,7 +104,7 @@ impl<R: Reader> BufferedReader<R> {
impl<R: Reader> Buffer for BufferedReader<R> {
fn fill_buf<'a>(&'a mut self) -> IoResult<&'a [u8]> {
if self.pos == self.cap {
self.cap = try!(self.inner.read(self.buf[mut]));
self.cap = try!(self.inner.read(self.buf.as_mut_slice()));
self.pos = 0;
}
Ok(self.buf[self.pos..self.cap])
@ -219,7 +219,7 @@ impl<W: Writer> Writer for BufferedWriter<W> {
if buf.len() > self.buf.len() {
self.inner.as_mut().unwrap().write(buf)
} else {
let dst = self.buf[mut self.pos..];
let dst = self.buf.slice_from_mut(self.pos);
slice::bytes::copy_memory(dst, buf);
self.pos += buf.len();
Ok(())

View File

@ -87,7 +87,7 @@ impl Reader for ChanReader {
loop {
let count = match self.fill_buf().ok() {
Some(src) => {
let dst = buf[mut num_read..];
let dst = buf.slice_from_mut(num_read);
let count = cmp::min(src.len(), dst.len());
bytes::copy_memory(dst, src[..count]);
count

View File

@ -931,11 +931,11 @@ mod test {
{
let mut read_stream = File::open_mode(filename, Open, Read);
{
let read_buf = read_mem[mut 0..4];
let read_buf = read_mem.slice_mut(0, 4);
check!(read_stream.read(read_buf));
}
{
let read_buf = read_mem[mut 4..8];
let read_buf = read_mem.slice_mut(4, 8);
check!(read_stream.read(read_buf));
}
}

View File

@ -169,7 +169,7 @@ impl Reader for MemReader {
let write_len = min(buf.len(), self.buf.len() - self.pos);
{
let input = self.buf[self.pos.. self.pos + write_len];
let output = buf[mut ..write_len];
let output = buf.slice_to_mut(write_len);
assert_eq!(input.len(), output.len());
slice::bytes::copy_memory(output, input);
}
@ -214,7 +214,7 @@ impl<'a> Reader for &'a [u8] {
let write_len = min(buf.len(), self.len());
{
let input = self[..write_len];
let output = buf[mut ..write_len];
let output = buf.slice_to_mut(write_len);
slice::bytes::copy_memory(output, input);
}
@ -279,7 +279,7 @@ impl<'a> BufWriter<'a> {
impl<'a> Writer for BufWriter<'a> {
#[inline]
fn write(&mut self, src: &[u8]) -> IoResult<()> {
let dst = self.buf[mut self.pos..];
let dst = self.buf.slice_from_mut(self.pos);
let dst_len = dst.len();
if dst_len == 0 {
@ -359,7 +359,7 @@ impl<'a> Reader for BufReader<'a> {
let write_len = min(buf.len(), self.buf.len() - self.pos);
{
let input = self.buf[self.pos.. self.pos + write_len];
let output = buf[mut ..write_len];
let output = buf.slice_to_mut(write_len);
assert_eq!(input.len(), output.len());
slice::bytes::copy_memory(output, input);
}
@ -652,7 +652,7 @@ mod test {
assert!(r.read_at_least(buf.len(), &mut buf).is_ok());
let b: &[_] = &[1, 2, 3];
assert_eq!(buf, b);
assert!(r.read_at_least(0, buf[mut ..0]).is_ok());
assert!(r.read_at_least(0, buf.slice_to_mut(0)).is_ok());
assert_eq!(buf, b);
assert!(r.read_at_least(buf.len(), &mut buf).is_ok());
let b: &[_] = &[4, 5, 6];

View File

@ -513,7 +513,7 @@ pub trait Reader {
while read < min {
let mut zeroes = 0;
loop {
match self.read(buf[mut read..]) {
match self.read(buf.slice_from_mut(read)) {
Ok(0) => {
zeroes += 1;
if zeroes >= NO_PROGRESS_LIMIT {
@ -1123,7 +1123,7 @@ pub trait Writer {
#[inline]
fn write_char(&mut self, c: char) -> IoResult<()> {
let mut buf = [0u8, ..4];
let n = c.encode_utf8(buf[mut]).unwrap_or(0);
let n = c.encode_utf8(buf.as_mut_slice()).unwrap_or(0);
self.write(buf[..n])
}
@ -1555,7 +1555,7 @@ pub trait Buffer: Reader {
{
let mut start = 1;
while start < width {
match try!(self.read(buf[mut start..width])) {
match try!(self.read(buf.slice_mut(start, width))) {
n if n == width - start => break,
n if n < width - start => { start += n; }
_ => return Err(standard_error(InvalidInput)),

View File

@ -250,7 +250,7 @@ impl<'a> Parser<'a> {
assert!(head.len() + tail.len() <= 8);
let mut gs = [0u16, ..8];
gs.clone_from_slice(head);
gs[mut 8 - tail.len() .. 8].clone_from_slice(tail);
gs.slice_mut(8 - tail.len(), 8).clone_from_slice(tail);
Ipv6Addr(gs[0], gs[1], gs[2], gs[3], gs[4], gs[5], gs[6], gs[7])
}

View File

@ -49,7 +49,7 @@ use sys_common;
/// match socket.recv_from(&mut buf) {
/// Ok((amt, src)) => {
/// // Send a reply to the socket we received data from
/// let buf = buf[mut ..amt];
/// let buf = buf.slice_to_mut(amt);
/// buf.reverse();
/// socket.send_to(buf, src);
/// }

View File

@ -51,7 +51,7 @@ impl<R: Reader> Reader for LimitReader<R> {
}
let len = cmp::min(self.limit, buf.len());
let res = self.inner.read(buf[mut ..len]);
let res = self.inner.read(buf.slice_to_mut(len));
match res {
Ok(len) => self.limit -= len,
_ => {}

View File

@ -55,7 +55,7 @@ mod imp {
let mut read = 0;
let len = v.len();
while read < len {
let result = getrandom(v[mut read..]);
let result = getrandom(v.slice_from_mut(read));
if result == -1 {
let err = errno() as libc::c_int;
if err == libc::EINTR {

View File

@ -139,7 +139,7 @@ fn abort_(args: &fmt::Arguments) -> ! {
}
impl<'a> FormatWriter for BufWriter<'a> {
fn write(&mut self, bytes: &[u8]) -> fmt::Result {
let left = self.buf[mut self.pos..];
let left = self.buf.slice_from_mut(self.pos);
let to_write = bytes[..cmp::min(bytes.len(), left.len())];
slice::bytes::copy_memory(left, to_write);
self.pos += to_write.len();

View File

@ -52,7 +52,7 @@ fn rotate(x: &mut [i32]) {
fn next_permutation(perm: &mut [i32], count: &mut [i32]) {
for i in range(1, perm.len()) {
rotate(perm[mut ..i + 1]);
rotate(perm.slice_to_mut(i + 1));
let count_i = &mut count[i];
if *count_i >= i as i32 {
*count_i = 0;
@ -131,7 +131,7 @@ impl Perm {
fn reverse(tperm: &mut [i32], mut k: uint) {
tperm[mut ..k].reverse()
tperm.slice_to_mut(k).reverse()
}
fn work(mut perm: Perm, n: uint, max: uint) -> (i32, i32) {

View File

@ -128,7 +128,7 @@ impl<'a, W: Writer> RepeatFasta<'a, W> {
copy_memory(buf.as_mut_slice(), alu);
let buf_len = buf.len();
copy_memory(buf[mut alu_len..buf_len],
copy_memory(buf.slice_mut(alu_len, buf_len),
alu[..LINE_LEN]);
let mut pos = 0;

View File

@ -254,6 +254,6 @@ fn parallel<'a, I, T, F>(mut iter: I, f: F)
fn main() {
let mut data = read_to_end(&mut stdin_raw()).unwrap();
let tables = &Tables::new();
parallel(mut_dna_seqs(data[mut]), |&: seq| reverse_complement(seq, tables));
parallel(mut_dna_seqs(data.as_mut_slice()), |&: seq| reverse_complement(seq, tables));
stdout_raw().write(data.as_mut_slice()).unwrap();
}

View File

@ -9,6 +9,7 @@
// except according to those terms.
// Test range syntax - type errors.
#![feature(slicing_syntax)]
pub fn main() {
// Mixed types.

View File

@ -9,6 +9,7 @@
// except according to those terms.
// Test range syntax - borrow errors.
#![feature(slicing_syntax)]
pub fn main() {
let r = {

View File

@ -20,8 +20,4 @@ fn main() {
x[Foo..]; //~ ERROR cannot take a slice of a value with type `Foo`
x[..Foo]; //~ ERROR cannot take a slice of a value with type `Foo`
x[Foo..Foo]; //~ ERROR cannot take a slice of a value with type `Foo`
x[mut]; //~ ERROR cannot take a mutable slice of a value with type `Foo`
x[mut Foo..]; //~ ERROR cannot take a mutable slice of a value with type `Foo`
x[mut ..Foo]; //~ ERROR cannot take a mutable slice of a value with type `Foo`
x[mut Foo..Foo]; //~ ERROR cannot take a mutable slice of a value with type `Foo`
}

View File

@ -15,5 +15,6 @@
fn main() {
let x: &[int] = &[1, 2, 3, 4, 5];
// Can't mutably slice an immutable slice
let y = x[mut 2..4]; //~ ERROR cannot borrow
let slice: &mut [int] = &mut [0, 1];
x[2..4] = slice; //~ ERROR cannot borrow
}

View File

@ -16,9 +16,4 @@ fn main() {
let x: &[int] = &[1, 2, 3, 4, 5];
// Immutable slices are not mutable.
let y: &mut[_] = x[2..4]; //~ ERROR cannot borrow immutable dereference of `&`-pointer as mutabl
let x: &mut [int] = &mut [1, 2, 3, 4, 5];
// Can't borrow mutably twice
let y = x[mut 1..2];
let y = x[mut 4..5]; //~ERROR cannot borrow
}

View File

@ -10,6 +10,8 @@
// Test range syntax.
#![feature(slicing_syntax)]
fn foo() -> int { 42 }
pub fn main() {