Skip to content

Commit 6bf1dfd

Browse files
committed
Implement TrustedRandomAccess for slice::{ExactChunks, ExactChunksMut}
1 parent cea36f4 commit 6bf1dfd

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/libcore/slice/mod.rs

+18
Original file line numberDiff line numberDiff line change
@@ -2484,6 +2484,15 @@ impl<'a, T> ExactSizeIterator for ExactChunks<'a, T> {
24842484
#[unstable(feature = "fused", issue = "35602")]
24852485
impl<'a, T> FusedIterator for ExactChunks<'a, T> {}
24862486

2487+
#[doc(hidden)]
2488+
unsafe impl<'a, T> TrustedRandomAccess for ExactChunks<'a, T> {
2489+
unsafe fn get_unchecked(&mut self, i: usize) -> &'a [T] {
2490+
let start = i * self.chunk_size;
2491+
from_raw_parts(self.v.as_ptr().offset(start as isize), self.chunk_size)
2492+
}
2493+
fn may_have_side_effect() -> bool { false }
2494+
}
2495+
24872496
/// An iterator over a slice in (non-overlapping) mutable chunks (`chunk_size`
24882497
/// elements at a time). When the slice len is not evenly divided by the chunk
24892498
/// size, the last up to `chunk_size-1` elements will be omitted.
@@ -2572,6 +2581,15 @@ impl<'a, T> ExactSizeIterator for ExactChunksMut<'a, T> {
25722581
#[unstable(feature = "fused", issue = "35602")]
25732582
impl<'a, T> FusedIterator for ExactChunksMut<'a, T> {}
25742583

2584+
#[doc(hidden)]
2585+
unsafe impl<'a, T> TrustedRandomAccess for ExactChunksMut<'a, T> {
2586+
unsafe fn get_unchecked(&mut self, i: usize) -> &'a mut [T] {
2587+
let start = i * self.chunk_size;
2588+
from_raw_parts_mut(self.v.as_mut_ptr().offset(start as isize), self.chunk_size)
2589+
}
2590+
fn may_have_side_effect() -> bool { false }
2591+
}
2592+
25752593
//
25762594
// Free functions
25772595
//

0 commit comments

Comments
 (0)