Struct spin_sleep::LoopHelper

source ·
pub struct LoopHelper { /* private fields */ }
Expand description

Tool for loop rate reporting and control.

Can report mean rate per second of a loop over a configured report interval with LoopHelper::report_rate.

Can limit a loop rate to a desired target using LoopHelper::loop_sleep.

Example

use spin_sleep::LoopHelper;

let mut loop_helper = LoopHelper::builder()
    .report_interval_s(0.5) // report every half a second
    .build_with_target_rate(250.0); // limit to 250 FPS if possible

let mut current_fps = None;

loop {
    let delta = loop_helper.loop_start(); // or .loop_start_s() for f64 seconds

    // compute_something(delta);

    if let Some(fps) = loop_helper.report_rate() {
        current_fps = Some(fps.round());
    }

    // render_fps(current_fps);

    loop_helper.loop_sleep(); // sleeps to achieve a 250 FPS rate
}

Implementations§

source§

impl LoopHelper

source

pub fn builder() -> LoopHelperBuilder

Returns a LoopHelperBuilder with which to build a LoopHelper.

source

pub fn loop_start(&mut self) -> Duration

Notifies the helper that a new loop has begun. Returns the delta, the duration since the last call to loop_start or loop_start_s.

source

pub fn loop_start_s(&mut self) -> Seconds

Notifies the helper that a new loop has begun. Returns the delta, the seconds since the last call to loop_start or loop_start_s.

source

pub fn loop_sleep(&mut self)

Generally called at the end of a loop to sleep until the desired delta (configured with build_with_target_rate) has elapsed. Uses a SpinSleeper to sleep the thread to provide improved accuracy. If the delta has already elapsed this method returns immediately.

source

pub fn loop_sleep_no_spin(&mut self)

Generally called at the end of a loop to sleep until the desired delta (configured with build_with_target_rate) has elapsed. Does not use a SpinSleeper, instead directly calls thread::sleep and will never spin. This is less accurate than loop_sleep but less CPU intensive.

source

pub fn report_rate(&mut self) -> Option<RatePerSecond>

Returns the mean rate per second recorded since the last report. Returns None if the last report was within the configured report_interval.

source

pub fn set_target_rate<R: Into<RatePerSecond>>(&mut self, target_rate: R)

Changes the target loop rate

source

pub fn target_rate(&self) -> RatePerSecond

Returns the current target loop rate

Trait Implementations§

source§

impl Clone for LoopHelper

source§

fn clone(&self) -> LoopHelper

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 LoopHelper

source§

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

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

impl PartialEq<LoopHelper> for LoopHelper

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for LoopHelper

source§

impl StructuralEq for LoopHelper

source§

impl StructuralPartialEq for LoopHelper

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.