提交 37a5b515 编写于 作者: T The8472

implement TrustedRandomAccess for Take iterator adapter

上级 0bbf4731
use crate::cmp; use crate::cmp;
use crate::iter::{adapters::SourceIter, FusedIterator, InPlaceIterable, TrustedLen}; use crate::iter::{
adapters::zip::try_get_unchecked, adapters::SourceIter, FusedIterator, InPlaceIterable,
TrustedLen, TrustedRandomAccess,
};
use crate::ops::{ControlFlow, Try}; use crate::ops::{ControlFlow, Try};
/// An iterator that only iterates over the first `n` iterations of `iter`. /// An iterator that only iterates over the first `n` iterations of `iter`.
...@@ -111,6 +114,15 @@ fn ok<B, T>(mut f: impl FnMut(B, T) -> B) -> impl FnMut(B, T) -> Result<B, !> { ...@@ -111,6 +114,15 @@ fn ok<B, T>(mut f: impl FnMut(B, T) -> B) -> impl FnMut(B, T) -> Result<B, !> {
self.try_fold(init, ok(fold)).unwrap() self.try_fold(init, ok(fold)).unwrap()
} }
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> <I as Iterator>::Item
where
Self: TrustedRandomAccess,
{
// SAFETY: the caller must uphold the contract for
// `Iterator::__iterator_get_unchecked`.
unsafe { try_get_unchecked(&mut self.iter, idx) }
}
} }
#[unstable(issue = "none", feature = "inplace_iteration")] #[unstable(issue = "none", feature = "inplace_iteration")]
...@@ -207,3 +219,12 @@ impl<I> FusedIterator for Take<I> where I: FusedIterator {} ...@@ -207,3 +219,12 @@ impl<I> FusedIterator for Take<I> where I: FusedIterator {}
#[unstable(feature = "trusted_len", issue = "37572")] #[unstable(feature = "trusted_len", issue = "37572")]
unsafe impl<I: TrustedLen> TrustedLen for Take<I> {} unsafe impl<I: TrustedLen> TrustedLen for Take<I> {}
#[doc(hidden)]
#[unstable(feature = "trusted_random_access", issue = "none")]
unsafe impl<I> TrustedRandomAccess for Take<I>
where
I: TrustedRandomAccess,
{
const MAY_HAVE_SIDE_EFFECT: bool = I::MAY_HAVE_SIDE_EFFECT;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册