Skip to main content

Countdown

Struct Countdown 

Source
pub struct Countdown { /* private fields */ }
Expand description

A timer that fires once, after length steps or turns, and waits to be restarted.

It is the cooldown too. Advance it by 1 each step or turn, and:

use shuyi_core::timer::Countdown;

// Ready for its first use, then two turns' wait.
let mut cooldown = Countdown::new_ready(2);
let mut uses = 0;
for used in [true, true, false, true] {
    if used && cooldown.ready() {
        uses += 1;
        cooldown.restart();
    }
    cooldown.advance(1);
}
assert_eq!(uses, 2);

The default has a length of 0 and is ready.

Implementations§

Source§

impl Countdown

Source

pub const fn new(length: u32) -> Self

One started: it fires after length.

Source

pub const fn new_ready(length: u32) -> Self

One done already, for a cooldown whose first use goes through.

Source

pub fn advance(&mut self, n: u32) -> bool

Takes n off what is left, stopping at 0. true only on the call that takes it from above 0 to 0, so it fires once however far n overshoots, and advance(0) never fires. A length of 0 is ready at once and never fires.

Source

pub const fn ready(&self) -> bool

Whether nothing is left.

Source

pub const fn remaining(&self) -> u32

What is left of it.

Source

pub const fn elapsed(&self) -> u32

How much of it has passed, for a bar that fills: its length less what is left, stopping at 0.

Source

pub const fn length(&self) -> u32

The whole of it.

Source

pub const fn restart(&mut self)

Starts it again from its length.

Trait Implementations§

Source§

impl Clone for Countdown

Source§

fn clone(&self) -> Countdown

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 Copy for Countdown

Source§

impl Debug for Countdown

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Countdown

Source§

fn default() -> Countdown

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Countdown

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Eq for Countdown

Source§

impl Hash for Countdown

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Countdown

Source§

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

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

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

Inequality operator !=. Read more
Source§

impl Serialize for Countdown

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Countdown

Auto Trait Implementations§

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
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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

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

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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.

Source§

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

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.