Skip to content

Commit e81fefa

Browse files
thomccthe8472
andcommitted
Address some issues in chunk iterator safety comments
Co-authored-by: the8472 <[email protected]>
1 parent 83aa6d4 commit e81fefa

File tree

1 file changed

+9
-25
lines changed

1 file changed

+9
-25
lines changed

library/core/src/slice/iter.rs

+9-25
Original file line numberDiff line numberDiff line change
@@ -1475,22 +1475,19 @@ impl<'a, T> DoubleEndedIterator for Chunks<'a, T> {
14751475
let remainder = self.v.len() % self.chunk_size;
14761476
let chunksz = if remainder != 0 { remainder } else { self.chunk_size };
14771477
// SAFETY: split_at_unchecked requires the argument be less than or
1478-
// equal to the length. This is guaranteed, but subtle: We need the
1479-
// expression `self.v.len() - sz` not to overflow, which means we
1480-
// need `sz >= tmp_len`.
1481-
//
1482-
// `sz` will always either be `self.v.len() % self.chunk_size`,
1483-
// which will always evaluate to strictly less than `self.v.len()`
1484-
// (or panic, in the case that `self.chunk_size` is zero), or it can
1485-
// be `self.chunk_size`, in the case that the length is exactly
1478+
// equal to the length. This is guaranteed, but subtle: `chunksz`
1479+
// will always either be `self.v.len() % self.chunk_size`, which
1480+
// will always evaluate to strictly less than `self.v.len()` (or
1481+
// panic, in the case that `self.chunk_size` is zero), or it can be
1482+
// `self.chunk_size`, in the case that the length is exactly
14861483
// divisible by the chunk size.
14871484
//
14881485
// While it seems like using `self.chunk_size` in this case could
14891486
// lead to a value greater than `self.v.len()`, it cannot: if
14901487
// `self.chunk_size` were greater than `self.v.len()`, then
1491-
// `self.v.len() % self.chunk_size` would have returned non-zero
1492-
// (note that in this branch of the `if`, we already know that
1493-
// `self.v` is non-empty).
1488+
// `self.v.len() % self.chunk_size` would return nonzero (note that
1489+
// in this branch of the `if`, we already know that `self.v` is
1490+
// non-empty).
14941491
let (fst, snd) = unsafe { self.v.split_at_unchecked(self.v.len() - chunksz) };
14951492
self.v = fst;
14961493
Some(snd)
@@ -2524,20 +2521,7 @@ impl<'a, T> DoubleEndedIterator for RChunks<'a, T> {
25242521
} else {
25252522
let remainder = self.v.len() % self.chunk_size;
25262523
let chunksz = if remainder != 0 { remainder } else { self.chunk_size };
2527-
// SAFETY: split_at_unchecked requires the argument be less than or
2528-
// equal to the length. This is guaranteed, but subtle: `chunksz`
2529-
// will always either be `self.v.len() % self.chunk_size`, which
2530-
// will always evaluate to strictly less than `self.v.len()` (or
2531-
// panic, in the case that `self.chunk_size` is zero), or it can be
2532-
// `self.chunk_size`, in the case that the length is exactly
2533-
// divisible by the chunk size.
2534-
//
2535-
// While it seems like using `self.chunk_size` in this case could
2536-
// lead to a value greater than `self.v.len()`, it cannot: if
2537-
// `self.chunk_size` were greater than `self.v.len()`, then
2538-
// `self.v.len() % self.chunk_size` would return nonzero (note that
2539-
// in this branch of the `if`, we already know that `self.v` is
2540-
// non-empty).
2524+
// SAFETY: similar to Chunks::next_back
25412525
let (fst, snd) = unsafe { self.v.split_at_unchecked(chunksz) };
25422526
self.v = snd;
25432527
Some(fst)

0 commit comments

Comments
 (0)