Skip to content

missing_iterator_fold #12209

Open
Open
@Philippe-Cholet

Description

@Philippe-Cholet

What it does

Lints implementations of core::iter::Iterator that do not specialize the fold method.

Advantage

  • Methods that consume the entire iterator (for_each, count, ...) rely on fold by default and would benefit from fold being specialized.
  • Adaptor iterators might use fold (or related methods) for their own usage and would benefit as well.

Drawbacks

Specialize it may not be faster than the default behavior.

Example

struct Iter(u8);
impl Iterator for Iter {
    type Item = u8;
    fn next(&mut self) -> Option<Self::Item> {
        todo!()
    }
}

Could be written as:

struct Iter(u8);
impl Iterator for Iter {
    type Item = u8;
    fn next(&mut self) -> Option<Self::Item> {
        todo!()
    }
    fn fold<B, F>(self, init: B, f: F) -> B
    where
        F: FnMut(B, Self::Item) -> B
    {
        todo!()
    }
}

Metadata

Metadata

Labels

A-lintArea: New lints

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions