azul_core::task

Struct Timer

Source
pub struct Timer {
    pub created: Instant,
    pub last_run: Option<Instant>,
    pub delay: Option<Duration>,
    pub interval: Option<Duration>,
    pub timeout: Option<Duration>,
    pub callback: TimerCallback,
}
Expand description

A Timer is a function that is run on every frame.

There are often a lot of visual tasks such as animations or fetching the next frame for a GIF or video, etc. - that need to run every frame or every X milliseconds, but they aren’t heavy enough to warrant creating a thread - otherwise the framework would create too many threads, which leads to a lot of context switching and bad performance.

The callback of a Timer should be fast enough to run under 16ms, otherwise running timers will block the main UI thread.

Fields§

§created: Instant

Stores when the timer was created (usually acquired by Instant::now())

§last_run: Option<Instant>

When the timer was last called (None only when the timer hasn’t been called yet).

§delay: Option<Duration>

If the timer shouldn’t start instantly, but rather be delayed by a certain timeframe

§interval: Option<Duration>

How frequently the timer should run, i.e. set this to Some(Duration::from_millis(16)) to run the timer every 16ms. If this value is set to None, (the default), the timer will execute the timer as-fast-as-possible (i.e. at a faster framerate than the framework itself) - which might be performance intensive.

§timeout: Option<Duration>

When to stop the timer (for example, you can stop the execution after 5s using Some(Duration::from_secs(5))).

§callback: TimerCallback

Callback to be called for this timer

Implementations§

Source§

impl Timer

Source

pub fn new(callback: TimerCallbackType) -> Self

Create a new timer

Source

pub fn with_delay(self, delay: Duration) -> Self

Delays the timer to not start immediately but rather start after a certain time frame has elapsed.

Source

pub fn with_interval(self, interval: Duration) -> Self

Converts the timer into a timer, running the function only if the given Duration has elapsed since the last run

Source

pub fn with_timeout(self, timeout: Duration) -> Self

Converts the timer into a countdown, by giving it a maximum duration (counted from the creation of the Timer, not the first use).

Source

pub fn invoke<'a>(&mut self, info: TimerCallbackInfo<'a>) -> TimerCallbackReturn

Crate-internal: Invokes the timer if the timer and the self.timeout allow it to

Trait Implementations§

Source§

impl Clone for Timer

Source§

fn clone(&self) -> Timer

Returns a copy of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Timer

Source§

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

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

impl Hash for Timer

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 Timer

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Timer

Source§

impl Eq for Timer

Source§

impl StructuralPartialEq for Timer

Auto Trait Implementations§

§

impl Freeze for Timer

§

impl RefUnwindSafe for Timer

§

impl Send for Timer

§

impl Sync for Timer

§

impl Unpin for Timer

§

impl UnwindSafe for Timer

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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.

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.