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.

Also see Self::update_with() for details.

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, this update is dicarded and this method returns immediately without any effect (also f is not invoked in this case).

The given closure is invoked with a mutable reference to the currently stored data to allow for its modification (e.g. via clone_from).

Source

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

Trigger the runner’s task, blocking until it can be executed.

Also see Self::wait_and_update_with() for details.

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

Source

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

Trigger the runner’s task, blocking until it can be executed.

Unlike Self::update_with(), this method will block the current thread until the new_data can be handed over to the background task. Thus, it is ensured that the task will see this or a later submitted datum, given that is does not panic and the program is not terminated.

This function is particularly useful in conjunction with Self::join() to ensure that the last update is processed completely.

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

Source

pub fn join(self)

Wait for the background thread to finish.

This function will wait until the background thread has finished executing it’s last task and exited. This is useful to ensure that the last task has completed, in particular, before terminating the program.

Notice, if the program is terminating, without calling this method, the background thread might be terminated abruptly, possibly in the middle of executing the task.

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.