Skip to content

Commit b543816

Browse files
committed
Reference Chunks::next_back in more of the chunk iterators safety comments
1 parent e81fefa commit b543816

File tree

1 file changed

+2
-34
lines changed

1 file changed

+2
-34
lines changed

library/core/src/slice/iter.rs

+2-34
Original file line numberDiff line numberDiff line change
@@ -1653,25 +1653,7 @@ impl<'a, T> DoubleEndedIterator for ChunksMut<'a, T> {
16531653
let sz = if remainder != 0 { remainder } else { self.chunk_size };
16541654
let tmp = mem::replace(&mut self.v, &mut []);
16551655
let tmp_len = tmp.len();
1656-
// SAFETY: split_at_unchecked requires the argument be less than or
1657-
// equal to the length. This is guaranteed, but subtle: We need the
1658-
// expression `tmp_len - sz` not to overflow, which means we need
1659-
// `sz >= tmp_len`.
1660-
//
1661-
// `sz` will always either be `tmp_or_v.len() % self.chunk_size`
1662-
// (where `tmp_or_v` is the slice that at the time was `self.v` but
1663-
// now is `tmp`, and thus `tmp_len` and `tmp_or_v.len()` are the
1664-
// same), which will always evaluate to strictly less than
1665-
// `tmp_or_v.len()` (or panic, in the case that `self.chunk_size` is
1666-
// zero), or it can be `self.chunk_size`, in the case that the
1667-
// length is exactly divisible by the chunk size.
1668-
//
1669-
// While it seems like using `self.chunk_size` in this case could
1670-
// lead to a value greater than `tmp_len`, it cannot: if
1671-
// `self.chunk_size` were greater than `tmp_len`, then
1672-
// `tmp_or_v.len() % self.chunk_size` would have returned non-zero
1673-
// (note that in this branch of the `if`, we already know that
1674-
// `self.v` is non-empty).
1656+
// SAFETY: Similar to `Chunks::next_back`
16751657
let (head, tail) = unsafe { tmp.split_at_mut_unchecked(tmp_len - sz) };
16761658
self.v = head;
16771659
Some(tail)
@@ -2691,21 +2673,7 @@ impl<'a, T> DoubleEndedIterator for RChunksMut<'a, T> {
26912673
let remainder = self.v.len() % self.chunk_size;
26922674
let sz = if remainder != 0 { remainder } else { self.chunk_size };
26932675
let tmp = mem::replace(&mut self.v, &mut []);
2694-
// SAFETY: split_at_mut_unchecked requires the argument be less than
2695-
// or equal to the length. This is guaranteed, but subtle: `chunksz`
2696-
// will always either be `tmp_or_v.len() % self.chunk_size` (where
2697-
// `tmp_or_v` is the slice that at the time was `self.v` but now is
2698-
// `tmp`), which will always evaluate to strictly less than
2699-
// `tmp_or_v.len()` (or panic, in the case that `self.chunk_size` is
2700-
// zero), or it can be `self.chunk_size`, in the case that the
2701-
// length is exactly divisible by the chunk size.
2702-
//
2703-
// While it seems like using `self.chunk_size` in this case could
2704-
// lead to a value greater than `tmp_or_v.len()`, it cannot: if
2705-
// `self.chunk_size` were greater than `tmp_or_v.len()`, then
2706-
// `tmp_or_v.len() % self.chunk_size` would return nonzero (note
2707-
// that in this branch of the `if`, we already know that `tmp_or_v`
2708-
// is non-empty).
2676+
// SAFETY: Similar to `Chunks::next_back`
27092677
let (head, tail) = unsafe { tmp.split_at_mut_unchecked(sz) };
27102678
self.v = tail;
27112679
Some(head)

0 commit comments

Comments
 (0)