Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test the laziness of our iterators #792

Merged

Conversation

Philippe-Cholet
Copy link
Member

@Philippe-Cholet Philippe-Cholet commented Nov 3, 2023

Related to #791
Add a test to:

  • track iterators that are not lazy yet ;
  • help track missing must_use attributes with very little discipline.

It's a long list of iterators and it has some edge cases. The error message help us understand how unlazy there are.

When let _ = ...; is not necessary, it indicates that a must_use attribute is missing.

CI will fail for some time.

@Philippe-Cholet Philippe-Cholet force-pushed the iterator-laziness branch 2 times, most recently from 0ee2319 to acf92c6 Compare November 3, 2023 10:18
Copy link
Member

@jswrenn jswrenn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tremendous thanks for doing this work! There's no rush on this (as we have some time to go before we can merge this PR), but before merging, I'd like us to have distinct tests, rather than one big one. We can do so without too much trouble with a little macro magic:

mod laziness {
    struct Panicking;

    impl Iterator for Panicking {
        type Item = ();
        fn next(&mut self) -> Option<()> {
            panic!("iterator adaptor is not lazy")
        }
    }
    
    macro_rules! tests {
        ($(test $name:ident { $body:expr })*) => {
            $(
                #[deny(unused_must_use)]
                #[test]
                fn $name() {
                    let _ = $body;
                }
            )*
        }
    }
    
    use itertools::*;
    
    tests! {
        test interleave {
            Panicking.interleave(Panicking)
        }
        
        test interleave_shortest {
            Panicking.interleave_shortest(Panicking)
        }
        
        test intersperse {
            Panicking.intersperse(())
        }
        
        /* and so on */
    }
}

The advantage of this approach is that the tests are run in parallel, are reported individually, and can be run individually.

@Philippe-Cholet
Copy link
Member Author

I surely had doubts about my big test. Thanks for your review.
I rewrote it.

Panicking: The ExactSizeIterator implementation is needed for circular_tuple_windows (the actual length does not matter). No DoubleEndedIterator implementation needed.

Copy link

codecov bot commented Jan 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (b1be1e4) 94.40% compared to head (fee3f6d) 94.29%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #792      +/-   ##
==========================================
- Coverage   94.40%   94.29%   -0.11%     
==========================================
  Files          48       48              
  Lines        6665     6665              
==========================================
- Hits         6292     6285       -7     
- Misses        373      380       +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Philippe-Cholet
Copy link
Member Author

I'm not sure what to do about the remaining unlazy iterators yet.
I therefore added one commit to ignore the 3 failing laziness tests (then rebased).
I think we should merge this and un-ignore failing tests later.

@Philippe-Cholet Philippe-Cholet added this to the next milestone Jan 30, 2024
Ignore the 3 failing tests (for now).

`ChunkBy` and `IntoChunks` are not iterators themselves but implement `IntoIterator` instead.
Then `Groups` and `Chunks` are iterators.
All four are lazy and must be used.
@Philippe-Cholet Philippe-Cholet added this pull request to the merge queue Feb 6, 2024
Merged via the queue into rust-itertools:master with commit 6814180 Feb 6, 2024
13 checks passed
@Philippe-Cholet Philippe-Cholet deleted the iterator-laziness branch February 6, 2024 22:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants