rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.37k stars 12.72k forks source link

Add `is_empty` function to `ExactSizeIterator` #35428

Open frewsxcv opened 8 years ago

frewsxcv commented 8 years ago

Tracking issue for functionality introduced in https://github.com/rust-lang/rust/pull/34357

frewsxcv commented 8 years ago

Added reference to this issue in https://github.com/rust-lang/rust/pull/35429

SimonSapin commented 8 years ago

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 commented 8 years ago

@rfcbot fcp merge

Seems good to have consistency!

rfcbot commented 8 years ago

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 commented 8 years ago

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

SimonSapin commented 8 years ago

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 commented 8 years ago

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

rfcbot commented 8 years ago

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

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

rfcbot commented 7 years ago

The final comment period is now complete.

bluss commented 7 years ago

used in PR #37943

bluss commented 7 years ago

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 commented 7 years ago

@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 commented 7 years ago

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

bluss commented 7 years ago
aturon commented 7 years ago

@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 commented 7 years ago

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

aturon commented 7 years ago

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

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 commented 7 years ago

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 commented 7 years ago

Did the IsEmpty trait preempt the stabilization FCP?

aturon commented 7 years ago

@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.

bluss commented 7 years ago

I'd like to modify ESI to be something like this:

trait ExactSizeIterator : IsEmpty {
    fn len(&self) -> usize;
}

trait IsEmpty {
    fn is_empty(&self) -> bool;
}

impl<T> IsEmpty for T where T: ExactSizeIterator {
    default fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

specialization seems to implement this correctly, so I don't immediately see any problems with it. It doesn't resolve having the method in the prelude.

Iterator could get a new method:

pub trait Iterator {
    ...
    fn is_empty(&self) -> bool
        where Self: IsEmpty
    {
        IsEmpty::is_empty(self)
    }
}

which puts it in the prelude.

aturon commented 7 years ago

cc @rust-lang/libs, thoughts on @bluss's proposed change?

The overall motivation here is to allow for greater customization of is_empty, and to pass it through iterator adapters and the like.

Note that customization would currently only be allowed on nightly via specialization.

bluss commented 7 years ago

And so that chars() has the same is_empty even though it's not an ESI.

alexcrichton commented 7 years ago

Given that is_empty was basically purely added for our len convention (where if you have len you have is_empty) I feel like blowing that up into an entire trait with specialization and new methods is way overkill.

If we can't stabilize it as is I would prefer to remove it as it doesn't seem to have strong enough motivation for a new trait and combinator.

bluss commented 7 years ago

Right, I don't think it's right to add an is_empty only for ESI, when it makes sense for a much larger class of iterators. I could see is_empty coming back in a new trait with an RFC, though.

@SimonSapin Thoughts? I thought you might weigh in on chars() and related iterators, if they should have is_empty() too.

SimonSapin commented 7 years ago

An IsEmpty trait also seems slightly overkill to me, but I’m ok with it if other people want it.

I think it’s also fine to not have a dedicated trait but only a default method on ExactSizeIterator and an inherent method on str::Chars (and others as appropriate). Note that the latter is already possible through .as_str().is_empty().

clarfonthey commented 7 years ago

Personally I think that is_empty as a convenience method here is useful; I've found myself directly checking is_empty on iterators sometimes and removing this method would be a bit of a pain.

Also, on the note of generic traits for is_empty, I've been slowly working on my len-trait crate and that may be of use to you, @bluss. In the future it may be worth having some sort of generalisation over these things in stdlib, but atm I'd honestly rather just have is_empty on ExactSizeIterator and deal with having the method duplicated over multiple traits if it comes down to it.

nvzqz commented 7 years ago

I'm very much in favor of a separate IsEmpty trait as described by @bluss.

One solid use case of is_empty() is in a chess engine. They commonly use a Bitboard type that can be implemented as an exact size iterator of 64 bits that map to squares. len() would be implemented via count_ones() on a u64. However, is_empty() can be made faster by simply checking if the internal u64 is zero rather than checking if len() returns zero.

SimonSapin commented 7 years ago

@nvzqz Is this chess board type used in a generic context? If not you can make an inherent is_empty method, no need for a standard library trait.

clarfonthey commented 7 years ago

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 commented 7 years ago

@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.

clarfonthey commented 7 years ago

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

ranma42 commented 7 years ago

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

clarfonthey commented 7 years ago

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

nvzqz commented 7 years ago

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

SimonSapin commented 7 years ago

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.

clarfonthey commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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 commented 6 years ago

Range::is_empty (https://github.com/rust-lang/rust/issues/48111) is another example of wanting the method on non-ESI.

SimonSapin commented 6 years ago

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 commented 6 years ago

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.

clarfonthey commented 6 years ago

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

clarfonthey commented 6 years ago

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 commented 6 years ago

(rfcbot is at https://github.com/anp/rfcbot-rs/)

scottmcm commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

@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.

clarfonthey commented 6 years ago

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.