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

Add `is_empty` function to `ExactSizeIterator` #35428

Open
frewsxcv opened this Issue Aug 6, 2016 · 54 comments

Comments

Projects
None yet
@frewsxcv
Copy link
Member

frewsxcv commented Aug 6, 2016

Tracking issue for functionality introduced in #34357

@frewsxcv

This comment has been minimized.

Copy link
Member Author

frewsxcv commented Aug 6, 2016

Added reference to this issue in #35429

@apasel422 apasel422 added the B-unstable label Aug 6, 2016

jonathandturner added a commit to jonathandturner/rust that referenced this issue Aug 7, 2016

Rollup merge of rust-lang#35429 - frewsxcv:tracking-is-empty, r=apase…
…l422

Indicate tracking issue for `exact_size_is_empty` unstability.

rust-lang#35428

@alexcrichton alexcrichton added the T-libs label Aug 7, 2016

jonathandturner added a commit to jonathandturner/rust that referenced this issue Aug 7, 2016

Rollup merge of rust-lang#35429 - frewsxcv:tracking-is-empty, r=apase…
…l422

Indicate tracking issue for `exact_size_is_empty` unstability.

rust-lang#35428

jonathandturner added a commit to jonathandturner/rust that referenced this issue Aug 7, 2016

Rollup merge of rust-lang#35429 - frewsxcv:tracking-is-empty, r=apase…
…l422

Indicate tracking issue for `exact_size_is_empty` unstability.

rust-lang#35428
@SimonSapin

This comment has been minimized.

Copy link
Contributor

SimonSapin commented Oct 17, 2016

I wanted to use this today until I realized that it’s unstable. (I could use .len() == 0 in the mean time.) Seems straightforward enough. FCP to stabilize?

@alexcrichton

This comment has been minimized.

Copy link
Member

alexcrichton commented Nov 1, 2016

@rfcbot fcp merge

Seems good to have consistency!

@rfcbot

This comment has been minimized.

Copy link

rfcbot commented Nov 1, 2016

Team member @alexcrichton has proposed to merge this. The next step is review by the rest of the tagged teams:

No concerns currently listed.

Once these reviewers reach consensus, this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@bluss

This comment has been minimized.

Copy link
Contributor

bluss commented Nov 4, 2016

@SimonSapin I'm curious, when do you use ExactSizeIterator and have the opportunity to use this?

@SimonSapin

This comment has been minimized.

Copy link
Contributor

SimonSapin commented Nov 5, 2016

When using a slice or vec iterator as input for a parser, .is_empty() is a way to tell "have I reach the end of the input yet?" https://github.com/servo/html5ever/blob/b5c4552fab/macros/match_token.rs#L175

@bluss

This comment has been minimized.

Copy link
Contributor

bluss commented Nov 5, 2016

Aha, that explains it for me: using a specific iterator type, not using ESI generically. This method is good, reinforces existing conventions.

@rfcbot

This comment has been minimized.

Copy link

rfcbot commented Nov 12, 2016

🔔 This is now entering its final comment period, as per the review above. 🔔

psst @alexcrichton, I wasn't able to add the final-comment-period label, please do so.

@rfcbot

This comment has been minimized.

Copy link

rfcbot commented Nov 22, 2016

The final comment period is now complete.

@bluss

This comment has been minimized.

Copy link
Contributor

bluss commented Nov 22, 2016

used in PR #37943

@bluss

This comment has been minimized.

Copy link
Contributor

bluss commented Nov 23, 2016

Here's a thought: Some iterators can implement a good is_empty() even if they can't do ESI or .len(). One example is .chars(). Does it matter?

@aturon

This comment has been minimized.

Copy link
Member

aturon commented Dec 15, 2016

@bluss Sorry I missed your comment when prepping the stabilization PR.

My feeling is that these iterators can/should provide an inherent is_empty method if we think that's useful. The addition of the method to ExactSizeIterator is more about convenience than generic programming, IMO.

FWIW, it's also a longstanding desire on the lang side to have an API evolution path for splitting a trait into supertraits without breaking existing code. It's not guaranteed to happen, but that would allow us to split out is_empty into its own trait if we wanted to program generically with it in std in the future. (Of course, we can always add a separate trait with a blanket impl, worst case.)

@aturon

This comment has been minimized.

Copy link
Member

aturon commented Dec 15, 2016

I'm removing is_empty from the current stabilization PR, to give more time for discussion here.

@bluss

This comment has been minimized.

Copy link
Contributor

bluss commented Dec 15, 2016

  • I think emptiness should be a trait, so that it can pass through adapters
  • Iterators as lazy sequences have a different relationship to emptiness than collections, and the common chars() can already tell emptiness but not length; it's a common case in for example parsers (maybe not the most common, since byte-level parsing seems to be the most popular).
@aturon

This comment has been minimized.

Copy link
Member

aturon commented Dec 15, 2016

@bluss

Both points make sense. However, without further language improvements, it will be tricky to meet those goals and the original ergonomic goal at the same time.

Ideally, ExactSizeIterator would be a subtrait of IsEmpty and provide a default implementation of the is_empty method. To make this work, we'll need both specialization and the ability to implicitly impl supertraits when impl'ing a subtrait (an idea we've been kicking around on the lang team precisely to allow for this kind of API evolution).

Alternatively, we could add the IsEmpty trait to the prelude, along with a blanket impl from ExactSizeIterator. That comes with its own backwards-compatibility risks, though.

@bluss

This comment has been minimized.

Copy link
Contributor

bluss commented Dec 15, 2016

don't we have a backdoor for adding methods like this? trait Iterator has the method .rposition() where Self: DoubleEndedIterator

@aturon

This comment has been minimized.

Copy link
Member

aturon commented Dec 15, 2016

@bluss I may be missing something, but I don't see how that helps here. To clarify, the competing goals I see are:

  • A separate IsEmpty trait that can be properly forwarded along adapters, and programmed over generically.
  • Ergonomic access to is_empty for any ExactSizeIterator.

I don't offhand see how the trick you mention helps here; maybe you can spell it out?

It's worth noting that we could provide an is_empty method both in ExactSizeIterator and in a separate IsEmpty trait, with a blanket impl as well. But of course if you have both traits in scope at the same time, you'll have to explicitly disambiguate.

@bluss

This comment has been minimized.

Copy link
Contributor

bluss commented Dec 15, 2016

It's easier said than done, apparently. trait Iterator can get a new method, something like:

fn is_empty(&self) -> bool
    where Self: IsEmptyIterator;

which fixes the issue with having the method in the prelude.

But to arrange the right blanket implementations for IsEmptyIterator does not seem to be possible, even with the specialization features that are in nightly now.

@SimonSapin

This comment has been minimized.

Copy link
Contributor

SimonSapin commented Mar 9, 2017

Did the IsEmpty trait preempt the stabilization FCP?

@aturon

This comment has been minimized.

Copy link
Member

aturon commented Mar 13, 2017

@SimonSapin Yes, this ended up getting pulled from the stabilization PR and has been sitting idle since then. Needs someone to drive it to a conclusion.

@clarfon

This comment has been minimized.

Copy link
Contributor

clarfon commented May 11, 2017

IMHO is_empty cases are much better covered by peek than any generic trait. I'd rather just have this trait have an is_empty method.

If people want to have more generality they can use the len-trait crate I mentioned earlier. Based upon this discussion I did split out Empty and Len in that crate because it's for more niche cases.

@nvzqz

This comment has been minimized.

Copy link
Contributor

nvzqz commented May 11, 2017

@SimonSapin While it currently isn't used internally in a generic context, I would like it so that an outside user can use the Bitboard as a generic iterator with an is_empty() method.

@clarfon

This comment has been minimized.

Copy link
Contributor

clarfon commented May 12, 2017

As I said, you can easily implement is_empty for any iterator with peekable().peek().is_none()

@ranma42

This comment has been minimized.

Copy link
Contributor

ranma42 commented May 12, 2017

@clarcharr peek can cause side-effects (as suggested by mut), while len and is_empty are pure.

@clarfon

This comment has been minimized.

Copy link
Contributor

clarfon commented May 12, 2017

@ranma42 huh, I never realised. that seems like a bug to me

@nvzqz

This comment has been minimized.

Copy link
Contributor

nvzqz commented May 12, 2017

Does peek depend on next? If so, it makes sense to me for it to require mut.

@SimonSapin

This comment has been minimized.

Copy link
Contributor

SimonSapin commented May 12, 2017

peek is a method of Peekable, an iterator wrapper that holds an Option<Self::Item> buffer. It does call next to fill that buffer if not filled already.

@clarfon

This comment has been minimized.

Copy link
Contributor

clarfon commented May 13, 2017

I mean, you could easily make peek immutable:

fn peekable(self) -> Peekable<Self> {
    Peekable {
        buffer: self.next(),
        iter: self,
    }
}

and

fn peek(&self) -> Option<&Self::Item> {
    self.buffer.as_ref()
}
fn next(&mut self) -> Option<Self::Item> {
    mem::replace(self.buffer, self.next())
}

Although I guess that this is a bit out of the scope of the general discussion.

My main point is that it's almost trivial to determine if an iterator is empty, and doesn't need a generic trait. We can deal with ExactSizeIterator just having an is_empty convenience method.

@nvzqz

This comment has been minimized.

Copy link
Contributor

nvzqz commented May 13, 2017

While the Peekable solution can serve as a trivial default, I still think that any default should be able to be overridden with a possibly faster form like with my previous example where the internal u64 is 0.

@scottmcm

This comment has been minimized.

Copy link
Member

scottmcm commented May 24, 2017

One thing that's unfortunate about is_empty being on ExactSizeIterator is that it would also make sense on TrustedLen. And their slightly-different requirements mean that either choice leaves out things that could have it easily -- Chain xor Skip can have it, if it's on only one of those two traits. And if it's on both, it'll be ambiguous on a whole bunch of common iterators...

@scottmcm

This comment has been minimized.

Copy link
Member

scottmcm commented Feb 20, 2018

Range::is_empty (#48111) is another example of wanting the method on non-ESI.

@SimonSapin

This comment has been minimized.

Copy link
Contributor

SimonSapin commented Mar 28, 2018

The libs team discussed this and the consensus was to stabilize ExactSizeIterator as-is.

Iterator types that are not ExactSizeIterator but are still able to implement a meaningful is_empty method can do so in an inherent method. Code that needs to be generic over such types can define a non-standard-library trait.

@rfcbot fcp merge

@SimonSapin

This comment has been minimized.

Copy link
Contributor

SimonSapin commented Mar 28, 2018

Oops, it looks like rfcbot is not responding because a FCP was already completed in 2016. Anyone please make a stabilization PR, we’ll FCP there.

@clarfon

This comment has been minimized.

Copy link
Contributor

clarfon commented Mar 28, 2018

(is the rfcbot code in a repo somewhere? because I'd be willing to take a look at that)

@clarfon

This comment has been minimized.

Copy link
Contributor

clarfon commented Mar 28, 2018

As far as a proper method goes, I think that perhaps an is_empty method could also be added to FusedIterator, and is_empty could be added to Fuse. Although I think the former is already stable, so, I'm not sure if we can do that...

@SimonSapin

This comment has been minimized.

Copy link
Contributor

SimonSapin commented Mar 28, 2018

@scottmcm

This comment has been minimized.

Copy link
Member

scottmcm commented Mar 29, 2018

It still seems weird to have is_empty only for things that meet the "here to make rposition work" rules of ExactSizeIterator. There are so many things for which it's obvious that it could exist, like chain, but it won't. Having it here gets literally nothing over .len() == 0, which isn't even shorter than .is_empty(). And making a third-party trait for it would be painful at best, since ExactSizeIterator is in the prelude and thus trying to call it would be ambiguous with no nice workaround.

@SimonSapin

This comment has been minimized.

Copy link
Contributor

SimonSapin commented Mar 31, 2018

I can’t tell whether you’re arguing that is_empty is so important that it should have a dedicated trait, or that it’s unimportant enough that it doesn’t need to exist at all. We’re not adding a dedicated trait right now, but it or inherent methods on other iterators can still be proposed in a separate RFC or PR. As to .len() == 0, I think it’s less about character count than about clarifying intent. Collections (slices, maps) already have an is_empty method.

@nvzqz

This comment has been minimized.

Copy link
Contributor

nvzqz commented Mar 31, 2018

On top of being able to express intent with is_empty, it may be more optimal to call than .len() == 0. One case that comes to mind is a linked list iterator where is_empty is a constant time operation whereas len may take linear time.

I don't think is_empty should go on ExactSizeIterator, however. There may be iterators where the remaining number of elements is unknown, but it is known whether or not there are more elements left.

@scottmcm

This comment has been minimized.

Copy link
Member

scottmcm commented Apr 1, 2018

@SimonSapin I'm arguing that is_empty is too useful to be restricted to only things (particularly adapters) that are ExactSizedIterator. (I agree that the clearer intent is valuable.) And I don't think "a dedicated trait" can be usefully added after this is stabilized, since it'd cause name conflicts with this one. I'd rather live with inherent methods and only-a-tiny-bit-worse .len() == 0 for now to keep from closing off a straight-forward path to having .is_empty() on things like Chain.

I don't think inherent methods are a great option for is_empty() either, since they disappear as soon as you .map() them. And there's no reason Map<RangeInclusive<usize>, _>, say, shouldn't have is_empty.

@clarfon

This comment has been minimized.

Copy link
Contributor

clarfon commented Apr 1, 2018

I think that putting it on FusedIterator would be best but that would require un-stabilising it after it's been in beta, when a release is around the corner.

@Phlosioneer

This comment has been minimized.

Copy link
Contributor

Phlosioneer commented Feb 1, 2019

Triage: What's barring this from stabilization? It's been on nightly for... 6 months now, I think?

@scottmcm

This comment has been minimized.

Copy link
Member

scottmcm commented Feb 1, 2019

@Phlosioneer I don't think the situation has changed in the ~18 months since I wrote #35428 (comment)

(More than that, since the "this should be on things that are not ESI" predates that, like #35428 (comment))

If you're looking at the labels, the finished-FCP is not for is_empty: #35428 (comment)

@Phlosioneer

This comment has been minimized.

Copy link
Contributor

Phlosioneer commented Feb 1, 2019

So we're not doing anything here because we haven't decided what to do about TrustedLen. And we haven't decided what to do about TrustedLen because no one has commented on it in 6 months.

Cross-referencing #37572

To your earlier comment: TrustedLen defines no methods. ExactSizeIterator defines len(). Everything else with a len() has a way to avoid len() == 0 by using is_empty() instead. TrustedLen will never have a len() or any similar method, because it can't be reduced to a usize by definition. I feel like there's no conflict of interest here between the two traits.

@Phlosioneer

This comment has been minimized.

Copy link
Contributor

Phlosioneer commented Feb 1, 2019

Also, we should fix the labels on this issue if they are incorrect. However, this issue is solely about stabilizing the is_empty function. The original call for RFC was specifically for is_empty. If we leave them as-is, this issue can easily be looked at as "If no one disagrees, is_empty will be merged." Particularly with the disposition-merge tag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.