/// Sealed trait to prevent downstream implementations. mod sealed { /// A trait that cannot be implemented by downstream users. pubtrait Sealed: Sized {} impl Sealed for std::time::Instant {}
}
/// An extension trait for [`std::time::Instant`] that adds methods for /// [`time::Duration`](Duration)s. pubtrait InstantExt: sealed::Sealed { /// # Panics /// /// This function may panic if the resulting point in time cannot be represented by the /// underlying data structure. See [`InstantExt::checked_add_signed`] for a non-panicking /// version. fn add_signed(self, duration: Duration) -> Self { self.checked_add_signed(duration)
.expect("overflow when adding duration to instant")
}
/// # Panics /// /// This function may panic if the resulting point in time cannot be represented by the /// underlying data structure. See [`InstantExt::checked_sub_signed`] for a non-panicking /// version. fn sub_signed(self, duration: Duration) -> Self { self.checked_sub_signed(duration)
.expect("overflow when subtracting duration from instant")
}
/// Returns `Some(t)` where `t` is the time `self.checked_add_signed(duration)` if `t` can be /// represented as `Instant` (which means it's inside the bounds of the underlying data /// structure), `None` otherwise. fn checked_add_signed(&self, duration: Duration) -> Option<Self>;
/// Returns `Some(t)` where `t` is the time `self.checked_sub_signed(duration)` if `t` can be /// represented as `Instant` (which means it's inside the bounds of the underlying data /// structure), `None` otherwise. fn checked_sub_signed(&self, duration: Duration) -> Option<Self>;
/// Returns the amount of time elapsed from another instant to this one. This will be negative /// if `earlier` is later than `self`. /// /// # Example /// /// ```rust /// # use std::thread::sleep; /// # use std::time::{Duration, Instant}; /// # use time::ext::InstantExt; /// let now = Instant::now(); /// sleep(Duration::new(1, 0)); /// let new_now = Instant::now(); /// println!("{:?}", new_now.signed_duration_since(now)); // positive /// println!("{:?}", now.signed_duration_since(new_now)); // negative /// ``` fn signed_duration_since(&self, earlier: Self) -> Duration;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.