blob: d5fcc782d169af3fba72d57bda50c6c4246545d5 [file] [log] [blame]
#[cfg(__standback_before_1_35)]
use crate::prelude::*;
use core::iter::FusedIterator;
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[derive(Clone, Debug)]
pub struct Copied<I> {
it: I,
}
impl<I> Copied<I> {
fn new(it: I) -> Copied<I> {
Copied { it }
}
}
fn copy_fold<T: Copy, Acc>(mut f: impl FnMut(Acc, T) -> Acc) -> impl FnMut(Acc, &T) -> Acc {
move |acc, &elt| f(acc, elt)
}
impl<'a, I, T: 'a> Iterator for Copied<I>
where
I: Iterator<Item = &'a T>,
T: Copy,
{
type Item = T;
fn next(&mut self) -> Option<T> {
self.it.next().copied()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.it.size_hint()
}
fn fold<Acc, F>(self, init: Acc, f: F) -> Acc
where
F: FnMut(Acc, Self::Item) -> Acc,
{
self.it.fold(init, copy_fold(f))
}
}
impl<'a, I, T: 'a> DoubleEndedIterator for Copied<I>
where
I: DoubleEndedIterator<Item = &'a T>,
T: Copy,
{
fn next_back(&mut self) -> Option<T> {
self.it.next_back().copied()
}
fn rfold<Acc, F>(self, init: Acc, f: F) -> Acc
where
F: FnMut(Acc, Self::Item) -> Acc,
{
self.it.rfold(init, copy_fold(f))
}
}
impl<'a, I, T: 'a> ExactSizeIterator for Copied<I>
where
I: ExactSizeIterator<Item = &'a T>,
T: Copy,
{
fn len(&self) -> usize {
self.it.len()
}
}
impl<'a, I, T: 'a> FusedIterator for Copied<I>
where
I: FusedIterator<Item = &'a T>,
T: Copy,
{
}
pub trait Iterator_v1_36: Iterator {
fn copied<'a, T: 'a>(self) -> Copied<Self>
where
Self: Sized + Iterator<Item = &'a T>,
T: Copy;
}
impl<Iter: Iterator> Iterator_v1_36 for Iter {
fn copied<'a, T: 'a>(self) -> Copied<Self>
where
Self: Sized + Iterator<Item = &'a T>,
T: Copy,
{
Copied::new(self)
}
}