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
sourceimpl LoopHelper
impl LoopHelper
sourcepub fn builder() -> LoopHelperBuilder
pub fn builder() -> LoopHelperBuilder
Returns a LoopHelperBuilder
with which to build a
LoopHelper
.
sourcepub fn loop_start(&mut self) -> Duration
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
.
sourcepub fn loop_start_s(&mut self) -> Seconds
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
.
sourcepub fn loop_sleep(&mut self)
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.
sourcepub fn loop_sleep_no_spin(&mut self)
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.
sourcepub fn report_rate(&mut self) -> Option<RatePerSecond>
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
.
sourcepub fn set_target_rate<R: Into<RatePerSecond>>(&mut self, target_rate: R)
pub fn set_target_rate<R: Into<RatePerSecond>>(&mut self, target_rate: R)
Changes the target loop rate
sourcepub fn target_rate(&self) -> RatePerSecond
pub fn target_rate(&self) -> RatePerSecond
Returns the current target loop rate
Trait Implementations
sourceimpl Clone for LoopHelper
impl Clone for LoopHelper
sourcefn clone(&self) -> LoopHelper
fn clone(&self) -> LoopHelper
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for LoopHelper
impl Debug for LoopHelper
sourceimpl PartialEq<LoopHelper> for LoopHelper
impl PartialEq<LoopHelper> for LoopHelper
sourcefn eq(&self, other: &LoopHelper) -> bool
fn eq(&self, other: &LoopHelper) -> bool
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
sourcefn ne(&self, other: &LoopHelper) -> bool
fn ne(&self, other: &LoopHelper) -> bool
This method tests for !=
.
impl Eq for LoopHelper
impl StructuralEq for LoopHelper
impl StructuralPartialEq for LoopHelper
Auto Trait Implementations
impl RefUnwindSafe for LoopHelper
impl Send for LoopHelper
impl Sync for LoopHelper
impl Unpin for LoopHelper
impl UnwindSafe for LoopHelper
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more