From 172a813829505e4bc4bc62c55ca58c2a24a9f1d4 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Mon, 16 May 2022 09:25:28 +0100 Subject: [PATCH 1/2] Add const fn none() --- src/duration.rs | 5 +++++ src/instant.rs | 5 +++++ tests/duration.rs | 5 +++++ tests/instant.rs | 5 +++++ 4 files changed, 20 insertions(+) diff --git a/src/duration.rs b/src/duration.rs index e2e8559..922d402 100644 --- a/src/duration.rs +++ b/src/duration.rs @@ -47,6 +47,11 @@ impl Duration { // TODO: duration_constants https://github.com/rust-lang/rust/issues/57391 // TODO: div_duration https://github.com/rust-lang/rust/issues/63139 + /// Returns a "none" value + pub const fn none() -> Self { + Self(None) + } + /// A duration of zero time. /// /// # Examples diff --git a/src/instant.rs b/src/instant.rs index 22d6aef..09b19a6 100644 --- a/src/instant.rs +++ b/src/instant.rs @@ -60,6 +60,11 @@ use super::{pair_and_then, Duration, TryFromTimeError}; pub struct Instant(Option); impl Instant { + /// Returns a "none" value + pub const fn none() -> Self { + Self(None) + } + /// Returns an instant corresponding to "now". /// /// # Examples diff --git a/tests/duration.rs b/tests/duration.rs index 993cd08..6f10c6e 100644 --- a/tests/duration.rs +++ b/tests/duration.rs @@ -5,6 +5,11 @@ use core::time; use easytime::Duration; +#[test] +fn none() { + assert!(Duration::none().is_none()); +} + #[test] fn cmp() { assert!(Duration::from_secs(1) == Duration::from_secs(1)); diff --git a/tests/instant.rs b/tests/instant.rs index 1a0bcf0..d7d9f5e 100644 --- a/tests/instant.rs +++ b/tests/instant.rs @@ -21,6 +21,11 @@ pub mod std_tests { }}; } + #[test] + fn none() { + assert!(Instant::none().is_none()); + } + #[test] fn instant_monotonic() { let a = Instant::now(); From edbeaf6393e720d93afe69e9b203dec252ed32cd Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Fri, 20 May 2022 16:16:34 +0100 Subject: [PATCH 2/2] Use assoc. const over const method --- src/duration.rs | 4 +--- src/instant.rs | 4 +--- tests/duration.rs | 2 +- tests/instant.rs | 2 +- 4 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/duration.rs b/src/duration.rs index 922d402..6dc76f5 100644 --- a/src/duration.rs +++ b/src/duration.rs @@ -48,9 +48,7 @@ impl Duration { // TODO: div_duration https://github.com/rust-lang/rust/issues/63139 /// Returns a "none" value - pub const fn none() -> Self { - Self(None) - } + pub const NONE: Self = Self(None); /// A duration of zero time. /// diff --git a/src/instant.rs b/src/instant.rs index 09b19a6..4ff2ad4 100644 --- a/src/instant.rs +++ b/src/instant.rs @@ -61,9 +61,7 @@ pub struct Instant(Option); impl Instant { /// Returns a "none" value - pub const fn none() -> Self { - Self(None) - } + pub const NONE: Self = Self(None); /// Returns an instant corresponding to "now". /// diff --git a/tests/duration.rs b/tests/duration.rs index 6f10c6e..e79ea37 100644 --- a/tests/duration.rs +++ b/tests/duration.rs @@ -7,7 +7,7 @@ use easytime::Duration; #[test] fn none() { - assert!(Duration::none().is_none()); + assert!(Duration::NONE.is_none()); } #[test] diff --git a/tests/instant.rs b/tests/instant.rs index d7d9f5e..f77ccf5 100644 --- a/tests/instant.rs +++ b/tests/instant.rs @@ -23,7 +23,7 @@ pub mod std_tests { #[test] fn none() { - assert!(Instant::none().is_none()); + assert!(Instant::NONE.is_none()); } #[test]