Skip to content
New issue

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

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

Already on GitHub? # to your account

Ownership issues for Itertools methods #710

Open
Philippe-Cholet opened this issue Jul 2, 2023 · 0 comments
Open

Ownership issues for Itertools methods #710

Philippe-Cholet opened this issue Jul 2, 2023 · 0 comments

Comments

@Philippe-Cholet
Copy link
Member

Philippe-Cholet commented Jul 2, 2023

Following the logic of #709 (comment):

I looked at the methods in trait Iterator to find the way the "std" considers "iterator ownership".
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.

I was worried about ownership for other methods than take_while_inclusive (subject of the comment), here's what I found:

It consumes the iterator completely, it should take by value, right?
Once called, the iterator still exists but is always empty, then it's dropped later.

fn join(&mut self, sep: &str) -> String;

Transfer ownership to another iterator: it should take by value (then people can call .by_ref() if they want).

fn take_while_inclusive... // subject of #709
fn peeking_take_while<F>(&mut self, accept: F) -> PeekingTakeWhile<Self, F>;
fn take_while_ref<F>(&mut self, accept: F) -> TakeWhileRef<Self, F>;

Methods that consume only part (in some case) of the iterator (should mutably borrow, people might want to consider remaining elements). But it's not a real issue as people can call .by_ref().

fn collect_tuple<T>(mut self) -> Option<T>;
fn try_collect<T, U, E>(self) -> Result<U, E>; // will be deprecated anyway once it's stabilized in Iterator
fn process_results<F, T, E, R>(self, processor: F) -> Result<R, E>;

The rest of the methods follow the "std Iterator logic" described in the quoted comment above.

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

No branches or pull requests

2 participants