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

Implement Default for more types in the standard library #32785

Merged
merged 1 commit into from
Apr 16, 2016

Conversation

tbu-
Copy link
Contributor

@tbu- tbu- commented Apr 7, 2016

Also add Hash to std::cmp::Ordering and most possible traits to
fmt::Error.

@rust-highfive
Copy link
Collaborator

r? @aturon

(rust_highfive has picked a reviewer for you, use r? to override)

@tbu-
Copy link
Contributor Author

tbu- commented Apr 7, 2016

There was a reddit comment that motivated this (especially for Mutex and RwLock), but I can't find it anymore.

@bluss
Copy link
Member

bluss commented Apr 7, 2016

The added other trait impls also should be mentioned in the commit message or PR.

@tbu-
Copy link
Contributor Author

tbu- commented Apr 7, 2016

@bluss Updated.

@alexcrichton
Copy link
Member

Thanks @tbu-! Tagging with T-libs to ensure that this comes up during triage.

@alexcrichton alexcrichton added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Apr 7, 2016
#[stable(feature = "duration_default", since = "1.9.0")]
impl Default for Duration {
fn default() -> Duration {
Duration::new(0, 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would it fail at runtime? The drawbacks were not fully explained on SO.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@llogiq
Copy link
Contributor

llogiq commented Apr 8, 2016

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.

@llogiq
Copy link
Contributor

llogiq commented Apr 8, 2016

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.

@alexcrichton
Copy link
Member

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 Path and CStr seem like "just because", so can they be removed for now? Additionally, the Duration default seems like it's not "clearly the right choice" so can that be removed for now as well? (erring on the side of being conservative).

Other than that though this looked good to us!

@tbu-
Copy link
Contributor Author

tbu- commented Apr 15, 2016

&CStr is pretty close to &str and Default is implemented for that type. Default is also implemented for &[T] and &mut [T].

The use case I have in mind here is when you have a Option<&CStr>, an unwrap_or_default should give you the empty string (like for Option<&str>).

Also add `Hash` to `std::cmp::Ordering` and most possible traits to
`fmt::Error`.
@llogiq
Copy link
Contributor

llogiq commented Apr 15, 2016

I also see no problem with CStr. Duration on the other hand should probably wait.

@alexcrichton
Copy link
Member

Yeah splitting them out is fine, let's merge this and continue discussion on #32990

@bors: r+ 3df35a0

@bors
Copy link
Contributor

bors commented Apr 16, 2016

⌛ Testing commit 3df35a0 with merge 6fa61b8...

bors added a commit that referenced this pull request Apr 16, 2016
Implement `Default` for more types in the standard library

Also add `Hash` to `std::cmp::Ordering` and most possible traits to
`fmt::Error`.
@bors bors merged commit 3df35a0 into rust-lang:master Apr 16, 2016
@llogiq
Copy link
Contributor

llogiq commented Apr 16, 2016

@tbu- 👍

@llogiq
Copy link
Contributor

llogiq commented Apr 16, 2016

This fixes #31865.

@bombless
Copy link
Contributor

I cannot think of a more proper default value for Duration other than zero though.

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 Default might just be used casually, like https://github.com/bombless/to_default/blob/master/examples/simple.rs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants