Struct slings::Task[][src]

pub struct Task<T> { /* fields omitted */ }
Expand description

A spawned task.

A Task can be awaited to retrieve the output of its future.

Dropping a Task cancels it, which means its future won’t be polled again. To drop the Task handle without canceling it, use detach() instead. To cancel a task gracefully and wait until it is fully destroyed, use the cancel() method.

Note that canceling a task actually wakes it and reschedules one last time. Then, the executor can destroy the task by simply dropping its Runnable or by invoking run().

Examples

use smol::{future, Executor};
use std::thread;

let ex = Executor::new();

// Spawn a future onto the executor.
let task = ex.spawn(async {
    println!("Hello from a task!");
    1 + 2
});

// Run an executor thread.
thread::spawn(move || future::block_on(ex.run(future::pending::<()>())));

// Wait for the task's output.
assert_eq!(future::block_on(task), 3);

Implementations

Detaches the task to let it keep running in the background.

Examples

use smol::{Executor, Timer};
use std::time::Duration;

let ex = Executor::new();

// Spawn a deamon future.
ex.spawn(async {
    loop {
        println!("I'm a daemon task looping forever.");
        Timer::after(Duration::from_secs(1)).await;
    }
})
.detach();

Cancels the task and waits for it to stop running.

Returns the task’s output if it was completed just before it got canceled, or None if it didn’t complete.

While it’s possible to simply drop the Task to cancel it, this is a cleaner way of canceling because it also waits for the task to stop running.

Examples

use smol::{future, Executor, Timer};
use std::thread;
use std::time::Duration;

let ex = Executor::new();

// Spawn a deamon future.
let task = ex.spawn(async {
    loop {
        println!("Even though I'm in an infinite loop, you can still cancel me!");
        Timer::after(Duration::from_secs(1)).await;
    }
});

// Run an executor thread.
thread::spawn(move || future::block_on(ex.run(future::pending::<()>())));

future::block_on(async {
    Timer::after(Duration::from_secs(3)).await;
    task.cancel().await;
});

Trait Implementations

Formats the value using the given formatter. Read more

Executes the destructor for this type. Read more

The type of value produced on completion.

Attempt to resolve the future to a final value, registering the current task for wakeup if the value is not yet available. Read more

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Map this future’s output to a different type, returning a new future of the resulting type. Read more

Map this future’s output to a different type, returning a new future of the resulting type. Read more

Chain on a computation for when a future finished, passing the result of the future to the provided closure f. Read more

Wrap this future in an Either future, making it the left-hand variant of that Either. Read more

Wrap this future in an Either future, making it the right-hand variant of that Either. Read more

Convert this future into a single element stream. Read more

Flatten the execution of this future when the output of this future is itself another future. Read more

Flatten the execution of this future when the successful result of this future is a stream. Read more

Fuse a future such that poll will never again be called once it has completed. This method can be used to turn any Future into a FusedFuture. Read more

Do something with the output of a future before passing it on. Read more

Catches unwinding panics while polling the future. Read more

Create a cloneable handle to this future where all handles will resolve to the same result. Read more

Wrap the future in a Box, pinning it. Read more

Wrap the future in a Box, pinning it. Read more

A convenience for calling Future::poll on Unpin future types.

Evaluates and consumes the future, returning the resulting output if the future is ready after the first call to Future::poll. Read more

Performs the conversion.

🔬 This is a nightly-only experimental API. (into_future)

The output that the future will produce on completion.

🔬 This is a nightly-only experimental API. (into_future)

Which kind of future are we turning this into?

🔬 This is a nightly-only experimental API. (into_future)

Creates a future from a value.

The type returned in the event of a conversion error.

Performs the conversion.

The type of successful values yielded by this future

The type of failures yielded by this future

Poll this TryFuture as if it were a Future. Read more

Maps this future’s success value to a different value. Read more

Maps this future’s success value to a different value, and permits for error handling resulting in the same type. Read more

Maps this future’s error value to a different value. Read more

Maps this future’s Error to a new error type using the Into trait. Read more

Maps this future’s Ok to a new type using the Into trait. Read more

Executes another future after this one resolves successfully. The success value is passed to a closure to create this subsequent future. Read more

Executes another future if this one resolves to an error. The error value is passed to a closure to create this subsequent future. Read more

Do something with the success value of a future before passing it on. Read more

Do something with the error value of a future before passing it on. Read more

Flatten the execution of this future when the successful result of this future is another future. Read more

Flatten the execution of this future when the successful result of this future is a stream. Read more

Unwraps this future’s output, producing a future with this future’s Ok type as its Output type. Read more

Wraps a [TryFuture] into a type that implements Future. Read more

A convenience method for calling [TryFuture::try_poll] on Unpin future types. Read more

The type returned in the event of a conversion error.

Performs the conversion.