Skip to main content

BackgroundTask

Struct BackgroundTask 

Source
pub struct BackgroundTask<T> { /* private fields */ }
Expand description

Non-blocking cross-platform background worker handle committing computed payloads onto the UI thread without stalling frames.

Implementations§

Source§

impl<T> BackgroundTask<T>

Non-blocking task spawning and lock-free completion polling for BackgroundTask<T>.

Source

pub fn ready(value: T) -> Self

Wraps an already-computed value in an immediately ready BackgroundTask<T> with zero thread spawn.

§Arguments
  • T - Precomputed payload ready for immediate consumption.
§Returns
  • Self - Immediately ready task handle.
Source

pub fn spawn<F>(work: F) -> Self
where F: FnOnce() -> T + Send + 'static, T: Send + 'static,

Executes work on a background OS thread on native targets, or lazily inline on wasm32.

§Arguments
  • F - Closure producing the background payload.
§Returns
  • Self - Non-blocking handle polled via is_ready / try_take.
Source

pub fn is_ready(&self) -> bool

Returns true when the background computation has finished (single lock-free atomic load).

§Arguments
  • &Self - Task handle reference.
§Returns
  • bool - true if the payload is ready to take.
Source

pub fn try_take(&self) -> Option<T>

Attempts to take the completed payload without blocking the UI thread.

§Arguments
  • &Self - Task handle reference.
§Returns
  • Option<T> - Completed payload once ready, or None if still running or already consumed.
Source

pub fn take_blocking(&self) -> Option<T>

Waits briefly until the background worker finishes and returns its payload.

§Arguments
  • &Self - Task handle reference.
§Returns
  • Option<T> - Completed payload, or None if already consumed.

Trait Implementations§

Source§

impl<T> Clone for BackgroundTask<T>

Cheap Arc-backed cloning for BackgroundTask<T> regardless of whether T: Clone.

Source§

fn clone(&self) -> Self

Clones the shared task handle referencing the same completion flag and result slot.

§Arguments
  • &Self - Existing background task handle.
§Returns
  • Self - Shared handle pointing to the same worker result.
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for BackgroundTask<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the struct fields as a debug struct, skipping excluded fields.

§Arguments
  • &mut ::std::fmt::Formatter<'_> - The formatter to write the representation into.
§Returns
  • ::std::fmt::Result - Success or failure of the formatting operation.

Auto Trait Implementations§

§

impl<T> Forget for BackgroundTask<T>

§

impl<T> Freeze for BackgroundTask<T>
where *const ArcInner<Atomic<bool>> is !null: Freeze, PhantomData<ArcInner<Atomic<bool>>>: Freeze, *const ArcInner<Mutex<Option<T>>> is !null: Freeze, PhantomData<ArcInner<Mutex<Option<T>>>>: Freeze,

§

impl<T> Move for BackgroundTask<T>

§

impl<T> RefUnwindSafe for BackgroundTask<T>

§

impl<T> Send for BackgroundTask<T>
where Arc<Atomic<bool>>: Send, Arc<Mutex<Option<T>>>: Send,

§

impl<T> Sync for BackgroundTask<T>
where Arc<Atomic<bool>>: Sync, Arc<Mutex<Option<T>>>: Sync,

§

impl<T> Unpin for BackgroundTask<T>

§

impl<T> UnsafeUnpin for BackgroundTask<T>
where *const ArcInner<Atomic<bool>> is !null: UnsafeUnpin, PhantomData<ArcInner<Atomic<bool>>>: UnsafeUnpin, *const ArcInner<Mutex<Option<T>>> is !null: UnsafeUnpin, PhantomData<ArcInner<Mutex<Option<T>>>>: UnsafeUnpin,

§

impl<T> UnwindSafe for BackgroundTask<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> 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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.