pub struct Instant { /* private fields */ }full only.Expand description
A measurement of the current system time.
Opaque and useful only with [Duration].
Instants are opaque types that should usually only be compared to one another.
[get()] can be used to get the internal value, but no guarantees are made about
the meaning of the value. The meaning of the result of get() varies by system
and may even change at runtime during a single boot. A best-attempt value of the
time since system boot can be retrieved with [nanos_since_boot()].
The size of an Instant struct may vary depending on the target platform.
Example:
use core::time::Duration;
use kernel_api::time::Instant;
use kernel_api::threading::sleep;
use kernel_api::executor::block_on;
fn main() {
let now = Instant::now();
// we sleep for 2 seconds
block_on(sleep(Duration::new(2, 0)));
// it prints '2'
sprintln!("{}", now.elapsed().as_secs());
}§Underlying system
The following mechanisms are currently being used by now() to find out
the current time:
| Platform | Mechanism |
|---|---|
x86_64 | rdtsc instruction |
Note: mathematical operations like
add()may panic if the underlying structure cannot represent the new point in time.
Implementations§
Source§impl Instant
impl Instant
Sourcepub fn duration_since(&self, earlier: Self) -> Duration
pub fn duration_since(&self, earlier: Self) -> Duration
Returns the amount of time elapsed from another instant to this one.
§Panics
This method may panic if self is earlier than earlier.
§Examples
use core::time::Duration;
use kernel_api::time::Instant;
use kernel_api::threading::sleep;
use kernel_api::executor::block_on;
let now = Instant::now();
block_on(sleep(Duration::new(1, 0)));
let new_now = Instant::now();
sprintln!("{:?}", new_now.duration_since(now));Sourcepub fn checked_duration_since(&self, earlier: Self) -> Option<Duration>
pub fn checked_duration_since(&self, earlier: Self) -> Option<Duration>
Returns the amount of time elapsed from another instant to this one, or None if that instant is later than this one.
§Examples
use core::time::Duration;
use kernel_api::time::Instant;
use kernel_api::threading::sleep;
use kernel_api::executor::block_on;
let now = Instant::now();
block_on(sleep(Duration::new(1, 0)));
let new_now = Instant::now();
sprintln!("{:?}", new_now.checked_duration_since(now));
sprintln!("{:?}", now.checked_duration_since(new_now)); // NoneSourcepub fn saturating_duration_since(&self, earlier: Self) -> Duration
pub fn saturating_duration_since(&self, earlier: Self) -> Duration
Returns the amount of time elapsed from another instant to this one, or zero duration if that instant is later than this one.
§Examples
use core::time::Duration;
use kernel_api::time::Instant;
use kernel_api::threading::sleep;
use kernel_api::executor::block_on;
let now = Instant::now();
block_on(sleep(Duration::new(1, 0)));
let new_now = Instant::now();
sprintln!("{:?}", new_now.saturating_duration_since(now));
sprintln!("{:?}", now.saturating_duration_since(new_now)); // 0nsSourcepub fn elapsed(&self) -> Duration
pub fn elapsed(&self) -> Duration
Returns the amount of time elapsed since this instant.
§Panics
This method may panic if the current time is earlier than self.
§Examples
use core::time::Duration;
use kernel_api::time::Instant;
use kernel_api::threading::sleep;
use kernel_api::executor::block_on;
let instant = Instant::now();
let three_secs = Duration::from_secs(3);
block_on(sleep(three_secs));
assert!(instant.elapsed() >= three_secs);Sourcepub fn checked_add(&self, duration: Duration) -> Option<Self>
pub fn checked_add(&self, duration: Duration) -> Option<Self>
Returns Some(t) where t is the time self + duration if t can be represented as
Instant (which means it’s inside the bounds of the underlying data structure), None
otherwise.
Sourcepub fn checked_sub(&self, duration: Duration) -> Option<Self>
pub fn checked_sub(&self, duration: Duration) -> Option<Self>
Returns Some(t) where t is the time self - duration if t can be represented as
Instant (which means it’s inside the bounds of the underlying data structure), None
otherwise.
Sourcepub fn nanos_since_boot(&self) -> u128
pub fn nanos_since_boot(&self) -> u128
The number of nanoseconds since the system booted.
No guarantees are provided on time before overflow.
Sourcepub const fn get(&self) -> u128
pub const fn get(&self) -> u128
Returns the opaque numeric value of the instant.
The meaning of the returned value is architecture dependant, see the struct level documentation for more information.
Trait Implementations§
Source§impl Add<Duration> for Instant
impl Add<Duration> for Instant
Source§fn add(self, rhs: Duration) -> Self::Output
fn add(self, rhs: Duration) -> Self::Output
§Panics
This function may panic if the resulting point in time cannot be represented by the
underlying data structure. See Instant::checked_add for a version without panic.
Source§impl AddAssign<Duration> for Instant
impl AddAssign<Duration> for Instant
Source§fn add_assign(&mut self, rhs: Duration)
fn add_assign(&mut self, rhs: Duration)
+= operation. Read moreimpl Copy for Instant
impl Eq for Instant
Source§impl Ord for Instant
impl Ord for Instant
1.21.0 (const: unstable)§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 (const: unstable)§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialOrd for Instant
impl PartialOrd for Instant
impl StructuralPartialEq for Instant
Auto Trait Implementations§
impl Freeze for Instant
impl RefUnwindSafe for Instant
impl Send for Instant
impl Sync for Instant
impl Unpin for Instant
impl UnsafeUnpin for Instant
impl UnwindSafe for Instant
Blanket Implementations§
§impl<T> Any for Twhere
T: 'static + ?Sized,
impl<T> Any for Twhere
T: 'static + ?Sized,
§impl<T> Borrow<T> for Twhere
T: ?Sized,
impl<T> Borrow<T> for Twhere
T: ?Sized,
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)