Skip to main content

TaskScope

Struct TaskScope 

Source
pub struct TaskScope { /* private fields */ }
Expand description

A node in the scope tree that owns spawned tasks and carries a typed context for dependency injection.

§Drop guarantee

When a TaskScope is dropped, all descendant scopes and their tasks are cancelled iteratively using a work queue — recursion is never used, so deeply nested UI trees (200+ levels) never overflow the stack.

§Context

Use provide / consume for lightweight dependency injection that walks up the scope tree.

§Callback lifecycle

CallbackHandles registered via register_callback_handle are dropped before spawned tasks are cancelled, ensuring that signal subscriptions are removed before any task cleanup.

Implementations§

Source§

impl TaskScope

Source

pub fn new() -> Self

Create a new root scope (no parent).

Source

pub fn new_child(parent: &Self) -> Self

Create a child scope attached to self.

A weak back-reference to the parent is stored so that consume can walk up the tree.

Source

pub fn spawn(&self, future: impl Future<Output = ()> + 'static)

Spawn a future in this scope at low priority.

Source

pub fn spawn_with_priority( &self, priority: Priority, future: impl Future<Output = ()> + 'static, )

Spawn a future in this scope at the given priority.

The current scope is set to self during the spawn so that any synchronous work inside the future constructor (e.g. bind_text) can discover the owning scope via current_scope.

Source

pub fn register_callback_handle(&self, handle: CallbackHandle)

Register a CallbackHandle that will be dropped when this scope is dropped (or when clear_callbacks is called).

Used by bind_* functions to ensure signal subscriptions are cleaned up when the owning component is destroyed.

Source

pub fn provide<T: 'static>(&self, value: T)

Store a value of type T in this scope.

The value is wrapped in Rc so it can be shared. A subsequent call to consume on this scope (or any descendant) will discover it by walking up the parent chain.

Source

pub fn consume<T: 'static>(&self) -> Option<Rc<T>>

Look up a value of type T by walking up the scope tree.

Returns None if no ancestor (including self) has provided a value of this type.

Source

pub fn expect_context<T: 'static>(&self) -> Rc<T>

Like consume but panics if the value is not found.

§Panics

Panics if no ancestor scope has provided a value of type T.

Source

pub fn enter<R>(&self, f: impl FnOnce() -> R) -> R

Run f with self set as the current scope for the thread.

Used by framework glue code so bind functions can discover the owning scope via current_scope.

Source

pub fn suspend(&self)

Suspend all tasks owned by this scope and its descendants.

Suspended tasks are skipped during executor polling. Signal subscriptions remain registered but their callbacks are not invoked while the scope is suspended. Use resume to restart execution.

Used by if_async_cached and match_async_cached to pause hidden branches.

Source

pub fn resume(&self)

Resume all tasks owned by this scope and its descendants.

This reverses the effect of suspend. Tasks become eligible for polling again on the next executor flush.

Source

pub fn is_suspended(&self) -> bool

Return true if this scope is currently suspended.

Trait Implementations§

Source§

impl Clone for TaskScope

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Default for TaskScope

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Drop for TaskScope

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

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