easytime 0.1.1

Providing wrapper types for safely performing panic-free checked arithmetic on instants and durations.
Documentation

easytime

Build Status version documentation license

Providing wrapper types for safely performing panic-free checked arithmetic on instants and durations.

This crate provides the following two data structures.

Documentation

Usage

Add this to your Cargo.toml:

[dependencies]
easytime = "0.1"

The current version of easytime requires Rust 1.34 or later.

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:

#![feature(checked_duration_since)]

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(u64::from(nanos));

    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.
    • This requires Rust 1.34 or later.
    • If disabled this feature, easytime can compile with Rust 1.33.

License

Licensed under either of

at your option.

Contribution

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.