Rollup merge of #94855 - m-ou-se:advance-slice-panic-docs, r=kennytm
Panic when advance_slices()'ing too far and update docs. This updates advance_slices() to panic when advancing too far, like advance() already does. And updates the docs to say so. See https://github.com/rust-lang/rust/issues/62726#issuecomment-1065253213
This commit is contained in:
@@ -1084,6 +1084,10 @@ impl<'a> IoSliceMut<'a> {
|
|||||||
/// Also see [`IoSliceMut::advance_slices`] to advance the cursors of
|
/// Also see [`IoSliceMut::advance_slices`] to advance the cursors of
|
||||||
/// multiple buffers.
|
/// multiple buffers.
|
||||||
///
|
///
|
||||||
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// Panics when trying to advance beyond the end of the slice.
|
||||||
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
@@ -1105,15 +1109,18 @@ impl<'a> IoSliceMut<'a> {
|
|||||||
self.0.advance(n)
|
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
|
/// For example, if we have a slice of two 8-byte `IoSliceMut`s, and we advance by 10 bytes,
|
||||||
/// the end of the slice. For example if we have a slice of buffers with 2
|
/// the result will only include the second `IoSliceMut`, advanced by 2 bytes.
|
||||||
/// `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
|
/// # Panics
|
||||||
/// modified to remove the first 2 bytes (10 - 8).
|
///
|
||||||
|
/// Panics when trying to advance beyond the end of the slices.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
@@ -1154,7 +1161,9 @@ impl<'a> IoSliceMut<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
*bufs = &mut replace(bufs, &mut [])[remove..];
|
*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)
|
bufs[0].advance(n - accumulated_len)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1219,6 +1228,10 @@ impl<'a> IoSlice<'a> {
|
|||||||
/// Also see [`IoSlice::advance_slices`] to advance the cursors of multiple
|
/// Also see [`IoSlice::advance_slices`] to advance the cursors of multiple
|
||||||
/// buffers.
|
/// buffers.
|
||||||
///
|
///
|
||||||
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// Panics when trying to advance beyond the end of the slice.
|
||||||
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
@@ -1240,15 +1253,18 @@ impl<'a> IoSlice<'a> {
|
|||||||
self.0.advance(n)
|
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
|
/// For example, if we have a slice of two 8-byte `IoSlice`s, and we advance by 10 bytes,
|
||||||
/// the end of the slice. For example if we have a slice of buffers with 2
|
/// the result will only include the second `IoSlice`, advanced by 2 bytes.
|
||||||
/// `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
|
/// # Panics
|
||||||
/// remove the first 2 bytes (10 - 8).
|
///
|
||||||
|
/// Panics when trying to advance beyond the end of the slices.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
@@ -1288,7 +1304,9 @@ impl<'a> IoSlice<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
*bufs = &mut replace(bufs, &mut [])[remove..];
|
*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)
|
bufs[0].advance(n - accumulated_len)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -423,18 +423,18 @@ fn io_slice_mut_advance_slices() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
fn io_slice_mut_advance_slices_empty_slice() {
|
fn io_slice_mut_advance_slices_empty_slice() {
|
||||||
let mut empty_bufs = &mut [][..];
|
let mut empty_bufs = &mut [][..];
|
||||||
// Shouldn't panic.
|
|
||||||
IoSliceMut::advance_slices(&mut empty_bufs, 1);
|
IoSliceMut::advance_slices(&mut empty_bufs, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
fn io_slice_mut_advance_slices_beyond_total_length() {
|
fn io_slice_mut_advance_slices_beyond_total_length() {
|
||||||
let mut buf1 = [1; 8];
|
let mut buf1 = [1; 8];
|
||||||
let mut bufs = &mut [IoSliceMut::new(&mut buf1)][..];
|
let mut bufs = &mut [IoSliceMut::new(&mut buf1)][..];
|
||||||
|
|
||||||
// Going beyond the total length should be ok.
|
|
||||||
IoSliceMut::advance_slices(&mut bufs, 9);
|
IoSliceMut::advance_slices(&mut bufs, 9);
|
||||||
assert!(bufs.is_empty());
|
assert!(bufs.is_empty());
|
||||||
}
|
}
|
||||||
@@ -463,18 +463,18 @@ fn io_slice_advance_slices() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
fn io_slice_advance_slices_empty_slice() {
|
fn io_slice_advance_slices_empty_slice() {
|
||||||
let mut empty_bufs = &mut [][..];
|
let mut empty_bufs = &mut [][..];
|
||||||
// Shouldn't panic.
|
|
||||||
IoSlice::advance_slices(&mut empty_bufs, 1);
|
IoSlice::advance_slices(&mut empty_bufs, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
fn io_slice_advance_slices_beyond_total_length() {
|
fn io_slice_advance_slices_beyond_total_length() {
|
||||||
let buf1 = [1; 8];
|
let buf1 = [1; 8];
|
||||||
let mut bufs = &mut [IoSlice::new(&buf1)][..];
|
let mut bufs = &mut [IoSlice::new(&buf1)][..];
|
||||||
|
|
||||||
// Going beyond the total length should be ok.
|
|
||||||
IoSlice::advance_slices(&mut bufs, 9);
|
IoSlice::advance_slices(&mut bufs, 9);
|
||||||
assert!(bufs.is_empty());
|
assert!(bufs.is_empty());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user