Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upImplement `Default` for more types in the standard library #32785
Conversation
rust-highfive
assigned
aturon
Apr 7, 2016
This comment has been minimized.
This comment has been minimized.
|
r? @aturon (rust_highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
|
There was a reddit comment that motivated this (especially for |
This comment has been minimized.
This comment has been minimized.
|
The added other trait impls also should be mentioned in the commit message or PR. |
tbu-
force-pushed the
tbu-:pr_more_defaults
branch
from
da08168
to
b5b7fdb
Apr 7, 2016
This comment has been minimized.
This comment has been minimized.
|
@bluss Updated. |
This comment has been minimized.
This comment has been minimized.
|
Thanks @tbu-! Tagging with |
alexcrichton
added
the
T-libs
label
Apr 7, 2016
brson
referenced this pull request
Apr 7, 2016
Closed
impl Default for CondVar, Mutex and RwLock #32807
llogiq
reviewed
Apr 8, 2016
| #[stable(feature = "duration_default", since = "1.9.0")] | ||
| impl Default for Duration { | ||
| fn default() -> Duration { | ||
| Duration::new(0, 0) |
This comment has been minimized.
This comment has been minimized.
llogiq
Apr 8, 2016
Contributor
My question on StackOverflow has brought up the concern that a 0-duration is not a sensible default in most cases. Using this e.g. as a timeout is bound to fail at runtime. It's probably better not to implement Default for Duration at all.
This comment has been minimized.
This comment has been minimized.
bluss
Apr 8, 2016
Contributor
Why would it fail at runtime? The drawbacks were not fully explained on SO.
This comment has been minimized.
This comment has been minimized.
llogiq
Apr 8, 2016
Contributor
The problem is that Durations are often used as Timeouts and unless zero-duration timeouts are special case, the operation will always time out by default.
This comment has been minimized.
This comment has been minimized.
tbu-
Apr 8, 2016
Author
Contributor
I don't have strong feelings about this, we can definitely remove this if this isn't wanted. Two upsides: Using 0 seems to have some precedence (integers, floats), even though it's not clear where you'll use them. Implementing Default for more types means that you can #[derive] it in more cases.
This comment has been minimized.
This comment has been minimized.
llogiq
Apr 8, 2016
Contributor
Yeah, that was exactly the argument I made in the reddit comment. And of course the default just has to make intuitive sense; we cannot foresee possible applications. Still, I'd like to have more input for this before we commit to an implementation, especially as insta-stability means we cannot easily back off.
This comment has been minimized.
This comment has been minimized.
|
That reddit comment was by me. This would fix #31865 (and some more) by the way. I'm also unsure if this should be directly stable. |
This comment has been minimized.
This comment has been minimized.
|
OK, so TIL trait impls are insta-stable. Apart from being unsure about the 'correct' default value for Duration (zero seems to make intuitive sense for timestamp arithmetic, but is a bad choice for timeouts), I'm in favor of this. It's more complete than #32807. |
This comment has been minimized.
This comment has been minimized.
|
The libs team discussed this during triage yesterday and the decision was that most of these seem fine but we should avoid doing something just for the sake of doing something. In line with that the default impls for Other than that though this looked good to us! |
This comment has been minimized.
This comment has been minimized.
|
The use case I have in mind here is when you have a |
tbu-
force-pushed the
tbu-:pr_more_defaults
branch
from
b5b7fdb
to
3df35a0
Apr 15, 2016
tbu-
referenced this pull request
Apr 15, 2016
Merged
Add `Default` implementation for `&CStr`, `CString`, `Path` #32990
This comment has been minimized.
This comment has been minimized.
|
I also see no problem with |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
bors
added a commit
that referenced
this pull request
Apr 16, 2016
This comment has been minimized.
This comment has been minimized.
bors
merged commit 3df35a0
into
rust-lang:master
Apr 16, 2016
This comment has been minimized.
This comment has been minimized.
|
@tbu- |
This comment has been minimized.
This comment has been minimized.
|
This fixes #31865. |
This comment has been minimized.
This comment has been minimized.
|
I cannot think of a more proper default value for IMO if your scenario has a more proper value for a duration it should be a separate type and you may want to express duration itself through a trait. And the implementation of |
tbu- commentedApr 7, 2016
Also add
Hashtostd::cmp::Orderingand most possible traits tofmt::Error.