pub struct WorkerThread<ThreadState, Result> { /* private fields */ }
Expand description
Abstracts a stateful background worker thread, that can run synchronous functions and provide the return value as asynchronous future.
The thread owns an arbitrary ThreadState
, which will be passed as
a mutable reference to the called function.
Thus the asynchronous code can easily access and mutate state when
necessary.
The worker will only process a single function at a time.
Implementations§
Source§impl<ThreadState, Result> WorkerThread<ThreadState, Result>
impl<ThreadState, Result> WorkerThread<ThreadState, Result>
Sourcepub fn spawn() -> Self
pub fn spawn() -> Self
Spawns a new worker thread. The state will be initialized with
Default::default()
.
Source§impl<ThreadState, Result> WorkerThread<ThreadState, Result>
impl<ThreadState, Result> WorkerThread<ThreadState, Result>
Sourcepub fn spawn_with(data: ThreadState) -> Self
pub fn spawn_with(data: ThreadState) -> Self
Spawns a new worker thread. The state will be initialized with
data
.
Sourcepub async fn work_on<F>(&self, func: F) -> Result
pub async fn work_on<F>(&self, func: F) -> Result
Pass a synchronous function, so the worker thread can execute it. Execution will be begin even before the first call to poll.
Sourcepub async fn work_on_boxed(
&self,
func: Box<dyn FnOnce(&mut ThreadState) -> Result + Send + 'static>,
) -> Result
pub async fn work_on_boxed( &self, func: Box<dyn FnOnce(&mut ThreadState) -> Result + Send + 'static>, ) -> Result
Like work_on
but for functions that are already boxed.
Trait Implementations§
Auto Trait Implementations§
impl<ThreadState, Result> Freeze for WorkerThread<ThreadState, Result>
impl<ThreadState, Result> RefUnwindSafe for WorkerThread<ThreadState, Result>
impl<ThreadState, Result> Send for WorkerThread<ThreadState, Result>where
Result: Send,
impl<ThreadState, Result> Sync for WorkerThread<ThreadState, Result>where
Result: Send,
impl<ThreadState, Result> Unpin for WorkerThread<ThreadState, Result>
impl<ThreadState, Result> UnwindSafe for WorkerThread<ThreadState, Result>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more