X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=library%2Fstd%2Fsrc%2Fio%2Fmod.rs;h=18f7f6a35e98f8ba31d3ef807d04b92dc1480dec;hb=0ecbcbb0ace4229f59e09de035ccef747b024e00;hp=004f18bbfcbc6d4da2d2ad278423b32c829dfe2a;hpb=64ac04567ba397f8600952e3875b9a197f7b2910;p=rust.git diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 004f18bbfcb..18f7f6a35e9 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -252,7 +252,6 @@ mod tests; use crate::cmp; -use crate::convert::TryInto; use crate::fmt; use crate::mem::replace; use crate::ops::{Deref, DerefMut}; @@ -1085,6 +1084,10 @@ pub fn new(buf: &'a mut [u8]) -> IoSliceMut<'a> { /// Also see [`IoSliceMut::advance_slices`] to advance the cursors of /// multiple buffers. /// + /// # Panics + /// + /// Panics when trying to advance beyond the end of the slice. + /// /// # Examples /// /// ``` @@ -1106,15 +1109,18 @@ pub fn advance(&mut self, n: usize) { self.0.advance(n) } - /// Advance the internal cursor of the slices. + /// Advance a slice of slices. /// - /// # Notes + /// Shrinks the slice to remove any `IoSliceMut`s that are fully advanced over. + /// If the cursor ends up in the middle of an `IoSliceMut`, it is modified + /// to start at that cursor. /// - /// Elements in the slice may be modified if the cursor is not advanced to - /// the end of the slice. For example if we have a slice of buffers with 2 - /// `IoSliceMut`s, both of length 8, and we advance the cursor by 10 bytes - /// the first `IoSliceMut` will be untouched however the second will be - /// modified to remove the first 2 bytes (10 - 8). + /// For example, if we have a slice of two 8-byte `IoSliceMut`s, and we advance by 10 bytes, + /// the result will only include the second `IoSliceMut`, advanced by 2 bytes. + /// + /// # Panics + /// + /// Panics when trying to advance beyond the end of the slices. /// /// # Examples /// @@ -1155,7 +1161,9 @@ pub fn advance_slices(bufs: &mut &mut [IoSliceMut<'a>], n: usize) { } *bufs = &mut replace(bufs, &mut [])[remove..]; - if !bufs.is_empty() { + if bufs.is_empty() { + assert!(n == accumulated_len, "advancing io slices beyond their length"); + } else { bufs[0].advance(n - accumulated_len) } } @@ -1220,6 +1228,10 @@ pub fn new(buf: &'a [u8]) -> IoSlice<'a> { /// Also see [`IoSlice::advance_slices`] to advance the cursors of multiple /// buffers. /// + /// # Panics + /// + /// Panics when trying to advance beyond the end of the slice. + /// /// # Examples /// /// ``` @@ -1228,8 +1240,8 @@ pub fn new(buf: &'a [u8]) -> IoSlice<'a> { /// use std::io::IoSlice; /// use std::ops::Deref; /// - /// let mut data = [1; 8]; - /// let mut buf = IoSlice::new(&mut data); + /// let data = [1; 8]; + /// let mut buf = IoSlice::new(&data); /// /// // Mark 3 bytes as read. /// buf.advance(3); @@ -1241,15 +1253,18 @@ pub fn advance(&mut self, n: usize) { self.0.advance(n) } - /// Advance the internal cursor of the slices. + /// Advance a slice of slices. /// - /// # Notes + /// Shrinks the slice to remove any `IoSlice`s that are fully advanced over. + /// If the cursor ends up in the middle of an `IoSlice`, it is modified + /// to start at that cursor. /// - /// Elements in the slice may be modified if the cursor is not advanced to - /// the end of the slice. For example if we have a slice of buffers with 2 - /// `IoSlice`s, both of length 8, and we advance the cursor by 10 bytes the - /// first `IoSlice` will be untouched however the second will be modified to - /// remove the first 2 bytes (10 - 8). + /// For example, if we have a slice of two 8-byte `IoSlice`s, and we advance by 10 bytes, + /// the result will only include the second `IoSlice`, advanced by 2 bytes. + /// + /// # Panics + /// + /// Panics when trying to advance beyond the end of the slices. /// /// # Examples /// @@ -1289,7 +1304,9 @@ pub fn advance_slices(bufs: &mut &mut [IoSlice<'a>], n: usize) { } *bufs = &mut replace(bufs, &mut [])[remove..]; - if !bufs.is_empty() { + if bufs.is_empty() { + assert!(n == accumulated_len, "advancing io slices beyond their length"); + } else { bufs[0].advance(n - accumulated_len) } } @@ -1418,10 +1435,10 @@ pub trait Write { /// use std::fs::File; /// /// fn main() -> std::io::Result<()> { - /// let mut data1 = [1; 8]; - /// let mut data2 = [15; 8]; - /// let io_slice1 = IoSlice::new(&mut data1); - /// let io_slice2 = IoSlice::new(&mut data2); + /// let data1 = [1; 8]; + /// let data2 = [15; 8]; + /// let io_slice1 = IoSlice::new(&data1); + /// let io_slice2 = IoSlice::new(&data2); /// /// let mut buffer = File::create("foo.txt")?; /// @@ -2110,7 +2127,8 @@ fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { } /// Read all bytes until a newline (the `0xA` byte) is reached, and append - /// them to the provided buffer. + /// them to the provided buffer. You do not need to clear the buffer before + /// appending. /// /// This function will read bytes from the underlying stream until the /// newline delimiter (the `0xA` byte) or EOF is found. Once found, all bytes