Skip to content

Commit 3803c09

Browse files
committed
Rename IoSlice(Mut)::advance to advance_slice
To make way for a new IoSlice(Mut)::advance function that advances a single slice. Also changes the signature to accept a `&mut &mut [IoSlice]`, not returning anything. This will better match the future IoSlice::advance function.
1 parent 4664725 commit 3803c09

File tree

2 files changed

+27
-28
lines changed

2 files changed

+27
-28
lines changed

library/std/src/io/mod.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ mod tests;
253253

254254
use crate::cmp;
255255
use crate::fmt;
256+
use crate::mem::replace;
256257
use crate::ops::{Deref, DerefMut};
257258
use crate::ptr;
258259
use crate::slice;
@@ -1070,13 +1071,13 @@ impl<'a> IoSliceMut<'a> {
10701071
/// ][..];
10711072
///
10721073
/// // Mark 10 bytes as read.
1073-
/// bufs = IoSliceMut::advance(bufs, 10);
1074+
/// IoSliceMut::advance_slice(&mut bufs, 10);
10741075
/// assert_eq!(bufs[0].deref(), [2; 14].as_ref());
10751076
/// assert_eq!(bufs[1].deref(), [3; 8].as_ref());
10761077
/// ```
10771078
#[unstable(feature = "io_slice_advance", issue = "62726")]
10781079
#[inline]
1079-
pub fn advance<'b>(bufs: &'b mut [IoSliceMut<'a>], n: usize) -> &'b mut [IoSliceMut<'a>] {
1080+
pub fn advance_slice(bufs: &mut &mut [IoSliceMut<'a>], n: usize) {
10801081
// Number of buffers to remove.
10811082
let mut remove = 0;
10821083
// Total length of all the to be removed buffers.
@@ -1090,11 +1091,10 @@ impl<'a> IoSliceMut<'a> {
10901091
}
10911092
}
10921093

1093-
let bufs = &mut bufs[remove..];
1094+
*bufs = &mut replace(bufs, &mut [])[remove..];
10941095
if !bufs.is_empty() {
10951096
bufs[0].0.advance(n - accumulated_len)
10961097
}
1097-
bufs
10981098
}
10991099
}
11001100

@@ -1179,12 +1179,12 @@ impl<'a> IoSlice<'a> {
11791179
/// ][..];
11801180
///
11811181
/// // Mark 10 bytes as written.
1182-
/// bufs = IoSlice::advance(bufs, 10);
1182+
/// IoSlice::advance_slice(&mut bufs, 10);
11831183
/// assert_eq!(bufs[0].deref(), [2; 14].as_ref());
11841184
/// assert_eq!(bufs[1].deref(), [3; 8].as_ref());
11851185
#[unstable(feature = "io_slice_advance", issue = "62726")]
11861186
#[inline]
1187-
pub fn advance<'b>(bufs: &'b mut [IoSlice<'a>], n: usize) -> &'b mut [IoSlice<'a>] {
1187+
pub fn advance_slice(bufs: &mut &mut [IoSlice<'a>], n: usize) {
11881188
// Number of buffers to remove.
11891189
let mut remove = 0;
11901190
// Total length of all the to be removed buffers.
@@ -1198,11 +1198,10 @@ impl<'a> IoSlice<'a> {
11981198
}
11991199
}
12001200

1201-
let bufs = &mut bufs[remove..];
1201+
*bufs = &mut replace(bufs, &mut [])[remove..];
12021202
if !bufs.is_empty() {
12031203
bufs[0].0.advance(n - accumulated_len)
12041204
}
1205-
bufs
12061205
}
12071206
}
12081207

@@ -1511,7 +1510,7 @@ pub trait Write {
15111510
fn write_all_vectored(&mut self, mut bufs: &mut [IoSlice<'_>]) -> Result<()> {
15121511
// Guarantee that bufs is empty if it contains no data,
15131512
// to avoid calling write_vectored if there is no data to be written.
1514-
bufs = IoSlice::advance(bufs, 0);
1513+
IoSlice::advance_slice(&mut bufs, 0);
15151514
while !bufs.is_empty() {
15161515
match self.write_vectored(bufs) {
15171516
Ok(0) => {
@@ -1520,7 +1519,7 @@ pub trait Write {
15201519
&"failed to write whole buffer",
15211520
));
15221521
}
1523-
Ok(n) => bufs = IoSlice::advance(bufs, n),
1522+
Ok(n) => IoSlice::advance_slice(&mut bufs, n),
15241523
Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
15251524
Err(e) => return Err(e),
15261525
}

library/std/src/io/tests.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ fn test_read_to_end_capacity() -> io::Result<()> {
353353
}
354354

355355
#[test]
356-
fn io_slice_mut_advance() {
356+
fn io_slice_mut_advance_slice() {
357357
let mut buf1 = [1; 8];
358358
let mut buf2 = [2; 16];
359359
let mut buf3 = [3; 8];
@@ -364,75 +364,75 @@ fn io_slice_mut_advance() {
364364
][..];
365365

366366
// Only in a single buffer..
367-
bufs = IoSliceMut::advance(bufs, 1);
367+
IoSliceMut::advance_slice(&mut bufs, 1);
368368
assert_eq!(bufs[0].deref(), [1; 7].as_ref());
369369
assert_eq!(bufs[1].deref(), [2; 16].as_ref());
370370
assert_eq!(bufs[2].deref(), [3; 8].as_ref());
371371

372372
// Removing a buffer, leaving others as is.
373-
bufs = IoSliceMut::advance(bufs, 7);
373+
IoSliceMut::advance_slice(&mut bufs, 7);
374374
assert_eq!(bufs[0].deref(), [2; 16].as_ref());
375375
assert_eq!(bufs[1].deref(), [3; 8].as_ref());
376376

377377
// Removing a buffer and removing from the next buffer.
378-
bufs = IoSliceMut::advance(bufs, 18);
378+
IoSliceMut::advance_slice(&mut bufs, 18);
379379
assert_eq!(bufs[0].deref(), [3; 6].as_ref());
380380
}
381381

382382
#[test]
383-
fn io_slice_mut_advance_empty_slice() {
384-
let empty_bufs = &mut [][..];
383+
fn io_slice_mut_advance_slice_empty_slice() {
384+
let mut empty_bufs = &mut [][..];
385385
// Shouldn't panic.
386-
IoSliceMut::advance(empty_bufs, 1);
386+
IoSliceMut::advance_slice(&mut empty_bufs, 1);
387387
}
388388

389389
#[test]
390-
fn io_slice_mut_advance_beyond_total_length() {
390+
fn io_slice_mut_advance_slice_beyond_total_length() {
391391
let mut buf1 = [1; 8];
392392
let mut bufs = &mut [IoSliceMut::new(&mut buf1)][..];
393393

394394
// Going beyond the total length should be ok.
395-
bufs = IoSliceMut::advance(bufs, 9);
395+
IoSliceMut::advance_slice(&mut bufs, 9);
396396
assert!(bufs.is_empty());
397397
}
398398

399399
#[test]
400-
fn io_slice_advance() {
400+
fn io_slice_advance_slice() {
401401
let buf1 = [1; 8];
402402
let buf2 = [2; 16];
403403
let buf3 = [3; 8];
404404
let mut bufs = &mut [IoSlice::new(&buf1), IoSlice::new(&buf2), IoSlice::new(&buf3)][..];
405405

406406
// Only in a single buffer..
407-
bufs = IoSlice::advance(bufs, 1);
407+
IoSlice::advance_slice(&mut bufs, 1);
408408
assert_eq!(bufs[0].deref(), [1; 7].as_ref());
409409
assert_eq!(bufs[1].deref(), [2; 16].as_ref());
410410
assert_eq!(bufs[2].deref(), [3; 8].as_ref());
411411

412412
// Removing a buffer, leaving others as is.
413-
bufs = IoSlice::advance(bufs, 7);
413+
IoSlice::advance_slice(&mut bufs, 7);
414414
assert_eq!(bufs[0].deref(), [2; 16].as_ref());
415415
assert_eq!(bufs[1].deref(), [3; 8].as_ref());
416416

417417
// Removing a buffer and removing from the next buffer.
418-
bufs = IoSlice::advance(bufs, 18);
418+
IoSlice::advance_slice(&mut bufs, 18);
419419
assert_eq!(bufs[0].deref(), [3; 6].as_ref());
420420
}
421421

422422
#[test]
423-
fn io_slice_advance_empty_slice() {
424-
let empty_bufs = &mut [][..];
423+
fn io_slice_advance_slice_empty_slice() {
424+
let mut empty_bufs = &mut [][..];
425425
// Shouldn't panic.
426-
IoSlice::advance(empty_bufs, 1);
426+
IoSlice::advance_slice(&mut empty_bufs, 1);
427427
}
428428

429429
#[test]
430-
fn io_slice_advance_beyond_total_length() {
430+
fn io_slice_advance_slice_beyond_total_length() {
431431
let buf1 = [1; 8];
432432
let mut bufs = &mut [IoSlice::new(&buf1)][..];
433433

434434
// Going beyond the total length should be ok.
435-
bufs = IoSlice::advance(bufs, 9);
435+
IoSlice::advance_slice(&mut bufs, 9);
436436
assert!(bufs.is_empty());
437437
}
438438

0 commit comments

Comments
 (0)