From e498fb22158a0f40fb27030e30559240c68b324e Mon Sep 17 00:00:00 2001 From: WanderLanz Date: Sat, 24 Feb 2024 20:05:34 -0600 Subject: [PATCH] derive Debug, add must_use and inline attributes --- src/adapters/chain.rs | 4 +++- src/adapters/cloned.rs | 3 ++- src/adapters/enumerate.rs | 5 ++++- src/adapters/filter.rs | 9 +++++++++ src/adapters/filter_map.rs | 11 +++++++++++ src/adapters/map.rs | 9 +++++++++ src/adapters/skip.rs | 5 ++++- src/adapters/step_by.rs | 5 ++++- src/adapters/take.rs | 4 +++- src/adapters/take_while.rs | 15 ++++++++++++++- src/adapters/zip.rs | 4 +++- src/traits/lending_iterator.rs | 20 ++++++++++++++++++-- 12 files changed, 84 insertions(+), 10 deletions(-) diff --git a/src/adapters/chain.rs b/src/adapters/chain.rs index 3f9c1bd..121b94d 100644 --- a/src/adapters/chain.rs +++ b/src/adapters/chain.rs @@ -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: A, b: B, @@ -34,6 +35,7 @@ where Self: 'a ; + #[inline] fn next(&mut self) -> Option> { if self.a_done { self.b.next() diff --git a/src/adapters/cloned.rs b/src/adapters/cloned.rs index 8ba0e08..bfe503c 100644 --- a/src/adapters/cloned.rs +++ b/src/adapters/cloned.rs @@ -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 { iter: I, } diff --git a/src/adapters/enumerate.rs b/src/adapters/enumerate.rs index 964ee9e..ec4f0a4 100644 --- a/src/adapters/enumerate.rs +++ b/src/adapters/enumerate.rs @@ -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 { count: usize, iter: I, @@ -15,6 +16,8 @@ impl Enumerate { impl LendingIterator for Enumerate { type Item<'a> = (usize, I::Item<'a>) where Self: 'a; + + #[inline] fn next(&mut self) -> Option> { let item = self.iter.next()?; let count = self.count; diff --git a/src/adapters/filter.rs b/src/adapters/filter.rs index f17d7c7..581f667 100644 --- a/src/adapters/filter.rs +++ b/src/adapters/filter.rs @@ -1,4 +1,5 @@ use crate::LendingIterator; +use core::fmt; /// A lending iterator that filters the elements of `iter` with `predicate`. /// @@ -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 { iter: I, predicate: P, @@ -19,6 +21,12 @@ impl Filter { } } +impl fmt::Debug for Filter { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Filter").field("iter", &self.iter).finish() + } +} + impl LendingIterator for Filter where I: LendingIterator, @@ -28,6 +36,7 @@ where where Self: 'a; + #[inline] fn next(&mut self) -> Option> { loop { // SAFETY: see https://docs.rs/polonius-the-crab/0.3.1/polonius_the_crab/#the-arcanemagic diff --git a/src/adapters/filter_map.rs b/src/adapters/filter_map.rs index 64cf610..b54f6eb 100644 --- a/src/adapters/filter_map.rs +++ b/src/adapters/filter_map.rs @@ -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`. /// @@ -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 { iter: I, f: F, @@ -19,6 +21,14 @@ impl FilterMap { } } +impl fmt::Debug for FilterMap { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("FilterMap") + .field("iter", &self.iter) + .finish() + } +} + impl LendingIterator for FilterMap where I: LendingIterator, @@ -29,6 +39,7 @@ where where Self: 'a; + #[inline] fn next(&mut self) -> Option> { loop { // SAFETY: see https://docs.rs/polonius-the-crab/0.3.1/polonius_the_crab/#the-arcanemagic diff --git a/src/adapters/map.rs b/src/adapters/map.rs index 6c069f0..e851415 100644 --- a/src/adapters/map.rs +++ b/src/adapters/map.rs @@ -1,4 +1,5 @@ use crate::{LendingIterator, SingleArgFnMut, SingleArgFnOnce}; +use core::fmt; /// A lending iterator that maps the elements of `iter` with `f`. /// @@ -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 { iter: I, f: F, @@ -19,6 +21,12 @@ impl Map { } } +impl fmt::Debug for Map { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Map").field("iter", &self.iter).finish() + } +} + impl LendingIterator for Map where I: LendingIterator, @@ -28,6 +36,7 @@ where where Self: 'a; + #[inline] fn next(&mut self) -> Option> { self.iter.next().map(&mut self.f) } diff --git a/src/adapters/skip.rs b/src/adapters/skip.rs index 15861cc..0cac449 100644 --- a/src/adapters/skip.rs +++ b/src/adapters/skip.rs @@ -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 { iter: I, n: usize, @@ -19,6 +20,7 @@ where { type Item<'a> = I::Item<'a> where I: 'a; + #[inline] fn next(&mut self) -> Option> { if self.n > 0 { self.iter.nth(core::mem::take(&mut self.n)) @@ -27,6 +29,7 @@ where } } + #[inline] fn size_hint(&self) -> (usize, Option) { let (lower, upper) = self.iter.size_hint(); diff --git a/src/adapters/step_by.rs b/src/adapters/step_by.rs index 7098388..9fe85b0 100644 --- a/src/adapters/step_by.rs +++ b/src/adapters/step_by.rs @@ -7,7 +7,8 @@ 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 { iter: I, step: usize, @@ -15,6 +16,7 @@ pub struct StepBy { } impl StepBy { + #[inline] pub(crate) fn new(iter: I, step: usize) -> StepBy { assert!(step != 0); StepBy { @@ -34,6 +36,7 @@ where Self: 'a ; + #[inline] fn next(&mut self) -> Option> { if self.first_take { self.first_take = false; diff --git a/src/adapters/take.rs b/src/adapters/take.rs index abbc5fa..5ae58a5 100644 --- a/src/adapters/take.rs +++ b/src/adapters/take.rs @@ -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 { iter: I, n: usize, @@ -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> { if self.n != 0 { diff --git a/src/adapters/take_while.rs b/src/adapters/take_while.rs index d160504..58abaac 100644 --- a/src/adapters/take_while.rs +++ b/src/adapters/take_while.rs @@ -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 { iter: I, predicate: P, @@ -20,13 +22,23 @@ impl TakeWhile { } } +impl fmt::Debug for TakeWhile { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("TakeWhile") + .field("iter", &self.iter) + .field("done", &self.done) + .finish() + } +} + impl LendingIterator for TakeWhile 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> { if self.done { None @@ -41,6 +53,7 @@ where } } + #[inline] fn size_hint(&self) -> (usize, Option) { if self.done { (0, Some(0)) diff --git a/src/adapters/zip.rs b/src/adapters/zip.rs index 2f212ec..8681f26 100644 --- a/src/adapters/zip.rs +++ b/src/adapters/zip.rs @@ -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: A, b: B, @@ -28,6 +29,7 @@ where A: 'a, B: 'a ; + #[inline] fn next(&mut self) -> Option> { let a = self.a.next()?; let b = self.b.next()?; diff --git a/src/traits/lending_iterator.rs b/src/traits/lending_iterator.rs index b69ef23..2e5ad6d 100644 --- a/src/traits/lending_iterator.rs +++ b/src/traits/lending_iterator.rs @@ -2,7 +2,7 @@ use std::{num::NonZeroUsize, ops::Deref}; use crate::{ Chain, Cloned, Enumerate, Filter, FilterMap, Map, OptionTrait, SingleArgFnMut, SingleArgFnOnce, - Skip, StepBy, Take, Zip, TakeWhile, + Skip, StepBy, Take, TakeWhile, Zip, }; /// Like [`Iterator`], but items may borrow from `&mut self`. @@ -24,6 +24,7 @@ pub trait LendingIterator { /// Returns the bounds on the remaining length of the iterator. /// /// See [`Iterator::size_hint`]. + #[inline] fn size_hint(&self) -> (usize, Option) { (0, None) } @@ -31,6 +32,7 @@ pub trait LendingIterator { /// Returns the number of items in the lending iterator. /// /// See [`Iterator::count`]. + #[inline] fn count(self) -> usize where Self: Sized, @@ -41,6 +43,7 @@ pub trait LendingIterator { /// Advances the lending iterator by `n` elements. /// /// See [`Iterator::advance_by`]. + #[inline] #[allow(clippy::missing_errors_doc)] fn advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { for i in 0..n { @@ -55,6 +58,7 @@ pub trait LendingIterator { /// Returns the `n`th element of the lending iterator. /// /// See [`Iterator::nth`]. + #[inline] fn nth(&mut self, n: usize) -> Option> { self.advance_by(n).ok()?; self.next() @@ -64,6 +68,7 @@ pub trait LendingIterator { /// the given amount at each iteration. /// /// See [`Iterator::step_by`]. + #[inline] fn step_by(self, step: usize) -> StepBy where Self: Sized, @@ -75,6 +80,7 @@ pub trait LendingIterator { /// if the underlying iterator ends sooner. /// /// See [`Iterator::take`]. + #[inline] fn take(self, n: usize) -> Take where Self: Sized, @@ -88,10 +94,11 @@ pub trait LendingIterator { /// Once it returns false once, `None` is returned for all subsequent calls to [`next`]. /// /// [`next`]: Self::next + #[inline] fn take_while

(self, predicate: P) -> TakeWhile where Self: Sized, - P: for <'a> FnMut(&Self::Item<'a>) -> bool + P: for<'a> FnMut(&Self::Item<'a>) -> bool, { TakeWhile::new(self, predicate) } @@ -99,6 +106,7 @@ pub trait LendingIterator { /// Takes two lending iterators and creates a new lending iterator over both in sequence. /// /// See [`Iterator::chain`]. + #[inline] fn chain(self, other: I) -> Chain where Self: Sized, @@ -108,6 +116,7 @@ pub trait LendingIterator { } /// 'Zips up' two lending iterators into a single lending iterator of pairs. + #[inline] fn zip(self, other: I) -> Zip where Self: Sized, @@ -129,6 +138,7 @@ pub trait LendingIterator { /// the resulting `LendingIterator` will implement [`IntoIterator`]. /// /// See [`Iterator::map`]. + #[inline] fn map(self, f: F) -> Map where Self: Sized, @@ -140,6 +150,7 @@ pub trait LendingIterator { /// Calls a closure on each element of the lending iterator. /// /// See [`Iterator::for_each`]. + #[inline] fn for_each(mut self, mut f: F) where Self: Sized, @@ -154,6 +165,7 @@ pub trait LendingIterator { /// should be yielded. /// /// See [`Iterator::filter`]. + #[inline] fn filter

(self, predicate: P) -> Filter where Self: Sized, @@ -165,6 +177,7 @@ pub trait LendingIterator { /// Creates a lending iterator that both filters and maps. /// /// See [`Iterator::filter_map`]. + #[inline] fn filter_map(self, f: F) -> FilterMap where Self: Sized, @@ -178,6 +191,7 @@ pub trait LendingIterator { /// returning the final result. /// /// See [`Iterator::fold`]. + #[inline] fn fold(mut self, init: B, mut f: F) -> B where Self: Sized, @@ -207,6 +221,7 @@ pub trait LendingIterator { } /// Creates a lending iterator which gives the current iteration count as well as the next value. + #[inline] fn enumerate(self) -> Enumerate where Self: Sized, @@ -215,6 +230,7 @@ pub trait LendingIterator { } /// Creates a lending iterator that skips over the first `n` elements of self. + #[inline] fn skip(self, n: usize) -> Skip where Self: Sized,