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

Adding is_zero() to core::time::Duration #2809

Closed
ghost opened this issue Nov 9, 2019 · 9 comments
Closed

Adding is_zero() to core::time::Duration #2809

ghost opened this issue Nov 9, 2019 · 9 comments
Labels
A-time Proposals relating to time & dates. T-libs-api Relevant to the library API team, which will review and decide on the RFC.

Comments

@ghost
Copy link

ghost commented Nov 9, 2019

Hello Rust developers,

I apologize beforehand because I think this is a simple RFC, so I'm not going through full requirements of an RFC issue.

Would you please consider adding new function (below) to core::time::Duration?

/// # Checks if this duration is zero
pub fn is_zero(&self) -> bool;

When taking options from users, I find myself many times the need to check if a duration is zero. Perhaps it could be the same for other developers.

Thank you very much for your hard work,

@RustyYato
Copy link

For simple additions like this, you can just submit a pull request to get it added. But why do we need this? How would you implement this?

@jhpratt
Copy link
Member

jhpratt commented Nov 10, 2019

Though it is trivial to do with value == Duration::from_secs(0), I actually implemented this myself in the rewrite of the time crate. But I agree with @KrishnaSannasi, an RFC isn't necessary for something like this.

@CryZe
Copy link

CryZe commented Nov 10, 2019

I don't think this is sufficient motivation though. Integers, floats and co. don't have a .is_zero() either. What is this actually used for?

@ghost
Copy link
Author

ghost commented Nov 10, 2019

@CryZe

Integers can be compared to 0 easily, for example:

let flag: u8 = ...;
if flag > 0 {
    // ...
}

A Duration can't be compared to zero. Comparing a duration to zero can be useful in case the program takes input from user but doesn't allow a zero-duration.

@shepmaster
Copy link
Member

It seems in line with the various is_empty methods already existing.

@ghost
Copy link
Author

ghost commented Nov 11, 2019

@shepmaster

Thanks for your input. is_empty() is common to collections (string, vector, set, map, slice...) While a duration is not related to a collection.

However English is not my first language, so I'm not sure.

@shepmaster
Copy link
Member

My point was that is_zero fits in the same conceptual space as is_empty, which we use instead of len() == 0. I agree such a thing could be added.

It would be nice to find a different word besides "zero" though. We have is_empty, not is_len_zero. Unfortunately, nothing obvious is coming up.

Another possibility is creating a const ZERO: Duration = ... in the time module.

@ghost
Copy link
Author

ghost commented Nov 12, 2019

I followed full procedure and made this pull request: #2814

@jonas-schievink jonas-schievink added A-time Proposals relating to time & dates. T-libs-api Relevant to the library API team, which will review and decide on the RFC. labels Nov 15, 2019
@jonas-schievink
Copy link
Contributor

Closing this for the same reason as #2814 – it is not necessary to go through the full RFC process for this small addition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-time Proposals relating to time & dates. T-libs-api Relevant to the library API team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

5 participants