This repository was archived by the owner on May 28, 2025. It is now read-only.
Commit c1f9d98
Yuki Okushi
Rollup merge of rust-lang#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: rust-lang#83400.
### Implementation History
- rust-lang#82179
- rust-lang#90247
- rust-lang#96051
- Changed error type to `FromFloatSecsError` in rust-lang#90247
- rust-lang#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 rust-lang#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: artichoke/artichoke#2194.
`@rustbot` label +T-libs-api -T-libs
cc `@mbartlett21`5 files changed
+21
-26
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
493 | 493 | | |
494 | 494 | | |
495 | 495 | | |
496 | | - | |
497 | | - | |
| 496 | + | |
| 497 | + | |
498 | 498 | | |
499 | 499 | | |
500 | 500 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1225 | 1225 | | |
1226 | 1226 | | |
1227 | 1227 | | |
1228 | | - | |
1229 | 1228 | | |
1230 | 1229 | | |
1231 | 1230 | | |
1232 | 1231 | | |
1233 | 1232 | | |
1234 | 1233 | | |
1235 | 1234 | | |
1236 | | - | |
1237 | | - | |
1238 | | - | |
| 1235 | + | |
| 1236 | + | |
| 1237 | + | |
1239 | 1238 | | |
1240 | 1239 | | |
1241 | | - | |
| 1240 | + | |
1242 | 1241 | | |
1243 | 1242 | | |
1244 | | - | |
| 1243 | + | |
1245 | 1244 | | |
1246 | 1245 | | |
1247 | | - | |
| 1246 | + | |
1248 | 1247 | | |
1249 | 1248 | | |
1250 | 1249 | | |
1251 | 1250 | | |
1252 | 1251 | | |
1253 | 1252 | | |
1254 | | - | |
1255 | | - | |
| 1253 | + | |
| 1254 | + | |
1256 | 1255 | | |
1257 | 1256 | | |
1258 | 1257 | | |
1259 | 1258 | | |
1260 | 1259 | | |
1261 | 1260 | | |
1262 | | - | |
| 1261 | + | |
1263 | 1262 | | |
1264 | 1263 | | |
1265 | 1264 | | |
| |||
1280 | 1279 | | |
1281 | 1280 | | |
1282 | 1281 | | |
1283 | | - | |
| 1282 | + | |
1284 | 1283 | | |
1285 | 1284 | | |
1286 | 1285 | | |
| |||
1339 | 1338 | | |
1340 | 1339 | | |
1341 | 1340 | | |
1342 | | - | |
| 1341 | + | |
1343 | 1342 | | |
1344 | 1343 | | |
1345 | 1344 | | |
| |||
1355 | 1354 | | |
1356 | 1355 | | |
1357 | 1356 | | |
1358 | | - | |
1359 | | - | |
1360 | 1357 | | |
1361 | 1358 | | |
1362 | 1359 | | |
| |||
1404 | 1401 | | |
1405 | 1402 | | |
1406 | 1403 | | |
1407 | | - | |
| 1404 | + | |
| 1405 | + | |
1408 | 1406 | | |
1409 | | - | |
| 1407 | + | |
1410 | 1408 | | |
1411 | 1409 | | |
1412 | 1410 | | |
| |||
1425 | 1423 | | |
1426 | 1424 | | |
1427 | 1425 | | |
1428 | | - | |
1429 | | - | |
1430 | 1426 | | |
1431 | 1427 | | |
1432 | 1428 | | |
| |||
1482 | 1478 | | |
1483 | 1479 | | |
1484 | 1480 | | |
1485 | | - | |
| 1481 | + | |
| 1482 | + | |
1486 | 1483 | | |
1487 | | - | |
| 1484 | + | |
1488 | 1485 | | |
1489 | 1486 | | |
1490 | 1487 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
103 | 103 | | |
104 | 104 | | |
105 | 105 | | |
106 | | - | |
107 | 106 | | |
108 | 107 | | |
109 | 108 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
280 | 280 | | |
281 | 281 | | |
282 | 282 | | |
283 | | - | |
284 | 283 | | |
285 | 284 | | |
286 | 285 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
46 | | - | |
47 | | - | |
| 46 | + | |
| 47 | + | |
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
| |||
0 commit comments