Crates.io | easytime |
lib.rs | easytime |
version | 0.2.7 |
source | src |
created_at | 2019-02-19 23:26:11.188977 |
updated_at | 2024-03-05 14:38:28.563484 |
description | Providing wrapper types for safely performing panic-free checked arithmetic on instants and durations. |
homepage | |
repository | https://github.com/taiki-e/easytime |
max_upload_size | |
id | 115830 |
size | 84,221 |
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
Add this to your Cargo.toml
:
[dependencies]
easytime = "0.2"
Compiler support: requires rustc 1.58+
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)
}
std
(enabled by default)
easytime::Instant
.easytime
can be used in no_std
environments.Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.