Struct interruptible_polling::PollingTask

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

General purpose RAII polling task that executes a closure with a given frequency.

When PollingTask is dropped, the background thread is signaled to perform a clean exit at the first available opportunity. If the thread is currently sleeping, this will occur almost immediately. If the closure is still running, it will happen immediately after the closure finishes. The task joins on the background thread as a best-effort clean exit.

Note nothing special is done to try and keep the thread alive longer. If you terminate the program the default behavior of reaping the thread mid-execution will still occur.

Implementations§

source§

impl PollingTask

source

pub fn new<F>(interval: Duration, task: F) -> Result<Self, TryFromIntError>
where F: Fn() + Send + 'static,

Creates a new background thread that immediately executes the given task.

  • interval The interval to poll at. Note it must be expressible as an u64 in milliseconds.
  • task The closure to execute at every poll.
source

pub fn new_with_checker<F>( interval: Duration, task: F ) -> Result<Self, TryFromIntError>
where F: Fn(&dyn Fn() -> bool) + Send + 'static,

Creates a new background thread that immediately executes the given task.

  • interval The interval to poll at. Note it must be expressible as an u64 in milliseconds.
  • task The closure to execute at every poll. This closure gets access to another function that can assert if the managed task is still active.

If your task is long-running or has iterations (say updating 10 cache entries sequentially), you can assert if the managed task is active to early exit during a clean exit.

source

pub fn set_polling_rate( &self, interval: Duration ) -> Result<(), TryFromIntError>

Update the delay between poll events. Applied on the next iteration.

  • interval The interval to poll at. Note it must be expressible as an u64 in milliseconds.

Auto Trait Implementations§

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

§

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

§

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.