Skip to content

Commit

Permalink
Rollup merge of rust-lang#67049 - tmiasko:io-slice-advance, r=rkruppe
Browse files Browse the repository at this point in the history
Simplify {IoSlice, IoSliceMut}::advance examples and tests

Remove unnecessary calls to `std::mem::replace` and make variables immutable.
  • Loading branch information
JohnTitor authored Dec 6, 2019
2 parents 0b471bf + 92bc35f commit d1397db
Showing 1 changed file with 26 additions and 29 deletions.
55 changes: 26 additions & 29 deletions src/libstd/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,6 @@ impl<'a> IoSliceMut<'a> {
/// #![feature(io_slice_advance)]
///
/// use std::io::IoSliceMut;
/// use std::mem;
/// use std::ops::Deref;
///
/// let mut buf1 = [1; 8];
Expand All @@ -1000,7 +999,7 @@ impl<'a> IoSliceMut<'a> {
/// ][..];
///
/// // Mark 10 bytes as read.
/// bufs = IoSliceMut::advance(mem::replace(&mut bufs, &mut []), 10);
/// bufs = IoSliceMut::advance(bufs, 10);
/// assert_eq!(bufs[0].deref(), [2; 14].as_ref());
/// assert_eq!(bufs[1].deref(), [3; 8].as_ref());
/// ```
Expand Down Expand Up @@ -1090,20 +1089,19 @@ impl<'a> IoSlice<'a> {
/// #![feature(io_slice_advance)]
///
/// use std::io::IoSlice;
/// use std::mem;
/// use std::ops::Deref;
///
/// let mut buf1 = [1; 8];
/// let mut buf2 = [2; 16];
/// let mut buf3 = [3; 8];
/// let buf1 = [1; 8];
/// let buf2 = [2; 16];
/// let buf3 = [3; 8];
/// let mut bufs = &mut [
/// IoSlice::new(&mut buf1),
/// IoSlice::new(&mut buf2),
/// IoSlice::new(&mut buf3),
/// IoSlice::new(&buf1),
/// IoSlice::new(&buf2),
/// IoSlice::new(&buf3),
/// ][..];
///
/// // Mark 10 bytes as written.
/// bufs = IoSlice::advance(mem::replace(&mut bufs, &mut []), 10);
/// bufs = IoSlice::advance(bufs, 10);
/// assert_eq!(bufs[0].deref(), [2; 14].as_ref());
/// assert_eq!(bufs[1].deref(), [3; 8].as_ref());
#[unstable(feature = "io_slice_advance", issue = "62726")]
Expand Down Expand Up @@ -2415,7 +2413,6 @@ mod tests {
use crate::cmp;
use crate::io::prelude::*;
use crate::io::{self, IoSlice, IoSliceMut};
use crate::mem;
use crate::ops::Deref;

#[test]
Expand Down Expand Up @@ -2731,26 +2728,26 @@ mod tests {
][..];

// Only in a single buffer..
bufs = IoSliceMut::advance(mem::replace(&mut bufs, &mut []), 1);
bufs = IoSliceMut::advance(bufs, 1);
assert_eq!(bufs[0].deref(), [1; 7].as_ref());
assert_eq!(bufs[1].deref(), [2; 16].as_ref());
assert_eq!(bufs[2].deref(), [3; 8].as_ref());

// Removing a buffer, leaving others as is.
bufs = IoSliceMut::advance(mem::replace(&mut bufs, &mut []), 7);
bufs = IoSliceMut::advance(bufs, 7);
assert_eq!(bufs[0].deref(), [2; 16].as_ref());
assert_eq!(bufs[1].deref(), [3; 8].as_ref());

// Removing a buffer and removing from the next buffer.
bufs = IoSliceMut::advance(mem::replace(&mut bufs, &mut []), 18);
bufs = IoSliceMut::advance(bufs, 18);
assert_eq!(bufs[0].deref(), [3; 6].as_ref());
}

#[test]
fn io_slice_mut_advance_empty_slice() {
let mut empty_bufs = &mut [][..];
let empty_bufs = &mut [][..];
// Shouldn't panic.
IoSliceMut::advance(&mut empty_bufs, 1);
IoSliceMut::advance(empty_bufs, 1);
}

#[test]
Expand All @@ -2759,48 +2756,48 @@ mod tests {
let mut bufs = &mut [IoSliceMut::new(&mut buf1)][..];

// Going beyond the total length should be ok.
bufs = IoSliceMut::advance(mem::replace(&mut bufs, &mut []), 9);
bufs = IoSliceMut::advance(bufs, 9);
assert!(bufs.is_empty());
}

#[test]
fn io_slice_advance() {
let mut buf1 = [1; 8];
let mut buf2 = [2; 16];
let mut buf3 = [3; 8];
let buf1 = [1; 8];
let buf2 = [2; 16];
let buf3 = [3; 8];
let mut bufs =
&mut [IoSlice::new(&mut buf1), IoSlice::new(&mut buf2), IoSlice::new(&mut buf3)][..];
&mut [IoSlice::new(&buf1), IoSlice::new(&buf2), IoSlice::new(&buf3)][..];

// Only in a single buffer..
bufs = IoSlice::advance(mem::replace(&mut bufs, &mut []), 1);
bufs = IoSlice::advance(bufs, 1);
assert_eq!(bufs[0].deref(), [1; 7].as_ref());
assert_eq!(bufs[1].deref(), [2; 16].as_ref());
assert_eq!(bufs[2].deref(), [3; 8].as_ref());

// Removing a buffer, leaving others as is.
bufs = IoSlice::advance(mem::replace(&mut bufs, &mut []), 7);
bufs = IoSlice::advance(bufs, 7);
assert_eq!(bufs[0].deref(), [2; 16].as_ref());
assert_eq!(bufs[1].deref(), [3; 8].as_ref());

// Removing a buffer and removing from the next buffer.
bufs = IoSlice::advance(mem::replace(&mut bufs, &mut []), 18);
bufs = IoSlice::advance(bufs, 18);
assert_eq!(bufs[0].deref(), [3; 6].as_ref());
}

#[test]
fn io_slice_advance_empty_slice() {
let mut empty_bufs = &mut [][..];
let empty_bufs = &mut [][..];
// Shouldn't panic.
IoSlice::advance(&mut empty_bufs, 1);
IoSlice::advance(empty_bufs, 1);
}

#[test]
fn io_slice_advance_beyond_total_length() {
let mut buf1 = [1; 8];
let mut bufs = &mut [IoSlice::new(&mut buf1)][..];
let buf1 = [1; 8];
let mut bufs = &mut [IoSlice::new(&buf1)][..];

// Going beyond the total length should be ok.
bufs = IoSlice::advance(mem::replace(&mut bufs, &mut []), 9);
bufs = IoSlice::advance(bufs, 9);
assert!(bufs.is_empty());
}
}

0 comments on commit d1397db

Please sign in to comment.