rust/library/core/src
Yuki Okushi c1f9d985d7
Rollup merge of #102271 - lopopolo:lopopolo/stabilize-duration-try-from-secs-float, r=dtolnay
Stabilize `duration_checked_float`

## Stabilization Report

This stabilization report is for a stabilization of `duration_checked_float`, tracking issue: https://github.com/rust-lang/rust/issues/83400.

### Implementation History

- https://github.com/rust-lang/rust/pull/82179
- https://github.com/rust-lang/rust/pull/90247
- https://github.com/rust-lang/rust/pull/96051
- Changed error type to `FromFloatSecsError` in https://github.com/rust-lang/rust/pull/90247
- https://github.com/rust-lang/rust/pull/96051 changes the rounding mode to round-to-nearest instead of truncate.

## API Summary

This stabilization report proposes the following API to be stabilized in `core`, along with their re-exports in `std`:

```rust
// core::time

impl Duration {
    pub const fn try_from_secs_f32(secs: f32) -> Result<Duration, TryFromFloatSecsError>;
    pub const fn try_from_secs_f64(secs: f64) -> Result<Duration, TryFromFloatSecsError>;
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TryFromFloatSecsError { ... }

impl core::fmt::Display for TryFromFloatSecsError { ... }
impl core::error::Error for TryFromFloatSecsError { ... }
```

These functions are made const unstable under `duration_consts_float`, tracking issue #72440.

There is an open question in the tracking issue around what the error type should be called which I was hoping to resolve in the context of an FCP.

In this stabilization PR, I have altered the name of the error type to `TryFromFloatSecsError`. In my opinion, the error type shares the name of the method (adjusted to accommodate both types of floats), which is consistent with other error types in `core`, `alloc` and `std` like `TryReserveError` and `TryFromIntError`.

## Experience Report

Code such as this is ready to be converted to a checked API to ensure it is panic free:

```rust
impl Time {
    pub fn checked_add_f64(&self, seconds: f64) -> Result<Self, TimeError> {
        // Fail safely during `f64` conversion to duration
        if seconds.is_nan() || seconds.is_infinite() {
            return Err(TzOutOfRangeError::new().into());
        }

        if seconds.is_sign_positive() {
            self.checked_add(Duration::from_secs_f64(seconds))
        } else {
            self.checked_sub(Duration::from_secs_f64(-seconds))
        }
    }
}
```

See: https://github.com/artichoke/artichoke/issues/2194.

`@rustbot` label +T-libs-api -T-libs

cc `@mbartlett21`
2022-10-24 19:32:26 +09:00
..
alloc Rollup merge of #102072 - scottmcm:ptr-alignment-type, r=thomcc 2022-10-10 00:09:40 +09:00
array Rollup merge of #100462 - zohnannor:master, r=thomcc 2022-10-23 14:48:13 -07:00
async_iter Add Stream alias for AsyncIterator 2022-03-15 20:59:13 -07:00
cell Move/rename lazy::{OnceCell, Lazy} to cell::{OnceCell, LazyCell} 2022-06-16 19:53:59 +04:00
char Clarify the possible return values of len_utf16 2022-10-16 11:06:19 -04:00
convert Rollup merge of #102628 - H4x5:master, r=scottmcm 2022-10-04 06:14:12 +02:00
ffi Add is_empty() method to core::ffi::CStr. 2022-09-29 07:55:12 +09:00
fmt Auto merge of #99099 - Stargateur:phantomdata_debug, r=joshtriplett 2022-10-04 00:56:14 +00:00
future Implement Ready::into_inner() 2022-08-30 13:39:30 +02:00
hash Adjust cfgs 2022-08-12 16:28:15 -04:00
iter specialize slice_iter.copied().next_chunk() 2022-10-19 00:02:00 +02:00
macros Fix typo in concat_bytes documentation 2022-09-12 21:40:28 -04:00
mem MaybeUninit: use assume_init_drop() in the partially initialized array example 2022-10-23 19:09:18 +02:00
num Rollup merge of #101889 - tspiteri:redoc-uint-adc-sbb, r=m-ou-se 2022-10-18 21:18:46 +02:00
ops Fixed Documentation for wrap_mut_2_imp 2022-09-30 17:16:59 +02:00
panic Change tracking issue from #76156 to #102911 2022-10-11 06:40:37 +00:00
prelude Create 2024 edition 2022-04-02 02:45:49 -04:00
ptr Auto merge of #100848 - xfix:use-metadata-for-slice-len, r=thomcc 2022-10-24 04:14:46 +00:00
slice Auto merge of #100848 - xfix:use-metadata-for-slice-len, r=thomcc 2022-10-24 04:14:46 +00:00
str Fix typo in ReverseSearcher docs 2022-10-17 13:14:15 -04:00
sync Remove extra spaces 2022-10-19 23:54:00 +01:00
task Added tracking issue 2022-09-19 15:07:12 +02:00
unicode Bump Unicode to version 15.0.0, regenerate tables 2022-09-14 13:21:19 -07:00
any.rs Add Provider::{would_be_satisfied_by_value_of,would_be_satisfied_by_ref_of} 2022-08-23 10:48:59 -04:00
ascii.rs Inline <EscapeDefault as Iterator>::next 2022-03-10 15:35:22 +01:00
asserting.rs [RFC 2011] Library code 2022-05-22 07:18:32 -03:00
bool.rs Add missing assertion 2022-09-22 02:12:06 -04:00
borrow.rs Add const_traits 2022-09-16 11:48:42 +08:00
cell.rs Rollup merge of #101717 - Pointerbender:unsafecell-memory-layout, r=Amanieu 2022-10-16 11:41:12 +09:00
clone.rs update cfg(bootstrap)s 2022-07-01 15:48:23 +02:00
cmp.rs Add back ConstFnMutClosure::new, fix formatting 2022-09-30 17:41:01 +02:00
const_closure.rs Add back ConstFnMutClosure::new, fix formatting 2022-09-30 17:41:01 +02:00
default.rs Prevent errors for stage0 rustc build 2022-09-16 11:48:42 +08:00
error.md Move error trait into core 2022-08-22 13:28:25 -07:00
error.rs Stabilize duration_checked_float 2022-10-15 12:02:13 -07:00
hint.rs add a few more assert_unsafe_precondition 2022-10-07 14:35:12 +02:00
internal_macros.rs ignore a doctest for the non-exported macro 2022-05-03 18:33:56 +09:00
intrinsics.rs Adjust transmute{,_copy} to be clearer about which of T and U is input vs output 2022-10-19 22:36:14 -07:00
lib.rs Add default trait implementations for "c-unwind" ABI function pointers 2022-10-19 19:17:32 -07:00
marker.rs PhantomData: inline a macro that is used only once 2022-10-16 10:37:51 +02:00
option.rs Change feature name to is_some_and 2022-10-01 11:45:52 -05:00
panic.rs resolve the conflict in compiler/rustc_session/src/parse.rs 2022-03-16 20:12:30 +08:00
panicking.rs reorder panicking.rs to put main entry points at the top 2022-10-11 22:47:31 +02:00
pin.rs Fix the generator example for pin!() 2022-05-05 09:58:13 +10:00
primitive.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
primitive_docs.rs array docs - advertise how to get array from slice 2022-09-10 19:37:07 -07:00
result.rs Auto merge of #98354 - camsteffen:is-some-and-by-value, r=m-ou-se 2022-10-02 12:48:15 +00:00
time.rs Stabilize duration_checked_float 2022-10-15 12:02:13 -07:00
tuple.rs Added const Default impls for Arrays and Tuples. 2022-09-23 17:53:59 +02:00
unit.rs Use implicit capture syntax in format_args 2022-03-10 10:23:40 -05:00