|
| 1 | +use crate::fmt; |
| 2 | +use crate::iter::adapters::{ |
| 3 | + zip::try_get_unchecked, SourceIter, TrustedRandomAccess, TrustedRandomAccessNoCoerce, |
| 4 | +}; |
| 5 | +use crate::iter::{ |
| 6 | + FusedIterator, InPlaceIterable, Peekable, TrustedFused, TrustedLen, UncheckedIterator, |
| 7 | +}; |
| 8 | +use crate::num::NonZeroUsize; |
| 9 | +use crate::ops::Try; |
| 10 | + |
| 11 | +/// An iterator that maps the values of `iter` with `f`. |
| 12 | +#[must_use = "iterators are lazy and do nothing unless consumed"] |
| 13 | +#[stable(feature = "rust1", since = "1.0.0")] |
| 14 | +pub struct PeekMap<I, F> { |
| 15 | + // Used for `SplitWhitespace` and `SplitAsciiWhitespace` `as_str` methods |
| 16 | + pub(crate) peekable: Peekable<I>, |
| 17 | + f: F, |
| 18 | +} |
| 19 | + |
| 20 | +impl<I, F> PeekMap<I, F> { |
| 21 | + pub(in crate::iter) fn new(peekable: Peekable<I>, f: F) -> PeekMap<I, F> { |
| 22 | + PeekMap { peekable, f } |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +#[stable(feature = "rust1", since = "1.0.0")] |
| 27 | +impl<B, I: Iterator, F> Iterator for PeekMap<I, F> |
| 28 | +where |
| 29 | + F: FnMut(I::Item, Option<&I::Item>) -> B, |
| 30 | +{ |
| 31 | + type Item = B; |
| 32 | + |
| 33 | + #[inline] |
| 34 | + fn next(&mut self) -> Option<B> { |
| 35 | + self.peekable.next().map(|f| (f, self.peekable.peek())).map(&mut self.f) |
| 36 | + } |
| 37 | + |
| 38 | + #[inline] |
| 39 | + fn size_hint(&self) -> (usize, Option<usize>) { |
| 40 | + self.peekable.size_hint() |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +#[stable(feature = "rust1", since = "1.0.0")] |
| 45 | +impl<B, I: ExactSizeIterator, F> ExactSizeIterator for PeekMap<I, F> |
| 46 | +where |
| 47 | + F: FnMut(I::Item, Option<&I::Item>) -> B, |
| 48 | +{ |
| 49 | + fn len(&self) -> usize { |
| 50 | + self.peekable.len() |
| 51 | + } |
| 52 | + |
| 53 | + fn is_empty(&self) -> bool { |
| 54 | + self.peekable.is_empty() |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +#[stable(feature = "fused", since = "1.26.0")] |
| 59 | +impl<B, I: FusedIterator, F> FusedIterator for PeekMap<I, F> where |
| 60 | + F: FnMut(I::Item, Option<&I::Item>) -> B |
| 61 | +{ |
| 62 | +} |
| 63 | + |
| 64 | +#[unstable(issue = "none", feature = "trusted_fused")] |
| 65 | +unsafe impl<I: TrustedFused, F> TrustedFused for PeekMap<I, F> {} |
| 66 | + |
| 67 | +#[unstable(feature = "trusted_len", issue = "37572")] |
| 68 | +unsafe impl<B, I, F> TrustedLen for PeekMap<I, F> |
| 69 | +where |
| 70 | + I: TrustedLen, |
| 71 | + F: FnMut(I::Item, Option<&I::Item>) -> B, |
| 72 | +{ |
| 73 | +} |
| 74 | + |
| 75 | +#[doc(hidden)] |
| 76 | +#[unstable(feature = "trusted_random_access", issue = "none")] |
| 77 | +unsafe impl<I, F> TrustedRandomAccess for PeekMap<I, F> where I: TrustedRandomAccess {} |
| 78 | + |
| 79 | +#[doc(hidden)] |
| 80 | +#[unstable(feature = "trusted_random_access", issue = "none")] |
| 81 | +unsafe impl<I, F> TrustedRandomAccessNoCoerce for PeekMap<I, F> |
| 82 | +where |
| 83 | + I: TrustedRandomAccessNoCoerce, |
| 84 | +{ |
| 85 | + const MAY_HAVE_SIDE_EFFECT: bool = true; |
| 86 | +} |
| 87 | + |
| 88 | +#[unstable(issue = "none", feature = "inplace_iteration")] |
| 89 | +unsafe impl<I, F> SourceIter for PeekMap<I, F> |
| 90 | +where |
| 91 | + I: SourceIter, |
| 92 | +{ |
| 93 | + type Source = I::Source; |
| 94 | + |
| 95 | + #[inline] |
| 96 | + unsafe fn as_inner(&mut self) -> &mut I::Source { |
| 97 | + // SAFETY: unsafe function forwarding to unsafe function with the same requirements |
| 98 | + unsafe { SourceIter::as_inner(&mut self.peekable) } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +#[unstable(issue = "none", feature = "inplace_iteration")] |
| 103 | +unsafe impl<I: InPlaceIterable, F> InPlaceIterable for PeekMap<I, F> { |
| 104 | + const EXPAND_BY: Option<NonZeroUsize> = I::EXPAND_BY; |
| 105 | + const MERGE_BY: Option<NonZeroUsize> = I::MERGE_BY; |
| 106 | +} |
0 commit comments