1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// SPDX-License-Identifier: Apache-2.0 OR MIT
/*!
<!-- Note: Document from sync-markdown-to-rustdoc:start through sync-markdown-to-rustdoc:end
is synchronized from README.md. Any changes to that range are not preserved. -->
<!-- tidy:sync-markdown-to-rustdoc:start -->
Providing wrapper types for safely performing panic-free checked arithmetic
on instants and durations.
This crate provides the following two data structures.
- [`easytime::Instant`] -- A wrapper type for [`std::time::Instant`]
- [`easytime::Duration`] -- A wrapper type for [`std::time::Duration`]
## Usage
Add this to your `Cargo.toml`:
```toml
[dependencies]
easytime = "0.2"
```
## Examples
```
use easytime::{Duration, Instant};
use std::time::Duration as StdDuration;
fn foo(secs: u64, nanos: u32, instant: Instant) -> Option<StdDuration> {
let now = Instant::now();
let dur = Duration::new(secs, nanos);
(now - instant - dur).into_inner()
}
```
If you use [`std::time`] directly, you need to write as follows:
```
use std::time::{Duration, Instant};
fn foo(secs: u64, nanos: u32, instant: Instant) -> Option<Duration> {
let now = Instant::now();
let secs = Duration::from_secs(secs);
let nanos = Duration::from_nanos(nanos as u64);
let dur = secs.checked_add(nanos)?;
now.checked_duration_since(instant)?.checked_sub(dur)
}
```
## Optional features
- **`std`** *(enabled by default)*
- Enable to use [`easytime::Instant`].
- If disabled this feature, `easytime` can be used in `no_std` environments.
<!-- tidy:sync-markdown-to-rustdoc:end -->
*/
// docs.rs only (cfg is enabled by docs.rs, not build script)
extern crate self as easytime;
extern crate std;
pub use Duration;
pub use Instant;
pub use TryFromTimeError;