pub struct WorkerThread<T: Send + 'static> {
    worker: Option<(Event, JoinHandle<T>)>,
}
Expand description

Wrapper object for creating a worker thread that can be stopped by signaling an event.

Fields§

§worker: Option<(Event, JoinHandle<T>)>

Implementations§

source§

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

source

pub fn start<F>(thread_name: impl Into<String>, thread_func: F) -> Self
where F: FnOnce(Event) -> T + Send + 'static,

Starts a worker thread named thread_name running the thread_func function.

The thread_func implementation must monitor the provided Event and return from the thread when it is signaled.

Call stop() to stop the thread.

source

pub fn stop(self) -> T

Stops the worker thread.

Returns the value returned by the function running in the thread.

source

pub fn signal(&mut self) -> Result<(), Error>

Signal thread’s stop event. Unlike stop, the function doesn’t wait on joining the thread. The function can be called multiple times. Calling stop or drop will internally signal the stop event again and join the thread.

source

pub fn thread(&self) -> &Thread

Returns a handle to the running thread.

Trait Implementations§

source§

impl<T: Send + 'static> Drop for WorkerThread<T>

source§

fn drop(&mut self)

Stops the thread if the WorkerThread is dropped without calling stop().

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for WorkerThread<T>

§

impl<T> Send for WorkerThread<T>

§

impl<T> Sync for WorkerThread<T>

§

impl<T> Unpin for WorkerThread<T>

§

impl<T> !UnwindSafe for WorkerThread<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>,

§

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.