pub struct Instant { /* private fields */ }Expand description
Exact, non-negative point in time held as a reduced rational number of seconds.
Clock conversions need a time base that is free of floating-point rounding,
so an Instant stores a numerator and denominator (both in seconds) and
keeps them in lowest terms with a positive denominator. Stream instants are
always non-negative; constructing a negative value fails closed.
§Examples
use sim_lib_stream_clock::Instant;
let half = Instant::new(1, 2)?;
let one = Instant::seconds(1);
let sum = one.checked_add(half)?;
assert_eq!(sum.numerator(), 3);
assert_eq!(sum.denominator(), 2);Implementations§
Source§impl Instant
impl Instant
Sourcepub fn new(numerator: i128, denominator: i128) -> Result<Self>
pub fn new(numerator: i128, denominator: i128) -> Result<Self>
Builds an instant of numerator / denominator seconds, reduced to
lowest terms with a positive denominator.
Returns an error when denominator is zero, when the reduced value
would be negative, or when normalizing the sign overflows i128.
Sourcepub fn denominator(self) -> i128
pub fn denominator(self) -> i128
Returns the (always positive) denominator of the reduced seconds fraction.
Sourcepub fn checked_add(self, other: Self) -> Result<Self>
pub fn checked_add(self, other: Self) -> Result<Self>
Returns the sum of self and other, reduced to lowest terms.
Returns an error when the cross-multiplied addition overflows i128.
Sourcepub fn checked_sub(self, other: Self) -> Result<Self>
pub fn checked_sub(self, other: Self) -> Result<Self>
Returns the difference self - other, reduced to lowest terms.
Returns an error when the subtraction overflows i128 or when the
result would be negative (stream instants are non-negative).