Skip to main content

Duration

Struct Duration 

Source
pub struct Duration<R: Rate>(/* private fields */);
Expand description

A span of ticks at rate R.

The count wraps after 2^32 ticks, which is twelve days at 4096 Hz and months below that. Nothing here allows for that: a span measured across the wrap comes back wrong rather than saturated, so a machine left running that long after Timer::start reads the wrong time.

Implementations§

Source§

impl<R: Rate> Duration<R>

Source

pub const fn from_ticks(ticks: u32) -> Self

A span of ticks.

Source

pub const fn from_ms(ms: u32) -> Self

The shortest span of at least ms milliseconds.

Rounded up to a whole tick, so comparing against this is the answer to “has ms passed”. At 16 Hz a tick is 63 ms, and nothing shorter can be told apart.

Source

pub const fn ticks(self) -> u32

The span in ticks.

Source

pub fn ms(self) -> u32

The span in milliseconds, rounded down.

Do not compare against this. It multiplies, and that is not cheap enough to run each time a program asks whether a span has passed. from_ms converts at compile time instead, leaving a plain comparison on ticks:

if elapsed >= Duration::from_ms(5000) { .. }   // one comparison
if elapsed.ms() >= 5000 { .. }                 // a multiply first

This is for a number to show or to log.

Trait Implementations§

Source§

impl<R: Rate> Add for Duration<R>

Source§

fn add(self, other: Self) -> Self

The two spans one after the other, saturating rather than wrapping.

Source§

type Output = Duration<R>

The resulting type after applying the + operator.
Source§

impl<'a, R: Rate> Add<Duration<R>> for Instant<'a, R>

Source§

type Output = Instant<'a, R>

The resulting type after applying the + operator.
Source§

fn add(self, span: Duration<R>) -> Self

Performs the + operation. Read more
Source§

impl<R: Rate> Clone for Duration<R>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<R: Rate> Copy for Duration<R>

Source§

impl<R: Rate> Eq for Duration<R>

Source§

impl<R: Rate> Ord for Duration<R>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<R: Rate> PartialEq for Duration<R>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<R: Rate> PartialOrd for Duration<R>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<R: Rate> Sub for Duration<R>

Source§

fn sub(self, other: Self) -> Self

What is left of this span after other, or nothing if other is the longer of the two.

Source§

type Output = Duration<R>

The resulting type after applying the - operator.
Source§

impl<'a, R: Rate> Sub<Duration<R>> for Instant<'a, R>

Source§

type Output = Instant<'a, R>

The resulting type after applying the - operator.
Source§

fn sub(self, span: Duration<R>) -> Self

Performs the - operation. Read more

Auto Trait Implementations§

§

impl<R> BankSafe for Duration<R>
where R: BankSafe,

§

impl<R> Freeze for Duration<R>

§

impl<R> RefUnwindSafe for Duration<R>
where R: RefUnwindSafe,

§

impl<R> Send for Duration<R>
where R: Send,

§

impl<R> Sync for Duration<R>
where R: Sync,

§

impl<R> Unpin for Duration<R>
where R: Unpin,

§

impl<R> UnsafeUnpin for Duration<R>

§

impl<R> UnwindSafe for Duration<R>
where R: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CellValue for T
where T: Copy + BankSafe,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.