Skip to content

Trait for is_empty? #67409

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

Closed
mocsy opened this issue Dec 18, 2019 · 3 comments
Closed

Trait for is_empty? #67409

mocsy opened this issue Dec 18, 2019 · 3 comments
Labels
A-collections Area: `std::collections` C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@mocsy
Copy link

mocsy commented Dec 18, 2019

Lots of stuff in std provde pub fn is_empty, shouldn't there be a trait for it?

https://doc.rust-lang.org/nightly/std/collections/struct.HashMap.html#method.is_empty

@jonas-schievink
Copy link
Contributor

What would be the use case of abstracting over things that may be empty?

@jonas-schievink jonas-schievink added A-collections Area: `std::collections` C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Dec 18, 2019
@mocsy
Copy link
Author

mocsy commented Dec 18, 2019

I implement is_empty for my types, then skip their serialization entirely if they are empty.
I could write a derive macro for it as long as the type is composed of types which also implement is_empty.

@Mark-Simulacrum
Copy link
Member

I'm going to close this as I don't believe we'd be interested in such an abstraction. It's not too hard to implement yourself, and it's unclear what emptiness exactly means. If it's "is the iterator of this type empty" then that is better asked via .next().is_none() or so (on the first call) -- and indeed there's also #35428.

Generally speaking "emptiness" is not something that is easy to abstract over as an individual bound (i.e., T: IsEmpty is not useful as far as I can tell).

Note also that derives have no direct relationship to traits -- you can write a derive macro which expands to calling the is_empty method on many types already, and plausibly even provide that method via an extension trait for some types that don't implement it today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-collections Area: `std::collections` C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants