Skip to content

Commit

Permalink
Merge pull request #25 from WanderLanz/attrs
Browse files Browse the repository at this point in the history
derive Debug, add must_use and inline attributes
  • Loading branch information
Crazytieguy authored Sep 14, 2024
2 parents 8cebe20 + e498fb2 commit ce435be
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/adapters/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use crate::LendingIterator;
///
/// [`LendingIterator`]: crate::LendingIterator
/// [`chain`]: crate::LendingIterator::chain
#[derive(Clone)]
#[derive(Clone, Debug)]
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct Chain<A, B> {
a: A,
b: B,
Expand All @@ -34,6 +35,7 @@ where
Self: 'a
;

#[inline]
fn next(&mut self) -> Option<A::Item<'_>> {
if self.a_done {
self.b.next()
Expand Down
3 changes: 2 additions & 1 deletion src/adapters/cloned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use crate::LendingIterator;
///
/// [`LendingIterator`]: crate::LendingIterator
/// [`cloned`]: crate::LendingIterator::cloned
#[derive(Clone)]
#[derive(Clone, Debug)]
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct Cloned<I> {
iter: I,
}
Expand Down
5 changes: 4 additions & 1 deletion src/adapters/enumerate.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::LendingIterator;

/// A lending iterator that yields the current count and the element during iteration.
#[derive(Clone)]
#[derive(Clone, Debug)]
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct Enumerate<I> {
count: usize,
iter: I,
Expand All @@ -15,6 +16,8 @@ impl<I> Enumerate<I> {

impl<I: LendingIterator> LendingIterator for Enumerate<I> {
type Item<'a> = (usize, I::Item<'a>) where Self: 'a;

#[inline]
fn next(&mut self) -> Option<Self::Item<'_>> {
let item = self.iter.next()?;
let count = self.count;
Expand Down
9 changes: 9 additions & 0 deletions src/adapters/filter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::LendingIterator;
use core::fmt;

/// A lending iterator that filters the elements of `iter` with `predicate`.
///
Expand All @@ -8,6 +9,7 @@ use crate::LendingIterator;
/// [`LendingIterator`]: crate::LendingIterator
/// [`filter`]: crate::LendingIterator::filter
#[derive(Clone)]
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct Filter<I, P> {
iter: I,
predicate: P,
Expand All @@ -19,6 +21,12 @@ impl<I, P> Filter<I, P> {
}
}

impl<I: fmt::Debug, P> fmt::Debug for Filter<I, P> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Filter").field("iter", &self.iter).finish()
}
}

impl<I, P> LendingIterator for Filter<I, P>
where
I: LendingIterator,
Expand All @@ -28,6 +36,7 @@ where
where
Self: 'a;

#[inline]
fn next(&mut self) -> Option<Self::Item<'_>> {
loop {
// SAFETY: see https://docs.rs/polonius-the-crab/0.3.1/polonius_the_crab/#the-arcanemagic
Expand Down
11 changes: 11 additions & 0 deletions src/adapters/filter_map.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{LendingIterator, OptionTrait, SingleArgFnMut, SingleArgFnOnce};
use core::fmt;

/// A lending iterator that uses `f` to both filter and map elements from `iter`.
///
Expand All @@ -8,6 +9,7 @@ use crate::{LendingIterator, OptionTrait, SingleArgFnMut, SingleArgFnOnce};
/// [`LendingIterator`]: crate::LendingIterator
/// [`filter_map`]: crate::LendingIterator::filter_map
#[derive(Clone)]
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct FilterMap<I, F> {
iter: I,
f: F,
Expand All @@ -19,6 +21,14 @@ impl<I, F> FilterMap<I, F> {
}
}

impl<I: fmt::Debug, F> fmt::Debug for FilterMap<I, F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("FilterMap")
.field("iter", &self.iter)
.finish()
}
}

impl<I, F> LendingIterator for FilterMap<I, F>
where
I: LendingIterator,
Expand All @@ -29,6 +39,7 @@ where
where
Self: 'a;

#[inline]
fn next(&mut self) -> Option<Self::Item<'_>> {
loop {
// SAFETY: see https://docs.rs/polonius-the-crab/0.3.1/polonius_the_crab/#the-arcanemagic
Expand Down
9 changes: 9 additions & 0 deletions src/adapters/map.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{LendingIterator, SingleArgFnMut, SingleArgFnOnce};
use core::fmt;

/// A lending iterator that maps the elements of `iter` with `f`.
///
Expand All @@ -8,6 +9,7 @@ use crate::{LendingIterator, SingleArgFnMut, SingleArgFnOnce};
/// [`LendingIterator`]: crate::LendingIterator
/// [`map`]: crate::LendingIterator::map
#[derive(Clone)]
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct Map<I, F> {
iter: I,
f: F,
Expand All @@ -19,6 +21,12 @@ impl<I, F> Map<I, F> {
}
}

impl<I: fmt::Debug, F> fmt::Debug for Map<I, F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Map").field("iter", &self.iter).finish()
}
}

impl<I, F> LendingIterator for Map<I, F>
where
I: LendingIterator,
Expand All @@ -28,6 +36,7 @@ where
where
Self: 'a;

#[inline]
fn next(&mut self) -> Option<Self::Item<'_>> {
self.iter.next().map(&mut self.f)
}
Expand Down
5 changes: 4 additions & 1 deletion src/adapters/skip.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::LendingIterator;

/// A lending iterator that skips over the first `n` items of `iter`.
#[derive(Clone)]
#[derive(Clone, Debug)]
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct Skip<I> {
iter: I,
n: usize,
Expand All @@ -19,6 +20,7 @@ where
{
type Item<'a> = I::Item<'a> where I: 'a;

#[inline]
fn next(&mut self) -> Option<I::Item<'_>> {
if self.n > 0 {
self.iter.nth(core::mem::take(&mut self.n))
Expand All @@ -27,6 +29,7 @@ where
}
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let (lower, upper) = self.iter.size_hint();

Expand Down
5 changes: 4 additions & 1 deletion src/adapters/step_by.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ use crate::LendingIterator;
///
/// [`LendingIterator`]: crate::LendingIterator
/// [`step_by`]: crate::LendingIterator::step_by
#[derive(Clone)]
#[derive(Clone, Debug)]
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct StepBy<I> {
iter: I,
step: usize,
first_take: bool,
}

impl<I> StepBy<I> {
#[inline]
pub(crate) fn new(iter: I, step: usize) -> StepBy<I> {
assert!(step != 0);
StepBy {
Expand All @@ -34,6 +36,7 @@ where
Self: 'a
;

#[inline]
fn next(&mut self) -> Option<Self::Item<'_>> {
if self.first_take {
self.first_take = false;
Expand Down
4 changes: 3 additions & 1 deletion src/adapters/take.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::LendingIterator;

/// A Lending iterator that only lends the first `n` iterations of `iter`.
#[derive(Clone)]
#[derive(Clone, Debug)]
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct Take<I> {
iter: I,
n: usize,
Expand All @@ -22,6 +23,7 @@ where
{
type Item<'a> = I::Item<'a> where I: 'a;

#[inline]
#[allow(clippy::if_not_else)]
fn next(&mut self) -> Option<Self::Item<'_>> {
if self.n != 0 {
Expand Down
15 changes: 14 additions & 1 deletion src/adapters/take_while.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::LendingIterator;
use core::fmt;

/// A lending iterator that yields items based on a predicate.
///
/// This iterator is fused.
#[derive(Clone)]
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct TakeWhile<I, P> {
iter: I,
predicate: P,
Expand All @@ -20,13 +22,23 @@ impl<I, P> TakeWhile<I, P> {
}
}

impl<I: fmt::Debug, P> fmt::Debug for TakeWhile<I, P> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("TakeWhile")
.field("iter", &self.iter)
.field("done", &self.done)
.finish()
}
}

impl<I, P> LendingIterator for TakeWhile<I, P>
where
I: LendingIterator,
P: for<'a> FnMut(&I::Item<'a>) -> bool
P: for<'a> FnMut(&I::Item<'a>) -> bool,
{
type Item<'a> = I::Item<'a> where I: 'a, P: 'a;

#[inline]
fn next(&mut self) -> Option<Self::Item<'_>> {
if self.done {
None
Expand All @@ -41,6 +53,7 @@ where
}
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
if self.done {
(0, Some(0))
Expand Down
4 changes: 3 additions & 1 deletion src/adapters/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use crate::LendingIterator;
///
/// [`LendingIterator`]: crate::LendingIterator
/// [`zip`]: crate::LendingIterator::zip
#[derive(Clone)]
#[derive(Clone, Debug)]
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct Zip<A, B> {
a: A,
b: B,
Expand All @@ -28,6 +29,7 @@ where
A: 'a, B: 'a
;

#[inline]
fn next(&mut self) -> Option<Self::Item<'_>> {
let a = self.a.next()?;
let b = self.b.next()?;
Expand Down
Loading

0 comments on commit ce435be

Please sign in to comment.