Skip to content

Commit 0bfa908

Browse files
committed
Update docs and tests based on std
1 parent 44df082 commit 0bfa908

File tree

7 files changed

+142
-12
lines changed

7 files changed

+142
-12
lines changed

.github/workflows/ci.yml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ jobs:
2222
test:
2323
strategy:
2424
matrix:
25-
rust:
26-
- stable
27-
- beta
28-
- nightly
29-
runs-on: ubuntu-latest
25+
include:
26+
- rust: stable
27+
- rust: beta
28+
- rust: nightly
29+
- rust: nightly
30+
os: macos-latest
31+
runs-on: ${{ matrix.os || 'ubuntu-latest' }}
3032
steps:
3133
- uses: actions/checkout@v2
3234
- uses: taiki-e/github-actions/install-rust@main

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@ autocfg = "1"
3030

3131
[dependencies]
3232
const_fn = "0.4"
33+
34+
[dev-dependencies]
35+
rustversion = "1"

src/duration.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ use super::{pair_and_then, TryFromTimeError};
2626
pub struct Duration(pub(crate) Option<time::Duration>);
2727

2828
impl Duration {
29+
// TODO: duration_constants https://github.com/rust-lang/rust/issues/57391
30+
// TODO: duration_zero https://github.com/rust-lang/rust/issues/73544
31+
// TODO: duration_float https://github.com/rust-lang/rust/issues/54361
32+
// TODO: div_duration https://github.com/rust-lang/rust/issues/63139
33+
2934
/// Creates a new `Duration` from the specified number of whole seconds and
3035
/// additional nanoseconds.
3136
///
@@ -149,9 +154,6 @@ impl Duration {
149154
}
150155
}
151156

152-
// TODO: duration_float https://github.com/rust-lang/rust/issues/54361
153-
// TODO: div_duration https://github.com/rust-lang/rust/issues/63139
154-
155157
// =============================================================================
156158
// Option based method implementations
157159

src/instant.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,33 @@ use super::{pair_and_then, Duration, TryFromTimeError};
2727
///
2828
/// The size of an `Instant` struct may vary depending on the target operating
2929
/// system.
30+
///
31+
/// # OS-specific behaviors
32+
///
33+
/// An `Instant` is a wrapper around system-specific types and it may behave
34+
/// differently depending on the underlying operating system. For example,
35+
/// the following snippet is fine on Linux but fails and returns `None` on macOS.
36+
/// ():
37+
///
38+
/// ```
39+
/// use easytime::{Duration, Instant};
40+
///
41+
/// let now = Instant::now();
42+
/// let max_nanoseconds = u64::MAX / 1_000_000_000;
43+
/// let duration = Duration::new(max_nanoseconds, 0);
44+
/// // Unlike `std::time::Instant`, it won't panic!
45+
/// let result = now + duration;
46+
///
47+
/// #[cfg(target_os = "linux")]
48+
/// assert!(result.is_some());
49+
/// #[cfg(target_os = "macos")]
50+
/// assert!(result.is_none());
51+
/// ```
52+
///
53+
/// # Underlying System calls
54+
///
55+
/// See the [standard library documentation](std::time::Instant#underlying-system-calls)
56+
/// for the system calls used to get the current time using `now()`.
3057
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
3158
pub struct Instant(Option<time::Instant>);
3259

src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@
5252
#![cfg_attr(not(feature = "std"), no_std)]
5353
#![doc(test(
5454
no_crate_inject,
55-
attr(deny(warnings, rust_2018_idioms, single_use_lifetimes), allow(dead_code))
55+
attr(
56+
deny(warnings, rust_2018_idioms, single_use_lifetimes),
57+
allow(dead_code, unused_variables)
58+
)
5659
))]
5760
#![forbid(unsafe_code)]
5861
#![warn(future_incompatible, rust_2018_idioms, single_use_lifetimes, unreachable_pub)]

tests/duration.rs

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#![warn(rust_2018_idioms, single_use_lifetimes)]
22
#![allow(clippy::zero_prefixed_literal)]
3+
#![allow(clippy::non_ascii_literal)]
34

4-
// https://github.com/rust-lang/rust/blob/master/src/libcore/tests/time.rs
5+
// https://github.com/rust-lang/rust/blob/1.49.0/library/core/tests/time.rs
56

67
use core::time;
78
use easytime::Duration;
@@ -294,3 +295,95 @@ fn debug_formatting_precision_high() {
294295
fn debug_formatting_none() {
295296
assert_eq!(format!("{:?}", Duration::new(0, 0) - Duration::new(0, 1)), "None");
296297
}
298+
299+
const fn duration_second() -> Duration {
300+
Duration::from_secs(1)
301+
}
302+
303+
#[rustversion::since(1.46)]
304+
#[test]
305+
fn duration_const() {
306+
// test that the methods of `Duration` are usable in a const context
307+
308+
const DURATION: Duration = Duration::from_nanos(123_456_789);
309+
310+
const SUB_SEC_MILLIS: Option<u32> = DURATION.subsec_millis();
311+
assert_eq!(SUB_SEC_MILLIS, Some(123));
312+
313+
const SUB_SEC_MICROS: Option<u32> = DURATION.subsec_micros();
314+
assert_eq!(SUB_SEC_MICROS, Some(123_456));
315+
316+
const SUB_SEC_NANOS: Option<u32> = DURATION.subsec_nanos();
317+
assert_eq!(SUB_SEC_NANOS, Some(123_456_789));
318+
319+
// const IS_ZERO: bool = Duration::ZERO.is_zero();
320+
// assert!(IS_ZERO);
321+
322+
const SECONDS: Option<u64> = duration_second().as_secs();
323+
assert_eq!(SECONDS, Some(1));
324+
325+
const FROM_SECONDS: Duration = Duration::from_secs(1);
326+
assert_eq!(FROM_SECONDS, duration_second());
327+
328+
// const SECONDS_F32: Option<f32> = duration_second().as_secs_f32();
329+
// assert_eq!(SECONDS_F32, Some(1.0));
330+
331+
// const FROM_SECONDS_F32: Duration = Duration::from_secs_f32(1.0);
332+
// assert_eq!(FROM_SECONDS_F32, duration_second);
333+
334+
// const SECONDS_F64: f64 = duration_second().as_secs_f64();
335+
// assert_eq!(SECONDS_F64, 1.0);
336+
337+
// const FROM_SECONDS_F64: Duration = Duration::from_secs_f64(1.0);
338+
// assert_eq!(FROM_SECONDS_F64, duration_second());
339+
340+
const MILLIS: Option<u128> = duration_second().as_millis();
341+
assert_eq!(MILLIS, Some(1_000));
342+
343+
const FROM_MILLIS: Duration = Duration::from_millis(1_000);
344+
assert_eq!(FROM_MILLIS, duration_second());
345+
346+
const MICROS: Option<u128> = duration_second().as_micros();
347+
assert_eq!(MICROS, Some(1_000_000));
348+
349+
const FROM_MICROS: Duration = Duration::from_micros(1_000_000);
350+
assert_eq!(FROM_MICROS, duration_second());
351+
352+
const NANOS: Option<u128> = duration_second().as_nanos();
353+
assert_eq!(NANOS, Some(1_000_000_000));
354+
355+
const FROM_NANOS: Duration = Duration::from_nanos(1_000_000_000);
356+
assert_eq!(FROM_NANOS, duration_second());
357+
358+
// const MAX: Duration = Duration::new(u64::MAX, 999_999_999);
359+
360+
// const ADD: Duration = MAX + duration_second();
361+
// assert_eq!(ADD.into_inner(), None);
362+
363+
// const SUB: Duration = Duration::ZERO - duration_second();
364+
// assert_eq!(SUB.into_inner(), None);
365+
366+
// const MUL: Duration = duration_second() * 1;
367+
// assert_eq!(MUL, duration_second());
368+
369+
// const MUL_F32: Duration = duration_second().mul_f32(1.0);
370+
// assert_eq!(MUL_F32, duration_second());
371+
372+
// const MUL_F64: Duration = duration_second().mul_f64(1.0);
373+
// assert_eq!(MUL_F64, duration_second());
374+
375+
// const DIV: Duration = duration_second() / 1;
376+
// assert_eq!(DIV, duration_second());
377+
378+
// const DIV_F32: Duration = duration_second().div_f32(1.0);
379+
// assert_eq!(DIV_F32, duration_second());
380+
381+
// const DIV_F64: Duration = duration_second().div_f64(1.0);
382+
// assert_eq!(DIV_F64, duration_second());
383+
384+
// const DIV_DURATION_F32: f32 = duration_second().div_duration_f32(duration_second());
385+
// assert_eq!(DIV_DURATION_F32, 1.0);
386+
387+
// const DIV_DURATION_F64: f64 = duration_second().div_duration_f64(duration_second());
388+
// assert_eq!(DIV_DURATION_F64, 1.0);
389+
}

tests/instant.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![warn(rust_2018_idioms, single_use_lifetimes)]
33
#![allow(clippy::eq_op)]
44

5-
// https://github.com/rust-lang/rust/blob/master/src/libstd/time.rs
5+
// https://github.com/rust-lang/rust/blob/1.49.0/library/std/src/time/tests.rs
66

77
use easytime::{Duration, Instant};
88

@@ -11,7 +11,7 @@ macro_rules! assert_almost_eq {
1111
let (a, b) = ($a, $b);
1212
if a != b {
1313
let (a, b) = if a > b { (a, b) } else { (b, a) };
14-
assert!(a - Duration::new(0, 1000) <= b, "{:?} is not almost equal to {:?}", a, b);
14+
assert!(a - Duration::from_micros(1) <= b, "{:?} is not almost equal to {:?}", a, b);
1515
}
1616
}};
1717
}

0 commit comments

Comments
 (0)