Skip to content

Commit 48f2f71

Browse files
committed
Implement TrustedRandomAccess for slice::{Chunks, ChunksMut, Windows}
1 parent b107f72 commit 48f2f71

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/libcore/slice/mod.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2117,6 +2117,14 @@ impl<'a, T> ExactSizeIterator for Windows<'a, T> {}
21172117
#[unstable(feature = "fused", issue = "35602")]
21182118
impl<'a, T> FusedIterator for Windows<'a, T> {}
21192119

2120+
#[doc(hidden)]
2121+
unsafe impl<'a, T> TrustedRandomAccess for Windows<'a, T> {
2122+
unsafe fn get_unchecked(&mut self, i: usize) -> &'a [T] {
2123+
from_raw_parts(self.v.as_ptr().offset(i as isize), self.size)
2124+
}
2125+
fn may_have_side_effect() -> bool { false }
2126+
}
2127+
21202128
/// An iterator over a slice in (non-overlapping) chunks (`chunk_size` elements at a
21212129
/// time).
21222130
///
@@ -2228,6 +2236,16 @@ impl<'a, T> ExactSizeIterator for Chunks<'a, T> {}
22282236
#[unstable(feature = "fused", issue = "35602")]
22292237
impl<'a, T> FusedIterator for Chunks<'a, T> {}
22302238

2239+
#[doc(hidden)]
2240+
unsafe impl<'a, T> TrustedRandomAccess for Chunks<'a, T> {
2241+
unsafe fn get_unchecked(&mut self, i: usize) -> &'a [T] {
2242+
let start = i * self.size;
2243+
let end = cmp::min(start + self.size, self.v.len());
2244+
from_raw_parts(self.v.as_ptr().offset(start as isize), end - start)
2245+
}
2246+
fn may_have_side_effect() -> bool { false }
2247+
}
2248+
22312249
/// An iterator over a slice in (non-overlapping) mutable chunks (`chunk_size`
22322250
/// elements at a time). When the slice len is not evenly divided by the chunk
22332251
/// size, the last slice of the iteration will be the remainder.
@@ -2331,6 +2349,16 @@ impl<'a, T> ExactSizeIterator for ChunksMut<'a, T> {}
23312349
#[unstable(feature = "fused", issue = "35602")]
23322350
impl<'a, T> FusedIterator for ChunksMut<'a, T> {}
23332351

2352+
#[doc(hidden)]
2353+
unsafe impl<'a, T> TrustedRandomAccess for ChunksMut<'a, T> {
2354+
unsafe fn get_unchecked(&mut self, i: usize) -> &'a mut [T] {
2355+
let start = i * self.chunk_size;
2356+
let end = cmp::min(start + self.chunk_size, self.v.len());
2357+
from_raw_parts_mut(self.v.as_mut_ptr().offset(start as isize), end - start)
2358+
}
2359+
fn may_have_side_effect() -> bool { false }
2360+
}
2361+
23342362
//
23352363
// Free functions
23362364
//

0 commit comments

Comments
 (0)