Commit Graph

115 Commits

Author SHA1 Message Date
mu001999 73c42c1800 Remove or allow unused features in library doc and tests 2026-02-13 09:27:16 +08:00
Jonathan Brouwer 7b105b21d1 Rollup merge of #149582 - max-heller:duration-integer-division, r=Mark-Simulacrum
Implement `Duration::div_duration_{floor,ceil}`
2025-12-28 22:52:30 +01:00
Josh Kaplan c74aa910ca Merged Duration Debug tie rounding with existing rounding logic 2025-12-22 09:25:57 -05:00
Josh Kaplan 95302b2537 Update comment on duration rounding behavior 2025-12-22 08:59:20 -05:00
Josh Kaplan f5aec79668 Implement round-ties-to-even for Duration Debug for consistency with f64 2025-12-22 08:59:20 -05:00
Boxy Uwu 90a33f69f4 replace version placeholder 2025-12-19 15:04:30 -08:00
Esteban Küber 0488690d3e Use let...else instead of match foo { ... _ => return }; and if let ... else return in std 2025-12-09 23:35:14 +00:00
Max Heller 23e99d3ec4 Implement Duration::div_duration_{floor,ceil} 2025-12-02 20:54:14 -05:00
Ralf Jung b032fac34e stabilize duration_from_nanos_u128 2025-11-06 18:08:29 +01:00
Mark Rousskov 4e9716fbc5 Update CURRENT_RUSTC_VERSION post-bump 2025-09-26 18:41:32 -04:00
actuallylost 8134a10ec7 Add Duration::from_nanos_u128
Tracking issue: RUST-139201

Co-authored-by: omanirudh <omanirudh2014@gmail.com>
2025-08-30 01:38:14 +03:00
Marijn Schouten 845311a065 remove deprecated Error::description in impls 2025-08-26 06:36:53 +00:00
Stuart Cook d06b3b432f Rollup merge of #143949 - clarfonthey:const-arith-ops, r=Amanieu
Constify remaining traits/impls for `const_ops`

Tracking issue: rust-lang/rust#143802

This is split into two commits for ease of reviewability:

1. Updates the `forward_ref_*` macros to accept multiple attributes (in anticipation of needing `rust_const_unstable` attributes) and also *require* attributes in these macros. Since the default attribute only helps for the initial implementations, it means it's easy to get wrong for future implementations, as shown for the saturating implementations which were incorrect before.
2. Actually constify the traits/impls.

A few random other notes on the implementation specifically:

* I unindented the attributes that were passed to the `forward_ref_*` macro calls because in some places rustfmt wanted them to be unindented, and in others it was allowed because they were themselves inside of macro bodies. I chose the consistent indenting even though I (personally) think it looks worse.

----

As far as the actual changes go, this constifies the following additional traits:

* `Neg`
* `Not`
* `BitAnd`
* `BitOr`
* `BitXor`
* `Shl`
* `Shr`
* `AddAssign`
* `SubAssign`
* `MulAssign`
* `DivAssign`
* `RemAssign`
* `BitAndAssign`
* `BitOrAssign`
* `BitXorAssign`
* `ShlAssign`
* `ShrAssign`

In terms of constified implementations of these traits, it adds the reference-forwarded versions of all the arithmetic operators, which are defined by the macros in `library/core/src/internal_macros.rs`. I'm not going to fully enumerate these because we'd be here all day, but sufficed to say, it effectively allows adding an `&` to one or both sides of an operator for primitives.

Additionally, I constified the implementations for `Wrapping`, `Saturating`, and `NonZero` as well, since all of them forward to already-const-stable methods. (potentially via intrinsics, to avoid extra overhead)

There are three "non-primitive" types which implement these traits, listed below. Note that I put "non-primitive" in quotes since I'm including `Wrapping`, `Saturating`, and `NonZero`, which are just wrappers over primitives.

* `Duration` (arithmetic operations)
* `SystemTime` (arithmetic operations)
* `Ipv4Addr` (bit operations)
* `Ipv6Addr` (bit operations)

Additionally, because the methods on `SystemTime` needed to make these operations const were not marked const, a separate tracking issue for const-stabilising those methods is rust-lang/rust#144517.

Stuff left out of this PR:

* `Assume` (this could trivially be made const, but since the docs indicate this is still under heavy design, I figured I'd leave it out)
* `Instant` (this could be made const, but cannot reasonably be constructed at constant time, so, isn't useful)
* `SystemTime` (will submit separate PR)
* SIMD types (I'm tackling these all at once later; see rust-lang/portable-simd#467)

<!-- TRIAGEBOT_START -->

<!-- TRIAGEBOT_CONCERN-ISSUE_START -->

> [!NOTE]
> # Concerns (0 active)
>
> - ~~[May break Clippy](https://github.com/rust-lang/rust/pull/143949#issuecomment-3081466077)~~ resolved in [this comment](https://github.com/rust-lang/rust/pull/143949#issuecomment-3083628215)
>
> *Managed by ```@rustbot`—see`` [help](https://forge.rust-lang.org/triagebot/concern.html) for details.*

<!-- TRIAGEBOT_CONCERN-ISSUE_END -->
<!-- TRIAGEBOT_END -->
2025-08-11 12:21:06 +10:00
ltdk bb32e31e65 Constify remaining operators 2025-08-10 01:11:45 -04:00
Kivooeo b5e2ba6775 Stabilize feature 2025-08-09 13:31:53 +05:00
Dietrich Daroch ed799c2019 Split duration_constructors to get non-controversial bits out faster. 2025-05-09 22:52:39 -04:00
许杰友 Jieyou Xu (Joe) e0846806db Rollup merge of #138082 - thaliaarchi:slice-cfg-not-test, r=thomcc
Remove `#[cfg(not(test))]` gates in `core`

These gates are unnecessary now that unit tests for `core` are in a separate package, `coretests`, instead of in the same files as the source code. They previously prevented the two `core` versions from conflicting with each other.
2025-03-16 09:40:05 +08:00
Mara Bos fb9ce02976 Limit formatting width and precision to 16 bits. 2025-03-10 12:20:05 +01:00
Thalia Archibald 638b226a6a Remove #[cfg(not(test))] gates in core
These gates are unnecessary now that unit tests for `core` are in a
separate package, `coretests`, instead of in the same files as the
source code. They previously prevented the two `core` versions from
conflicting with each other.
2025-03-06 13:21:59 -08:00
Peter Todd 4e4cb10b84 Add #[track_caller] to Duration Div impl
Previously the location of the divide-by-zero error condition would be
attributed to the code in the rust standard library, eg:

	thread 'main' panicked at /home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/time.rs:1172:31:
	divide by zero error when dividing duration by scalar

With #[track_caller] the error is correctly attributed to the callee.
2025-02-18 04:56:03 +00:00
Scott McMurray 6f2a78345e Update a bunch of library types for MCP807
This greatly reduces the number of places that actually use the `rustc_layout_scalar_valid_range_*` attributes down to just 3:
```
library/core\src\ptr\non_null.rs
68:#[rustc_layout_scalar_valid_range_start(1)]

library/core\src\num\niche_types.rs
19:        #[rustc_layout_scalar_valid_range_start($low)]
20:        #[rustc_layout_scalar_valid_range_end($high)]
```

Everything else -- PAL Nanoseconds, alloc's `Cap`, niched FDs, etc -- all just wrap those `niche_types` types.
2025-01-09 23:47:11 -08:00
Ralf Jung 66351a6184 get rid of a whole bunch of unnecessary rustc_const_unstable attributes 2024-11-02 09:59:55 +01:00
Josh Stone f204e2c23b replace placeholder version
(cherry picked from commit 567fd9610c)
2024-10-15 20:13:55 -07:00
George Bateman 4e438f7d6b Fix two const-hacks 2024-10-14 20:50:40 +01:00
Trevor Gross 19f6c17df4 Stabilize const_option
This makes the following API stable in const contexts:

    impl<T> Option<T> {
        pub const fn as_mut(&mut self) -> Option<&mut T>;
        pub const fn expect(self, msg: &str) -> T;
        pub const fn unwrap(self) -> T;
        pub const unsafe fn unwrap_unchecked(self) -> T;
        pub const fn take(&mut self) -> Option<T>;
        pub const fn replace(&mut self, value: T) -> Option<T>;
    }

    impl<T> Option<&T> {
        pub const fn copied(self) -> Option<T>
        where T: Copy;
    }

    impl<T> Option<&mut T> {
        pub const fn copied(self) -> Option<T>
        where T: Copy;
    }

    impl<T, E> Option<Result<T, E>> {
        pub const fn transpose(self) -> Result<Option<T>, E>
    }

    impl<T> Option<Option<T>> {
        pub const fn flatten(self) -> Option<T>;
    }

The following functions make use of the unstable
`const_precise_live_drops` feature:

- `expect`
- `unwrap`
- `unwrap_unchecked`
- `transpose`
- `flatten`

Fixes: <https://github.com/rust-lang/rust/issues/67441>
2024-10-12 17:07:13 -04:00
Ralf Jung 181e667626 stabilize duration_consts_float 2024-10-11 18:23:30 +02:00
Ralf Jung 332fa6aa6e add FIXME(const-hack) 2024-09-08 23:08:40 +02:00
Literally Void e2484be0c7 docs: add digit separators in Duration examples 2024-09-03 16:01:33 -07:00
Tshepang Mbambo af79a6396c time.rs: remove "Basic usage text"
Only one example is given (for each method)
2024-08-02 05:16:01 +02:00
bors f8060d282d Auto merge of #128083 - Mark-Simulacrum:bump-bootstrap, r=albertlarsan68
Bump bootstrap compiler to new beta

https://forge.rust-lang.org/release/process.html#master-bootstrap-update-t-2-day-tuesday
2024-07-30 17:49:08 +00:00
Mark Rousskov e8644f85b8 Update CURRENT_RUSTC_VERSION 2024-07-28 14:46:29 -04:00
John Arundel a19472a93e Fix doc nits
Many tiny changes to stdlib doc comments to make them consistent (for example
"Returns foo", rather than "Return foo", per RFC1574), adding missing periods, paragraph
breaks, backticks for monospace style, and other minor nits.

https://github.com/rust-lang/rfcs/blob/master/text/1574-more-api-documentation-conventions.md#appendix-a-full-conventions-text
2024-07-26 13:26:33 +01:00
Ole Bertram 7f383d098a Stabilize duration_abs_diff 2024-06-29 21:03:12 +02:00
Sky 9bbf3d9805 docs: say "includes" instead of "does include" 2024-06-28 00:01:32 -04:00
Pietro Albini be9e27e490 replace version placeholder 2024-06-11 16:52:02 +02:00
John Millikin a8234d5f87 Fix copy-paste error in Duration::from_weeks panic message. 2024-05-30 08:40:48 +09:00
Matthias Krüger 80aea305d3 Rollup merge of #124667 - newpavlov:stabilize_div_duration, r=jhpratt
Stabilize `div_duration`

Closes #63139
2024-05-25 22:15:18 +02:00
Noa 53b317710d Inline Duration construction into Duration::from_{millis,micros,nanos} 2024-05-17 18:37:59 -05:00
Noa 35522a9e09 Don't call Duration::new unnecessarily in Duration::from_secs 2024-05-17 14:26:50 -05:00
Artyom Pavlov 8da41b107d Divide float nanoseconds instead of seconds 2024-05-15 00:38:34 +03:00
Артём Павлов [Artyom Pavlov] 6d223bb6a1 Use CURRENT_RUSTC_VERSION 2024-05-03 17:42:34 +03:00
Артём Павлов [Artyom Pavlov] 1f1653c328 Stabilize div_duration 2024-05-03 17:31:55 +03:00
Jacob Trueb 3ad0618076 Fix cannot usage in time.rs 2024-04-24 17:37:44 +00:00
Pavel Grigorenko f2ec0d3d26 Implement Duration::as_millis_{f64,f32} 2024-03-14 01:37:12 +03:00
Matthias Krüger ba405a47bd Rollup merge of #120307 - djc:duration-constructors, r=Mark-Simulacrum
core: add Duration constructors

Add more `Duration` constructors.

Tracking issue: #120301.

These match similar convenience constructors available on both `chrono::Duration` and `time::Duration`.

What's the best ordering for these with respect to the existing constructors?
2024-02-11 08:25:42 +01:00
Matthias Krüger a576e81b1d Rollup merge of #119242 - BenWiederhake:dev-from-nanos, r=joshtriplett
Suggest less bug-prone construction of Duration in docs

std::time::Duration has a well-known quirk: Duration::as_nanos() returns u128 [1], but Duration::from_nanos() takes u64 [2]. So these methods cannot easily roundtrip [3]. It is not possible to simply accept u128 in from_nanos [4], because it requires breaking other API [5].

It seems to me that callers have basically only two options:
1. `Duration::from_nanos(d.as_nanos() as u64)`, which is the "obvious" and buggy approach.
2. `Duration::new(d.as_secs(), d.subsecs_nanos())`, which only becomes apparent after reading and digesting the entire Duration struct documentation.

I suggest that the documentation of `from_nanos` is changed to make option 2 more easily discoverable.

There are two major usecases for this:
- "Weird math" operations that should not be supported directly by `Duration`, like squaring.
- "Disconnected roundtrips", where the u128 value is passed through various other stack frames, and perhaps reconstructed into a Duration on a different machine.

In both cases, it seems like a good idea to not tempt people into thinking "Eh, u64 is good enough, what could possibly go wrong!". That's why I want to add a note that points out the similarly-easy and *safe* way to reconstruct a Duration.

[1] https://doc.rust-lang.org/stable/std/time/struct.Duration.html#method.as_nanos
[2] https://doc.rust-lang.org/stable/std/time/struct.Duration.html#method.from_nanos
[3] https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=fa6bab2b6b72f20c14b5243610ea1dde
[4] https://github.com/rust-lang/rust/issues/103332
[5] https://github.com/rust-lang/rust/issues/51107#issuecomment-392353166
2024-02-11 08:25:41 +01:00
Dirkjan Ochtman e077ff0eed core: add Duration constructors 2024-01-24 14:24:57 +01:00
Utkarsh Gupta 8a850cd12b std/time: avoid divisions in Duration::new 2024-01-24 11:10:14 +00:00
Ben Wiederhake 27ba1c1a8c Suggest less bug-prone construction of Duration in docs 2024-01-16 21:11:42 +01:00
Ole Bertram 0ac438c8d4 Add Duration::abs_diff 2023-11-05 19:45:17 +01:00