Skip to content

Commit

Permalink
Add IoSlice(Mut)::advance
Browse files Browse the repository at this point in the history
Advance the internal cursor of a single slice.
  • Loading branch information
Thomasdezeeuw committed May 29, 2021
1 parent 3803c09 commit 49e25b5
Showing 1 changed file with 54 additions and 2 deletions.
56 changes: 54 additions & 2 deletions library/std/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,32 @@ impl<'a> IoSliceMut<'a> {

/// Advance the internal cursor of the slice.
///
/// Also see [`IoSliceMut::advance_slice`] to advance the cursors of
/// multiple buffers.
///
/// # Examples
///
/// ```
/// #![feature(io_slice_advance)]
///
/// use std::io::IoSliceMut;
/// use std::ops::Deref;
///
/// let mut data = [1; 8];
/// let mut buf = IoSliceMut::new(&mut data);
///
/// // Mark 10 bytes as read.
/// buf.advance(3);
/// assert_eq!(buf.deref(), [1; 5].as_ref());
/// ```
#[unstable(feature = "io_slice_advance", issue = "62726")]
#[inline]
pub fn advance(&mut self, n: usize) {
self.0.advance(n)
}

/// Advance the internal cursor of the slices.
///
/// # Notes
///
/// Elements in the slice may be modified if the cursor is not advanced to
Expand Down Expand Up @@ -1093,7 +1119,7 @@ impl<'a> IoSliceMut<'a> {

*bufs = &mut replace(bufs, &mut [])[remove..];
if !bufs.is_empty() {
bufs[0].0.advance(n - accumulated_len)
bufs[0].advance(n - accumulated_len)
}
}
}
Expand Down Expand Up @@ -1153,6 +1179,32 @@ impl<'a> IoSlice<'a> {

/// Advance the internal cursor of the slice.
///
/// Also see [`IoSlice::advance_slice`] to advance the cursors of multiple
/// buffers.
///
/// # Examples
///
/// ```
/// #![feature(io_slice_advance)]
///
/// use std::io::IoSlice;
/// use std::ops::Deref;
///
/// let mut data = [1; 8];
/// let mut buf = IoSlice::new(&mut data);
///
/// // Mark 10 bytes as read.
/// buf.advance(3);
/// assert_eq!(buf.deref(), [1; 5].as_ref());
/// ```
#[unstable(feature = "io_slice_advance", issue = "62726")]
#[inline]
pub fn advance(&mut self, n: usize) {
self.0.advance(n)
}

/// Advance the internal cursor of the slices.
///
/// # Notes
///
/// Elements in the slice may be modified if the cursor is not advanced to
Expand Down Expand Up @@ -1200,7 +1252,7 @@ impl<'a> IoSlice<'a> {

*bufs = &mut replace(bufs, &mut [])[remove..];
if !bufs.is_empty() {
bufs[0].0.advance(n - accumulated_len)
bufs[0].advance(n - accumulated_len)
}
}
}
Expand Down

0 comments on commit 49e25b5

Please sign in to comment.