#![deny(
unsafe_code,
unused,
warnings,
clippy::all,
clippy::cargo,
clippy::complexity,
clippy::correctness,
clippy::nursery,
clippy::pedantic,
clippy::perf,
clippy::restriction,
clippy::style,
clippy::suspicious
)]
#![allow(
clippy::arithmetic_side_effects,
clippy::blanket_clippy_restriction_lints,
clippy::implicit_return,
clippy::min_ident_chars,
clippy::missing_trait_methods,
clippy::question_mark_used,
clippy::single_char_lifetime_names
)]
pub trait LendingIterator {
type Item<'a>
where
Self: 'a;
fn lend_next(&mut self) -> Option<Self::Item<'_>>;
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(0, None)
}
#[inline]
fn count(self) -> usize
where
Self: Sized,
{
self.lend_fold(0, |count, _| count + 1)
}
#[inline]
fn advance_by(&mut self, n: usize) -> Result<(), usize> {
for i in 0..n {
self.lend_next().ok_or(i)?;
}
Ok(())
}
#[inline]
fn by_ref(&mut self) -> &mut Self
where
Self: Sized,
{
self
}
#[inline]
fn lend_try_fold<B, E, F>(&mut self, init: B, mut f: F) -> Result<B, E>
where
Self: Sized,
F: FnMut(B, Self::Item<'_>) -> Result<B, E>,
{
let mut accum = init;
while let Some(x) = self.lend_next() {
accum = f(accum, x)?;
}
Ok(accum)
}
#[inline]
fn lend_fold<B, F>(mut self, init: B, mut f: F) -> B
where
Self: Sized,
F: FnMut(B, Self::Item<'_>) -> B,
{
let mut accum = init;
while let Some(x) = self.lend_next() {
accum = f(accum, x);
}
accum
}
}
impl<T> LendingIterator for T
where
T: Iterator,
{
type Item<'a> = T::Item where Self: 'a;
#[inline]
fn lend_next(&mut self) -> Option<Self::Item<'_>> {
self.next()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.size_hint()
}
#[inline]
fn count(self) -> usize
where
Self: Sized,
{
self.count()
}
#[inline]
fn advance_by(&mut self, n: usize) -> Result<(), usize> {
for i in 0..n {
self.lend_next().ok_or(i)?;
}
Ok(())
}
#[inline]
fn by_ref(&mut self) -> &mut Self
where
Self: Sized,
{
self
}
}