Skip to content

Commit

Permalink
Remove long Iterator::chain sequence in RuleCodePrefix::iter (#10452
Browse files Browse the repository at this point in the history
)

## Summary

This PR removes the `Iterator::chain(...)` sequence in
`RuleCodePrefix::iter()` with `Vec::expand` to avoid an
overlong-recursive types.

The existing `RuleCodePrefix::iter` method chains all rule group
iterators together. This leads to very long recursive types
`Chain<Map<Chain<Map<Chain<Map.....>>>>` (proportional to the number of
rule groups).

This PR rewrites the macro to use `Vec::extend` instead, which removes
the long recursive type (at the cost of introducing a potential
allocation).

## Alternatives

An alternative would be to use a stack allocated array by unrolling the
`Linter::iter` methods (generated by `EnumIter`).
I don't think it's worth the extra complexity, considering that
`RuleCodePrefix::iter` isn't a hot function.

## Test Plan

`cargo test`
  • Loading branch information
MichaReiser authored Mar 18, 2024
1 parent 93566f9 commit 31db1b6
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions crates/ruff_macros/src/map_codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,10 @@ fn generate_iter_impl(
pub fn iter() -> impl Iterator<Item = RuleCodePrefix> {
use strum::IntoEnumIterator;

std::iter::empty()
#(.chain(#linter_idents::iter().map(|x| Self::#linter_idents(x))))*
let mut prefixes = Vec::new();

#(prefixes.extend(#linter_idents::iter().map(|x| Self::#linter_idents(x)));)*
prefixes.into_iter()
}
}
}
Expand Down

0 comments on commit 31db1b6

Please sign in to comment.