pub struct Task<T>(/* private fields */);Expand description
A handle to a future that runs in the background, and to its output.
Executors return a Task from their spawn methods. gpui::Task is this
type. The handle controls the future’s lifetime in three ways:
- Dropping the task cancels the future. It stops at its current
.awaitpoint and is never polled again. Work it already finished stays finished. Keep the handle alive while you need the result. Task::detachgives up the handle and lets the future run to completion. Its output is discarded, so use this for side effects.- Awaiting the task waits for the output. A
TaskimplementsFuture, and awaiting it consumes it. If the future panics, the panic resumes in the awaiting code.
Use Task::ready for a value you already have. Use Task::fallible
when a cancelled task should resolve to None instead of panicking.
§Panics
Awaiting a task panics if the task closed without producing an output. This
happens when the scheduler drops the runnable before it runs, as during
shutdown. Use Task::fallible to get None in that case.
For the bigger picture, see gpui::_documentation::concepts::executors.
Implementations§
Source§impl<T> Task<T>
impl<T> Task<T>
Sourcepub fn ready(val: T) -> Self
pub fn ready(val: T) -> Self
Returns a task that is already complete and resolves to val.
Use it where a function returns a Task but the value is known now,
such as a cache hit. No work is scheduled. Dropping or detaching the
task has no effect.
§Examples
let task = Task::ready(42);
assert!(task.is_ready());Sourcepub fn from_async_task(task: Task<T, RunnableMeta>) -> Self
pub fn from_async_task(task: Task<T, RunnableMeta>) -> Self
Wraps an async_task::Task that carries RunnableMeta as its metadata.
The executors use this internally. Call it only when you build the
async_task runnable yourself.
Sourcepub fn is_ready(&self) -> bool
pub fn is_ready(&self) -> bool
Returns true if the output is ready to take without waiting.
A Task::ready task is always ready. A spawned task becomes ready
when its future finishes, even if the output hasn’t been awaited yet.
This is a snapshot: a task on another thread may finish right after the
call returns false.
§Examples
assert!(Task::ready("done").is_ready());Sourcepub fn detach(self)
pub fn detach(self)
Lets the future run to completion in the background and discards its output.
Use this for work whose result you don’t need, such as saving a file or
sending a message. Dropping the task would cancel it instead. Detaching
a Task::ready task does nothing.
Sourcepub fn fallible(self) -> FallibleTask<T> ⓘ
pub fn fallible(self) -> FallibleTask<T> ⓘ
Converts this task into a FallibleTask, which resolves to None
instead of panicking when the task closes without an output.
A panic inside the future still propagates to the awaiter.
Source§impl Task<Box<dyn Any + Send + Sync>>
impl Task<Box<dyn Any + Send + Sync>>
Sourcepub fn downcast<T: Send + Sync + 'static>(self) -> Task<T> ⓘ
pub fn downcast<T: Send + Sync + 'static>(self) -> Task<T> ⓘ
Converts a task that yields a boxed Any into a task that yields T.
LocalExecutor::spawn_dedicated and BackgroundExecutor::spawn_dedicated
use this to recover the closure’s output type from the type-erased
Scheduler::spawn_dedicated.
§Panics
Polling the returned task panics if the boxed value is not a T. The
check runs on poll, not in this call. A mismatch is a bug in whatever
produced the inner task, because the caller chose T.
Trait Implementations§
Auto Trait Implementations§
impl<T> !RefUnwindSafe for Task<T>
impl<T> !UnwindSafe for Task<T>
impl<T> Freeze for Task<T>where
TaskState<T>: Freeze,
impl<T> Send for Task<T>where
TaskState<T>: Send,
impl<T> Sync for Task<T>where
TaskState<T>: Sync,
impl<T> Unpin for Task<T>where
TaskState<T>: Unpin,
impl<T> UnsafeUnpin for Task<T>where
TaskState<T>: UnsafeUnpin,
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn map<U, F>(self, f: F) -> Map<Self, F> ⓘ
fn map<U, F>(self, f: F) -> Map<Self, F> ⓘ
Source§fn map_into<U>(self) -> MapInto<Self, U> ⓘ
fn map_into<U>(self) -> MapInto<Self, U> ⓘ
Source§fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F> ⓘ
fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F> ⓘ
f. Read moreSource§fn left_future<B>(self) -> Either<Self, B> ⓘ
fn left_future<B>(self) -> Either<Self, B> ⓘ
Source§fn right_future<A>(self) -> Either<A, Self> ⓘ
fn right_future<A>(self) -> Either<A, Self> ⓘ
Source§fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
Source§fn flatten(self) -> Flatten<Self> ⓘ
fn flatten(self) -> Flatten<Self> ⓘ
Source§fn flatten_stream(self) -> FlattenStream<Self>
fn flatten_stream(self) -> FlattenStream<Self>
Source§fn fuse(self) -> Fuse<Self> ⓘwhere
Self: Sized,
fn fuse(self) -> Fuse<Self> ⓘwhere
Self: Sized,
poll will never again be called once it has
completed. This method can be used to turn any Future into a
FusedFuture. Read moreSource§fn inspect<F>(self, f: F) -> Inspect<Self, F> ⓘ
fn inspect<F>(self, f: F) -> Inspect<Self, F> ⓘ
Source§fn catch_unwind(self) -> CatchUnwind<Self> ⓘwhere
Self: Sized + UnwindSafe,
fn catch_unwind(self) -> CatchUnwind<Self> ⓘwhere
Self: Sized + UnwindSafe,
Source§fn remote_handle(self) -> (Remote<Self>, RemoteHandle<Self::Output>)where
Self: Sized,
fn remote_handle(self) -> (Remote<Self>, RemoteHandle<Self::Output>)where
Self: Sized,
() on completion and sends
its output to another future on a separate task. Read moreSource§fn boxed<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>
fn boxed<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>
Source§fn boxed_local<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + 'a>>where
Self: Sized + 'a,
fn boxed_local<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + 'a>>where
Self: Sized + 'a,
Source§fn unit_error(self) -> UnitError<Self> ⓘwhere
Self: Sized,
fn unit_error(self) -> UnitError<Self> ⓘwhere
Self: Sized,
Future<Output = T> into a
TryFuture<Ok = T, Error = ()>.Source§fn never_error(self) -> NeverError<Self> ⓘwhere
Self: Sized,
fn never_error(self) -> NeverError<Self> ⓘwhere
Self: Sized,
Future<Output = T> into a
TryFuture<Ok = T, Error = Never>.