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

fix: take_while_inclusive takes iterator by value #709

Merged
merged 1 commit into from Aug 5, 2023
Merged

fix: take_while_inclusive takes iterator by value #709

merged 1 commit into from Aug 5, 2023

Conversation

abhikjain360
Copy link
Contributor

@abhikjain360 abhikjain360 commented Jul 1, 2023

this helps in case TakeWhileInclusive is returned from a function, as otherwise we would be returning a struct which takes a reference from a locally created iterator.

example code which breaks, but shouldn't:

use itertools::Itertools;

struct Entry {
    constraint: bool,
}

struct List {
    entries: Vec<Entry>,
}

impl List {
    fn special(&self) -> impl Iterator<Item = &Entry> {
        self
            .entries
            .iter()
            .take_while_inclusive(|entry| entry.constraint)
    }
}

Rust Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f602ba3c6e7c2ab2f5132a0b40b80ceb

this helps in case TakeWhileInclusive is returned from a function, as
otherwise we would be returning a struct which takes a reference from a
locally created iterator.
@Philippe-Cholet
Copy link
Member

Philippe-Cholet commented Jul 2, 2023

I thought you had a point so I looked at the methods in trait Iterator to find the way the "std" considers this.

There are 4 cases:

  • no change (size_hint): immutable borrow ;
  • transfer ownership to another iterator type (such as take_while...): take by value ;
  • consume the iterator completely (such as count/last/max...): take by value ;
  • consume only part of the iterator (such as find/all/try_collect...): mutably borrow.

take_while_inclusive is like take_while, it returns another iterator type. So I think it should take by value. People that does not want that can still do some_it.by_ref().take_while_inclusive(...) then do something else with what remains in some_it.

tldr I fully agree with you.

PS: It's a breaking change but take_while_inclusive is really new (appeared in "0.11.0" a few days ago) so it won't be a big issue for people if it's fixed soon.

@phimuemue
Copy link
Member

I think @Philippe-Cholet's assessment is correct.

Thus, I'd like to merge this. @jswrenn Do you have objections?

@abhikjain360
Copy link
Contributor Author

Hi @phimuemue @jswrenn ,
any updates on this or #710?
I think by now it is late enough that you probably need to release this fix in a separate release 😅 .
I can add fixes for other methods mentioned in #710 if that would help get this merged.

Thanks!

@phimuemue
Copy link
Member

Thanks a lot.

bors r+

@bors
Copy link
Contributor

bors bot commented Aug 5, 2023

Build succeeded!

The publicly hosted instance of bors-ng is deprecated and will go away soon.

If you want to self-host your own instance, instructions are here.
For more help, visit the forum.

If you want to switch to GitHub's built-in merge queue, visit their help page.

@bors bors bot merged commit 79bfebb into rust-itertools:master Aug 5, 2023
10 checks passed
@jswrenn
Copy link
Member

jswrenn commented Aug 31, 2023

Thank you for this! Apologizes for not releasing this fast enough to avoid a breaking release; I was on an extended bicycle tour. I'll get out a release soon.

@meithecatte
Copy link

@jswrenn I just hit this issue and found this PR. Just wanna make sure that you haven't forgot about the release. Thanks for your work, itertools is a very useful crate ;3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants