diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-08-20 16:26:37 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-08-20 16:26:37 +0200 |
| commit | 60649e3a2efe34f8221e53e6cc0233d2dbd147ab (patch) | |
| tree | 8dd0e68c069706340383f3ed391fc0e0334d03ba /src/libcore/slice | |
| parent | a5299dd5d1c517b8a2da463ec463abec68b08b33 (diff) | |
| parent | d3019d16b992105d5948fae4a4fb17337ccac177 (diff) | |
| download | rust-60649e3a2efe34f8221e53e6cc0233d2dbd147ab.tar.gz rust-60649e3a2efe34f8221e53e6cc0233d2dbd147ab.zip | |
Rollup merge of #63265 - JohnTitor:implement-nth-back-for-chunksexactmut, r=scottmcm
Implement `nth_back` for ChunksExactMut This is a part of #54054. r? @scottmcm
Diffstat (limited to 'src/libcore/slice')
| -rw-r--r-- | src/libcore/slice/mod.rs | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs index ce5af13d4ca..bfbbb15c8d4 100644 --- a/src/libcore/slice/mod.rs +++ b/src/libcore/slice/mod.rs @@ -4637,6 +4637,22 @@ impl<'a, T> DoubleEndedIterator for ChunksExactMut<'a, T> { Some(tail) } } + + #[inline] + fn nth_back(&mut self, n: usize) -> Option<Self::Item> { + let len = self.len(); + if n >= len { + self.v = &mut []; + None + } else { + let start = (len - 1 - n) * self.chunk_size; + let end = start + self.chunk_size; + let (temp, _tail) = mem::replace(&mut self.v, &mut []).split_at_mut(end); + let (head, nth_back) = temp.split_at_mut(start); + self.v = head; + Some(nth_back) + } + } } #[stable(feature = "chunks_exact", since = "1.31.0")] |
