Skip to content

Commit cc1ff54

Browse files
bors[bot]dhardy
andauthored
Merge #27
27: Add const fn none() r=taiki-e a=dhardy Both `Duration` and `Instant` are explicitly optional types, so why not add this? Alternative: add `NONE` as an associated constant. Simpler, but less flexible in case the internal representation changes. Workaround: both support `From<Option<..>>` already. Co-authored-by: Diggory Hardy <git@dhardy.name>
2 parents 33f140e + edbeaf6 commit cc1ff54

File tree

4 files changed

+16
-0
lines changed

4 files changed

+16
-0
lines changed

src/duration.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ impl Duration {
4747
// TODO: duration_constants https://github.com/rust-lang/rust/issues/57391
4848
// TODO: div_duration https://github.com/rust-lang/rust/issues/63139
4949

50+
/// Returns a "none" value
51+
pub const NONE: Self = Self(None);
52+
5053
/// A duration of zero time.
5154
///
5255
/// # Examples

src/instant.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ use super::{pair_and_then, Duration, TryFromTimeError};
6060
pub struct Instant(Option<time::Instant>);
6161

6262
impl Instant {
63+
/// Returns a "none" value
64+
pub const NONE: Self = Self(None);
65+
6366
/// Returns an instant corresponding to "now".
6467
///
6568
/// # Examples

tests/duration.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ use core::time;
55

66
use easytime::Duration;
77

8+
#[test]
9+
fn none() {
10+
assert!(Duration::NONE.is_none());
11+
}
12+
813
#[test]
914
fn cmp() {
1015
assert!(Duration::from_secs(1) == Duration::from_secs(1));

tests/instant.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ pub mod std_tests {
2121
}};
2222
}
2323

24+
#[test]
25+
fn none() {
26+
assert!(Instant::NONE.is_none());
27+
}
28+
2429
#[test]
2530
fn instant_monotonic() {
2631
let a = Instant::now();

0 commit comments

Comments
 (0)