diff options
| author | The8472 <git@infinite-source.de> | 2021-01-31 21:16:08 +0100 |
|---|---|---|
| committer | The8472 <git@infinite-source.de> | 2021-03-21 20:43:48 +0100 |
| commit | 08a1dd287d371b8df7fbef610e66bc925b3eea0b (patch) | |
| tree | 382300bb036895c3e2817d114eb061a323d21a99 /library/core/src/array | |
| parent | 895d7a9a096aab4690a032cee28f57f4b8a0e9ac (diff) | |
| download | rust-08a1dd287d371b8df7fbef610e66bc925b3eea0b.tar.gz rust-08a1dd287d371b8df7fbef610e66bc925b3eea0b.zip | |
implement TrustedRandomAccess for array::IntoIter
Diffstat (limited to 'library/core/src/array')
| -rw-r--r-- | library/core/src/array/iter.rs | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/library/core/src/array/iter.rs b/library/core/src/array/iter.rs index 4472fba26b9..f82454addd0 100644 --- a/library/core/src/array/iter.rs +++ b/library/core/src/array/iter.rs @@ -2,7 +2,7 @@ use crate::{ fmt, - iter::{ExactSizeIterator, FusedIterator, TrustedLen}, + iter::{ExactSizeIterator, FusedIterator, TrustedLen, TrustedRandomAccess}, mem::{self, MaybeUninit}, ops::Range, ptr, @@ -130,6 +130,18 @@ impl<T, const N: usize> Iterator for IntoIter<T, N> { fn last(mut self) -> Option<Self::Item> { self.next_back() } + + #[inline] + unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item + where + Self: TrustedRandomAccess, + { + // SAFETY: Callers are only allowed to pass an index that is in bounds + // Additionally Self: TrustedRandomAccess is only implemented for T: Copy which means even + // multiple repeated reads of the same index would be safe and the + // values aree !Drop, thus won't suffer from double drops. + unsafe { self.data.get_unchecked(self.alive.start + idx).assume_init_read() } + } } #[stable(feature = "array_value_iter_impls", since = "1.40.0")] @@ -184,6 +196,17 @@ impl<T, const N: usize> FusedIterator for IntoIter<T, N> {} #[stable(feature = "array_value_iter_impls", since = "1.40.0")] unsafe impl<T, const N: usize> TrustedLen for IntoIter<T, N> {} +#[doc(hidden)] +#[unstable(feature = "trusted_random_access", issue = "none")] +// T: Copy as approximation for !Drop since get_unchecked does not update the pointers +// and thus we can't implement drop-handling +unsafe impl<T, const N: usize> TrustedRandomAccess for IntoIter<T, N> +where + T: Copy, +{ + const MAY_HAVE_SIDE_EFFECT: bool = false; +} + #[stable(feature = "array_value_iter_impls", since = "1.40.0")] impl<T: Clone, const N: usize> Clone for IntoIter<T, N> { fn clone(&self) -> Self { |
