Expand description
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 forstd::time::Instant -
easytime::Duration– A wrapper type forstd::time::Duration
Examples
use std::time::Duration as StdDuration;
use easytime::{Duration, Instant};
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,
easytimecan be used inno_stdenvironments.
- Enable to use
Structs
A Duration type to represent a span of time, typically used for system
timeouts.
Instant
stdA measurement of a monotonically nondecreasing clock.
Opaque and useful only with Duration.
The error type returned when a conversion from easytime types to std::time types fails.