mod adapters;
pub use adapters::*;
unsafe fn change_lifetime<'a, 'b, I: ?Sized + Iterator>(i: I::Item<'a>) -> I::Item<'b> {
unsafe { core::mem::transmute::<I::Item<'a>, I::Item<'b>>(i) }
}
pub trait Iterator {
type Item<'a>
where
Self: 'a;
fn next(&mut self) -> Option<Self::Item<'_>>;
fn size_hint(&self) -> (usize, Option<usize>) {
(0, None)
}
fn advance_by(&mut self, n: usize) -> Result<(), usize> {
let mut idx = 0;
while idx < n {
if self.next().is_none() {
return Err(idx);
}
idx += 1;
}
Ok(())
}
fn nth(&mut self, mut n: usize) -> Option<Self::Item<'_>> {
while n > 0 {
self.next()?;
n -= 1;
}
self.next()
}
fn map<O, F>(self, f: F) -> Map<Self, F>
where
Self: Sized,
F: FnMut(Self::Item<'_>) -> O,
{
Map::new(self, f)
}
fn touch<F>(self, f: F) -> Touch<Self, F>
where
Self: Sized,
F: FnMut(&mut Self::Item<'_>),
{
Touch::new(self, f)
}
fn filter<F>(self, f: F) -> Filter<Self, F>
where
Self: Sized,
F: FnMut(&Self::Item<'_>) -> bool,
{
Filter::new(self, f)
}
fn step_by(self, step: usize) -> StepBy<Self>
where
Self: Sized,
{
assert_ne!(step, 0);
StepBy::new(self, step)
}
fn chain<U>(self, other: U) -> Chain<Self, U::IntoIter>
where
Self: Sized,
U: IntoIterator,
U::IntoIter: for<'a> Iterator<Item<'a> = Self::Item<'a>>,
{
Chain::new(self, other.into_iter())
}
fn zip<U>(self, other: U) -> Zip<Self, U::IntoIter>
where
Self: Sized,
U: IntoIterator,
{
Zip::new(self, other.into_iter())
}
fn enumerate(self) -> Enumerate<Self>
where
Self: Sized,
{
Enumerate::new(self)
}
fn skip_while<F>(self, f: F) -> SkipWhile<Self, F>
where
Self: Sized,
F: FnMut(&Self::Item<'_>) -> bool,
{
SkipWhile::new(self, f)
}
fn take_while<F>(self, f: F) -> TakeWhile<Self, F>
where
Self: Sized,
F: FnMut(&Self::Item<'_>) -> bool,
{
TakeWhile::new(self, f)
}
fn skip(self, n: usize) -> Skip<Self>
where
Self: Sized,
{
Skip::new(self, n)
}
fn take(self, n: usize) -> Take<Self>
where
Self: Sized,
{
Take::new(self, n)
}
fn all<F>(&mut self, mut f: F) -> bool
where
F: FnMut(Self::Item<'_>) -> bool,
{
while let Some(val) = self.next() {
if !f(val) {
return false;
}
}
true
}
fn any<F>(&mut self, mut f: F) -> bool
where
F: FnMut(Self::Item<'_>) -> bool,
{
while let Some(val) = self.next() {
if f(val) {
return true;
}
}
false
}
fn find<F>(&mut self, mut f: F) -> Option<Self::Item<'_>>
where
F: FnMut(&Self::Item<'_>) -> bool,
{
while let Some(val) = self.next() {
if f(&val) {
return Some(unsafe { change_lifetime::<Self>(val) });
}
}
None
}
fn count(self) -> usize
where
Self: Sized,
{
self.fold(0, |acc, _| acc + 1)
}
fn for_each<F>(mut self, mut f: F)
where
Self: Sized,
F: FnMut(Self::Item<'_>),
{
while let Some(next) = self.next() {
f(next)
}
}
fn fold<T, F>(mut self, acc: T, mut f: F) -> T
where
Self: Sized,
F: FnMut(T, Self::Item<'_>) -> T,
{
let mut acc = acc;
while let Some(x) = self.next() {
acc = f(acc, x);
}
acc
}
fn scan<T, B, F>(self, acc: T, f: F) -> Scan<Self, T, F>
where
Self: Sized,
F: FnMut(&mut T, Self::Item<'_>) -> Option<B>,
{
Scan::new(self, acc, f)
}
}
pub trait IntoIterator {
type IntoIter: Iterator;
fn into_iter(self) -> Self::IntoIter;
}
impl<T> IntoIterator for T
where
T: Iterator,
{
type IntoIter = T;
fn into_iter(self) -> Self::IntoIter {
self
}
}
pub trait IntoLending: Sized {
fn into_lending(self) -> FromCore<Self>;
}
impl<I> IntoLending for I
where
I: core::iter::Iterator,
{
fn into_lending(self) -> FromCore<Self> {
FromCore(self)
}
}
#[cfg(test)]
mod test;