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

Tracking issue for duration_float #54361

Closed
newpavlov opened this issue Sep 19, 2018 · 11 comments · Fixed by #62756
Closed

Tracking issue for duration_float #54361

newpavlov opened this issue Sep 19, 2018 · 11 comments · Fixed by #62756
Labels
B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@newpavlov
Copy link
Contributor

newpavlov commented Sep 19, 2018

Rust PRs: #52813, #59102

This feature adds the following float related methods to Duration:

  • as_secs_f64(&self) -> f64
  • as_secs_f32(&self) -> f32
  • from_secs_f64(secs: f64) -> Duration
  • from_secs_f32(secs: f32) -> Duration
  • mul_f64(self, rhs: f64) -> Duration
  • mul_f32(self, rhs: f32) -> Duration
  • div_f64(self, rhs: f64) -> Duration
  • div_f32(self, rhs: f64) -> Duration

div_duration methods are tracked in #63139.

@alexcrichton alexcrichton added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. B-unstable Blocker: Implemented in the nightly compiler and unstable. labels Sep 19, 2018
@m-ou-se
Copy link
Member

m-ou-se commented Jan 19, 2019

What needs to happen before this can be stabilized?

@icefoxen
Copy link
Contributor

icefoxen commented Jan 23, 2019

f64 vs f32

The only sane options are "have versions for both", or "f64 because it's more general and people can always downcast." You can't fit a billion nanoseconds into the 24 bit significand of a f32.

Also, yes, what needs to happen for this to stabilize?

@newpavlov
Copy link
Contributor Author

I guess we probably should rename methods to as_f64_secs, from_f64_secs and div_duration_f64, so we will have an option to add analogous f32 methods.

@smokytheangel0
Copy link

What can I do to help?

@aloucks
Copy link
Contributor

aloucks commented Feb 23, 2019

I guess we probably should rename methods to as_f64_secs, from_f64_secs and div_duration_f64, so we will have an option to add analogous f32 methods.

as_secs_f64 and from_secs_f64 would be more consistent.

pietroalbini added a commit to pietroalbini/rust that referenced this issue Mar 13, 2019
…chton

Consistent naming for duration_float methods and additional f32 methods

`duration_float` tracking issue: rust-lang#54361

New list of methods:
- `as_secs_f64(&self) -> f64`
- `as_secs_f32(&self) -> f32`
- `from_secs_f64(secs: f64) -> Duration`
- `from_secs_f32(secs: f32) -> Duration`
- `mul_f64(self, rhs: f64) -> Duration`
- `mul_f32(self, rhs: f32) -> Duration`
- `div_f64(self, rhs: f64) -> Duration`
- `div_f32(self, rhs: f64) -> Duration`
- `div_duration_f64(self, rhs: Duration) -> f64`
- `div_duration_f32(self, rhs: Duration) -> f32`

With [`num_traits::Float`](https://docs.rs/num-traits/0.2.6/num_traits/float/trait.Float.html) we could've reduced number of methods by factor of two, but unfortunately it's not part of `std`.
Centril added a commit to Centril/rust that referenced this issue Mar 13, 2019
…chton

Consistent naming for duration_float methods and additional f32 methods

`duration_float` tracking issue: rust-lang#54361

New list of methods:
- `as_secs_f64(&self) -> f64`
- `as_secs_f32(&self) -> f32`
- `from_secs_f64(secs: f64) -> Duration`
- `from_secs_f32(secs: f32) -> Duration`
- `mul_f64(self, rhs: f64) -> Duration`
- `mul_f32(self, rhs: f32) -> Duration`
- `div_f64(self, rhs: f64) -> Duration`
- `div_f32(self, rhs: f64) -> Duration`
- `div_duration_f64(self, rhs: Duration) -> f64`
- `div_duration_f32(self, rhs: Duration) -> f32`

With [`num_traits::Float`](https://docs.rs/num-traits/0.2.6/num_traits/float/trait.Float.html) we could've reduced number of methods by factor of two, but unfortunately it's not part of `std`.
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this issue Mar 14, 2019
…chton

Consistent naming for duration_float methods and additional f32 methods

`duration_float` tracking issue: rust-lang#54361

New list of methods:
- `as_secs_f64(&self) -> f64`
- `as_secs_f32(&self) -> f32`
- `from_secs_f64(secs: f64) -> Duration`
- `from_secs_f32(secs: f32) -> Duration`
- `mul_f64(self, rhs: f64) -> Duration`
- `mul_f32(self, rhs: f32) -> Duration`
- `div_f64(self, rhs: f64) -> Duration`
- `div_f32(self, rhs: f64) -> Duration`
- `div_duration_f64(self, rhs: Duration) -> f64`
- `div_duration_f32(self, rhs: Duration) -> f32`

With [`num_traits::Float`](https://docs.rs/num-traits/0.2.6/num_traits/float/trait.Float.html) we could've reduced number of methods by factor of two, but unfortunately it's not part of `std`.
kennytm added a commit to kennytm/rust that referenced this issue Mar 15, 2019
…chton

Consistent naming for duration_float methods and additional f32 methods

`duration_float` tracking issue: rust-lang#54361

New list of methods:
- `as_secs_f64(&self) -> f64`
- `as_secs_f32(&self) -> f32`
- `from_secs_f64(secs: f64) -> Duration`
- `from_secs_f32(secs: f32) -> Duration`
- `mul_f64(self, rhs: f64) -> Duration`
- `mul_f32(self, rhs: f32) -> Duration`
- `div_f64(self, rhs: f64) -> Duration`
- `div_f32(self, rhs: f64) -> Duration`
- `div_duration_f64(self, rhs: Duration) -> f64`
- `div_duration_f32(self, rhs: Duration) -> f32`

With [`num_traits::Float`](https://docs.rs/num-traits/0.2.6/num_traits/float/trait.Float.html) we could've reduced number of methods by factor of two, but unfortunately it's not part of `std`.
kennytm added a commit to kennytm/rust that referenced this issue Mar 16, 2019
…chton

Consistent naming for duration_float methods and additional f32 methods

`duration_float` tracking issue: rust-lang#54361

New list of methods:
- `as_secs_f64(&self) -> f64`
- `as_secs_f32(&self) -> f32`
- `from_secs_f64(secs: f64) -> Duration`
- `from_secs_f32(secs: f32) -> Duration`
- `mul_f64(self, rhs: f64) -> Duration`
- `mul_f32(self, rhs: f32) -> Duration`
- `div_f64(self, rhs: f64) -> Duration`
- `div_f32(self, rhs: f64) -> Duration`
- `div_duration_f64(self, rhs: Duration) -> f64`
- `div_duration_f32(self, rhs: Duration) -> f32`

With [`num_traits::Float`](https://docs.rs/num-traits/0.2.6/num_traits/float/trait.Float.html) we could've reduced number of methods by factor of two, but unfortunately it's not part of `std`.
@ghedo
Copy link
Contributor

ghedo commented Jun 7, 2019

What's left to do to stabilize this feature? Any ETA?

@icefoxen
Copy link
Contributor

It looks like this is finished and can be marked as stabilized, and so incorporated into the next stable release. Is there anything I can do to help with that?

@tyranron
Copy link

tyranron commented Sep 26, 2019

Is there any discussion or reasoning of why such duplicative interface was chosen?

It seems that a generic interface like from_secs<T>(secs: T) -> Duration will feel more natural/idiomatic for such generics-adopted language like Rust is.

I've found only this mention:

With num_traits::Float we could've reduced number of methods by factor of two, but unfortunately it's not part of std.

But one can argue that in this situation another addhoc inner trait can be introduced in std.

Would you be so kind to explain the reasoning of current stabilized interface?

@faern
Copy link
Contributor

faern commented Sep 27, 2019

Yeah, having standard library traits for floats and ints implemented by all corresponding types would be good for many places. But this is probably not the place to discuss that in particular.

I also feel this API is very duplicating. My interpretation is that the reasoning behind it is that there is some loss of precision involved with using floats, and people want that to stand out so it does not go unnoticed. See this message and the messages leading up to it: #62756 (comment)

If there were a Float trait then the two extra methods for each operation could at least be joined to one. For example from_secs_float(secs: impl Float) -> Duration, and the duplication could be cut in half.

EDIT: Probably better to have a specified generic, so callers can explicitly state the resolution: from_secs_float<F: Float>(secs: F) -> Duration. Duration::from_secs_float::<f64>(variable)

EDIT2: This can be done with current standard library like from_secs_float<F: Into<f64>>(secs: F). But it would then also allow passing integers. And it would require the implementation to cast to f64 before actually creating the Duration

@tyranron
Copy link

@faern why not something like from_secs<T: DurationFromSecs>(secs: T) -> Duration, where DurationFromSecs can be the inner or public sealed trait (or even not sealed at all, giving users the ability to use custom types) and contains the actual type-specific conversion code (not only floats), while the Duration type API remains simple, ergonomic, yet powerful.

Yeah, having standard library traits for floats and ints implemented by all corresponding types would be good for many places. But this is probably not the place to discuss that in particular.

Would you be so kind to point me to right places where this topic can be raised, discussed and won't remain unnoticed?

illicitonion added a commit to twitter/pants that referenced this issue Oct 4, 2019
illicitonion added a commit to twitter/pants that referenced this issue Oct 7, 2019
illicitonion added a commit to twitter/pants that referenced this issue Oct 9, 2019
illicitonion added a commit to twitter/pants that referenced this issue Oct 9, 2019
illicitonion added a commit to twitter/pants that referenced this issue Oct 14, 2019
illicitonion added a commit to pantsbuild/pants that referenced this issue Oct 14, 2019
@shadiakiki1986
Copy link

For future-comers, upgrading to rustc>=1.38 fixes this

est31 added a commit to est31/mimas that referenced this issue Sep 3, 2020
The duration_float feature will arrive in Rust 1.38.0 stable.

rust-lang/rust#54361
taiki-e added a commit to taiki-e/easytime that referenced this issue Jan 3, 2021
This adds `Duration::{as_secs_f64, as_secs_f32, from_secs_f64,
from_secs_f32, mul_f64, mul_f32, div_f64, div_f32}`  methods.

They are based on `duration_float`](rust-lang/rust#54361)
feature of the standard library that stabilized on Rust 1.39.
taiki-e added a commit to taiki-e/easytime that referenced this issue Jan 3, 2021
This adds `Duration::{as_secs_f64, as_secs_f32, from_secs_f64,
from_secs_f32, mul_f64, mul_f32, div_f64, div_f32}`  methods.

They are based on `duration_float`](rust-lang/rust#54361)
feature of the standard library that stabilized on Rust 1.38.
taiki-e added a commit to taiki-e/easytime that referenced this issue Jan 3, 2021
This adds `Duration::{as_secs_f64, as_secs_f32, from_secs_f64,
from_secs_f32, mul_f64, mul_f32, div_f64, div_f32}`  methods.

They are based on [`duration_float`](rust-lang/rust#54361)
feature of the standard library that stabilized on Rust 1.38.
bors bot added a commit to taiki-e/easytime that referenced this issue Jan 3, 2021
21: Add duration_float methods r=taiki-e a=taiki-e

This adds `Duration::{as_secs_f64, as_secs_f32, from_secs_f64, from_secs_f32, mul_f64, mul_f32, div_f64, div_f32}`  methods.

They are based on [`duration_float`](rust-lang/rust#54361) feature of the standard library that stabilized on Rust 1.38.

Co-authored-by: Taiki Endo <te316e89@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants