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

Projects
None yet
8 participants
@tbu-
Copy link
Contributor

tbu- commented Apr 7, 2016

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

@rust-highfive

This comment has been minimized.

Copy link
Collaborator

rust-highfive commented Apr 7, 2016

r? @aturon

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

@tbu-

This comment has been minimized.

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

This comment has been minimized.

Copy link
Contributor

bluss commented Apr 7, 2016

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

@tbu- tbu- force-pushed the tbu-:pr_more_defaults branch from da08168 to b5b7fdb Apr 7, 2016

@tbu-

This comment has been minimized.

Copy link
Contributor Author

tbu- commented Apr 7, 2016

@bluss Updated.

@alexcrichton

This comment has been minimized.

Copy link
Member

alexcrichton commented Apr 7, 2016

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

#[stable(feature = "duration_default", since = "1.9.0")]
impl Default for Duration {
fn default() -> Duration {
Duration::new(0, 0)

This comment has been minimized.

@llogiq

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.

@bluss

bluss Apr 8, 2016

Contributor

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

This comment has been minimized.

@llogiq

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.

@tbu-

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.

@llogiq

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.

@llogiq

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

Copy link
Member

alexcrichton commented Apr 15, 2016

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-

This comment has been minimized.

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>).

Implement `Default` for more types in the standard library
Also add `Hash` to `std::cmp::Ordering` and most possible traits to
`fmt::Error`.
@llogiq

This comment has been minimized.

Copy link
Contributor

llogiq commented Apr 15, 2016

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

@alexcrichton

This comment has been minimized.

Copy link
Member

alexcrichton commented Apr 15, 2016

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

@bors: r+ 3df35a0

@bors

This comment has been minimized.

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

Auto merge of #32785 - tbu-:pr_more_defaults, r=alexcrichton
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

2 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
homu Test successful
Details
@llogiq

This comment has been minimized.

Copy link
Contributor

llogiq commented Apr 16, 2016

@tbu- 👍

@llogiq

This comment has been minimized.

Copy link
Contributor

llogiq commented Apr 16, 2016

This fixes #31865.

@bombless

This comment has been minimized.

Copy link
Contributor

bombless commented Apr 26, 2016

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
You can’t perform that action at this time.