Struct WorkerThread

Source
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>
where ThreadState: Default + Send + 'static, Result: Send + 'static,

Source

pub fn spawn() -> Self

Spawns a new worker thread. The state will be initialized with Default::default().

Source§

impl<ThreadState, Result> WorkerThread<ThreadState, Result>
where ThreadState: Send + 'static, Result: Send + 'static,

Source

pub fn spawn_with(data: ThreadState) -> Self

Spawns a new worker thread. The state will be initialized with data.

Source

pub async fn work_on<F>(&self, func: F) -> Result
where F: FnOnce(&mut ThreadState) -> Result + Send + 'static,

Pass a synchronous function, so the worker thread can execute it. Execution will be begin even before the first call to poll.

Source

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§

Source§

impl<ThreadState, Result> Clone for WorkerThread<ThreadState, Result>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

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> 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.