-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Implement Default
for std::time::Duration
#37546
Comments
This seems quite simple for a beginner, if everybody's ok with it, may i do it? |
This was originally proposed in #32785 where when the libs team discussed it concluded that there's not a "clearly correct" default value for the That being said I'd personally be ok with just returning a zero duration, so I'd be curious if others from @rust-lang/libs could weigh in on this. |
The zero duration seems as reasonable as a default as 0 does for integer types, so I'd be on board with this. If nothing else, given a |
I guess I can see that argument, like if you had: struct SomeOptions {
things: usize,
timeout: Duration,
} You probably wouldn't want to have For APIs that take a |
I agree that if we have a default, 0 is the only sensible one to choose. It just continues to make me nervous that I can't think of many situations where that's actually the default you want for a duration -- but in any case where a default even makes sense, you should be using @luser Out of curiosity, how is the duration being used in the struct that led you to this problem? And is 0 really the "right" default for your case? |
@aturon I wanted to use it to accumulate a running duration of a set of events in a struct that accumulates stats: Each event returns a |
@luser Thanks, makes perfect sense! TLDR: a good reason for a default duration is accumulation, and zero is the right starting point. And I can't really see any way in which this is a footgun (as we all keep saying, zero's the only value that could make sense anyway). So 👍. However, given that there was some contention about this before, I'm going to run this change by the full libs team for approval: @rfcbot fcp merge |
@Tyannn Once the team has agreed we want to make this change, we'd be happy to take a PR from you implementing it! |
Team member @aturon 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. |
🔔 This is now entering its final comment period, as per the review above. 🔔 psst @aturon, I wasn't able to add the |
Discussed in rust-lang#37546 the libs team reached the conclusion that a default zero duration seems like a reasonable implementation of the `Default` trait. Closes rust-lang#37546
I've opened a PR for this at: #37699 |
…r=brson std: Derive `Default` for `Duration`. Discussed in rust-lang#37546 the libs team reached the conclusion that a default zero duration seems like a reasonable implementation of the `Default` trait. Closes rust-lang#37546
…r=brson std: Derive `Default` for `Duration`. Discussed in rust-lang#37546 the libs team reached the conclusion that a default zero duration seems like a reasonable implementation of the `Default` trait. Closes rust-lang#37546
Thanks for taking the time to work through this! |
Duration
doesn't implementDefault
. I found this out because I had a struct with#[derive(Default)]
and I tried to add aDuration
member to it, and that no longer worked. This means that I'd have to manually implementDefault
, which stinks because the struct has quite a few members. It seems like we could giveDuration
an implementation that just returns a zero-lengthDuration
.The text was updated successfully, but these errors were encountered: