Skip to content

Commit 2bf7208

Browse files
committed
refactor(core): use #![feature(const_result_drop)]
This unstable library feature, which covers the `const fn` version of `Result::{ok, error, and, or, unwrap_or}`, was added by [rust-lang/rust#92385][1]. [1]: rust-lang/rust#92385
1 parent 03f5425 commit 2bf7208

File tree

3 files changed

+6
-26
lines changed

3 files changed

+6
-26
lines changed

doc/toolchain_limitations.md

-14
Original file line numberDiff line numberDiff line change
@@ -436,20 +436,6 @@ const _: () = Ok::<(), ()>(()).expect("");
436436
```
437437

438438

439-
### `[tag:const_result_ok]` `Result::ok` is not `const fn`
440-
441-
*Upstream PR:* [rust-lang/rust#92385](https://github.com/rust-lang/rust/pull/92385) will unstably add this
442-
443-
```rust
444-
Ok::<(), ()>(()).ok();
445-
```
446-
447-
```rust,compile_fail,E0015
448-
// error[E0015]: cannot call non-const fn `Result::<(), ()>::ok` in constants
449-
const _: () = { Ok::<(), ()>(()).ok(); };
450-
```
451-
452-
453439
### `[tag:const_result_map]` `Result::map[_err]` is not `const fn`
454440

455441
```rust

src/r3_core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#![feature(const_refs_to_cell)]
2525
#![feature(maybe_uninit_slice)]
2626
#![feature(const_nonnull_new)]
27+
#![feature(const_result_drop)]
2728
#![feature(const_impl_trait)]
2829
#![feature(const_option_ext)]
2930
#![feature(const_ptr_offset)]

src/r3_core/src/time/time.rs

+5-12
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,11 @@ impl Time {
140140
/// `None` if the result overflows the representable range of `Duration`.
141141
#[inline]
142142
pub const fn duration_since(self, reference: Self) -> Option<Duration> {
143-
Some(Duration::from_micros(result_ok(
144-
(self.micros as i128 - reference.micros as i128).try_into(),
145-
)?))
143+
Some(Duration::from_micros(
144+
(self.micros as i128 - reference.micros as i128)
145+
.try_into()
146+
.ok()?,
147+
))
146148
}
147149

148150
/// Advance the time by `duration` and return the result.
@@ -278,12 +280,3 @@ impl TryFrom<Time> for chrono_0p4::DateTime<chrono_0p4::Utc> {
278280
}
279281

280282
// TODO: Add more tests
281-
282-
/// Polyfill for `[ref:const_result_ok]`
283-
#[inline]
284-
const fn result_ok<T, E: ~const Drop>(x: Result<T, E>) -> Option<T> {
285-
match x {
286-
Ok(x) => Some(x),
287-
Err(_x) => None,
288-
}
289-
}

0 commit comments

Comments
 (0)