Skip to content

Commit 37a5b51

Browse files
committed
implement TrustedRandomAccess for Take iterator adapter
1 parent 0bbf473 commit 37a5b51

File tree

1 file changed

+22
-1
lines changed
  • library/core/src/iter/adapters

1 file changed

+22
-1
lines changed

library/core/src/iter/adapters/take.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use crate::cmp;
2-
use crate::iter::{adapters::SourceIter, FusedIterator, InPlaceIterable, TrustedLen};
2+
use crate::iter::{
3+
adapters::zip::try_get_unchecked, adapters::SourceIter, FusedIterator, InPlaceIterable,
4+
TrustedLen, TrustedRandomAccess,
5+
};
36
use crate::ops::{ControlFlow, Try};
47

58
/// An iterator that only iterates over the first `n` iterations of `iter`.
@@ -111,6 +114,15 @@ where
111114

112115
self.try_fold(init, ok(fold)).unwrap()
113116
}
117+
118+
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> <I as Iterator>::Item
119+
where
120+
Self: TrustedRandomAccess,
121+
{
122+
// SAFETY: the caller must uphold the contract for
123+
// `Iterator::__iterator_get_unchecked`.
124+
unsafe { try_get_unchecked(&mut self.iter, idx) }
125+
}
114126
}
115127

116128
#[unstable(issue = "none", feature = "inplace_iteration")]
@@ -207,3 +219,12 @@ impl<I> FusedIterator for Take<I> where I: FusedIterator {}
207219

208220
#[unstable(feature = "trusted_len", issue = "37572")]
209221
unsafe impl<I: TrustedLen> TrustedLen for Take<I> {}
222+
223+
#[doc(hidden)]
224+
#[unstable(feature = "trusted_random_access", issue = "none")]
225+
unsafe impl<I> TrustedRandomAccess for Take<I>
226+
where
227+
I: TrustedRandomAccess,
228+
{
229+
const MAY_HAVE_SIDE_EFFECT: bool = I::MAY_HAVE_SIDE_EFFECT;
230+
}

0 commit comments

Comments
 (0)