relative-duration
A duration (the std style) that can retain negative values. Shipped with the standard set of operators for basic math.
use Suffix;
let a = 10.milliseconds;
let b = 20.milliseconds;
let c = a - b;
assert_eq!;
Reason
The existence of this (rather simple) crate is for calculus purposes only. Sometimes, when you work with offsets of timed events, you need to go backwards in time (shifting an event to the "left" for exemple).
when you try to do it on std durations, you get a panic.
use catch_unwind;
use Duration;
let a = from_millis;
let b = from_millis;
let res = catch_unwind;
assert!; // The thing has panicked... we lost :'(
That's because durations in the std library are meant to represent a delta between two instants, and instants are guaranteed to not go backwards.
The typical use case of this crate is when you want to do calculus with durations that can be negative without using formatting oriented types as with chrono, or resort to using bare numerical representations that take away the flexibility of the Duration type.
Features
Comparison
Relative duration objects can be compared like standard durations
use *;
let a = from_secs;
let b = from_secs;
assert!;
Basic operations
As this crate is really meant for calculus, basic operations are handled
use *;
let a = from_millis;
let b = from_millis;
let c = a + b;
let d = c - from_millis;
assert_eq!;
assert_eq!;
as well as divisions and multiplication by an unsigned integer scalar
use *;
let a = 2u32 * from_millis;
assert_eq!;
let b = from_millis / 2;
assert_eq!;
and methods that mirror those of std duration.
Duration division
It is possible to divide durations by other durations
use *;
let f1 = from_millis.div_duration_f32;
assert_eq!;
Handy things
This crate has a trait called Suffix. It helps create relative durations from a bunch of primitives (kotlin style)
use *;
let d = 10.microseconds;
assert_eq!;
exists for nanoseconds, microseconds, milliseconds and seconds.