BackgroundRunner

Struct BackgroundRunner 

Source
pub struct BackgroundRunner<T> { /* private fields */ }
Expand description

Runner for a background task to be invoked periodically.

Implementations§

Source§

impl<T: 'static + Send> BackgroundRunner<T>

Source

pub fn new(task: impl 'static + Send + FnMut(&T)) -> Self
where T: Default,

Create a new background runner with the given task

This spawns a new thread to execute the task periodically, triggered by the Self::update() method.

Examples found in repository?
examples/iter.rs (lines 10-15)
6fn main() {
7    let mut runner_iter = 0;
8
9    // Set up the runner, giving it the task to run
10    let runner = BackgroundRunner::new(move |iter| {
11        // Simulate some heavy work
12        sleep(Duration::from_millis(10));
13        println!("runner_iter = {runner_iter}, iter = {iter}");
14        runner_iter += 1;
15    });
16
17    let start = Instant::now();
18    let mut iter = 0;
19    while start.elapsed().as_millis() < 100 {
20        // Update the runner with the current loop iteration
21        runner.update(&iter);
22        iter += 1;
23    }
24}
Source

pub fn with_init_data( init_data: T, task: impl 'static + Send + FnMut(&T), ) -> Self

Create a new background runner with the given initial data and task

This spawns a new thread to execute the task periodically, triggered by the Self::update() method.

Source

pub fn update(&self, new_data: &T)
where T: Clone,

Trigger the runner’s task if it’s not currently running.

This will never block the current thread. If the task is currently running and thus not able to process this update request, nothing will be done and this method returns immediately.

T::clone_from() is invoked to update the data that is passed to the task.

Examples found in repository?
examples/iter.rs (line 21)
6fn main() {
7    let mut runner_iter = 0;
8
9    // Set up the runner, giving it the task to run
10    let runner = BackgroundRunner::new(move |iter| {
11        // Simulate some heavy work
12        sleep(Duration::from_millis(10));
13        println!("runner_iter = {runner_iter}, iter = {iter}");
14        runner_iter += 1;
15    });
16
17    let start = Instant::now();
18    let mut iter = 0;
19    while start.elapsed().as_millis() < 100 {
20        // Update the runner with the current loop iteration
21        runner.update(&iter);
22        iter += 1;
23    }
24}
Source

pub fn update_with(&self, f: impl FnOnce(&mut T))

Trigger the runner’s task if it’s not currently running.

This will never block the current thread. If the task is currently running and thus not able to process this update request, nothing will be done and this method returns immediately.

The given closure is invoked with a mutable reference to the currently stored data to allow for its modification

Trait Implementations§

Source§

impl<T: Debug> Debug for BackgroundRunner<T>

Source§

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

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

impl<T> Drop for BackgroundRunner<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for BackgroundRunner<T>

§

impl<T> RefUnwindSafe for BackgroundRunner<T>

§

impl<T> Send for BackgroundRunner<T>
where T: Send,

§

impl<T> Sync for BackgroundRunner<T>
where T: Send,

§

impl<T> Unpin for BackgroundRunner<T>

§

impl<T> UnwindSafe for BackgroundRunner<T>

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> 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, 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.