pub struct Task<T>(_);
Expand description
A spawned future that can be detached
Tasks are also futures themselves and yield the output of the spawned future.
When a task is dropped, its gets canceled and won’t be polled again. To
cancel a task a bit more gracefully and wait until it stops running, use the
cancel()
method.
Tasks that panic get immediately canceled. Awaiting a canceled task also causes a panic.
Examples
let task = glommio::spawn_local(async {
println!("Hello from a task!");
1 + 2
});
assert_eq!(task.await, 3);
Note that there is no guarantee of ordering when reasoning about when a task runs, as that is an implementation detail.
In particular, acquiring a borrow and holding across a task spawning may sometimes work but panic depending on scheduling decisions, so it is still illegal.
let example = Rc::new(RefCell::new(0));
let exclone = example.clone();
let mut ex_mut = example.borrow_mut();
*ex_mut = 1;
let task = glommio::spawn_local(async move {
let ex = exclone.borrow();
println!("Current value: {}", ex);
});
// This is fine if `task` executes after the current task, but will panic if
// preempts the current task and executes first. This is therefore invalid.
*ex_mut = 2;
drop(ex_mut);
task.await;
Implementations
sourceimpl<T> Task<T>
impl<T> Task<T>
sourcepub fn detach(self) -> JoinHandle<T>ⓘNotable traits for JoinHandle<R>impl<R> Future for JoinHandle<R> type Output = Option<R>;
pub fn detach(self) -> JoinHandle<T>ⓘNotable traits for JoinHandle<R>impl<R> Future for JoinHandle<R> type Output = Option<R>;
Detaches the task to let it keep running in the background.
Examples
use futures_lite::future;
use glommio::{timer::Timer, LocalExecutor};
let ex = LocalExecutor::default();
ex.run(async {
glommio::spawn_local(async {
loop {
println!("I'm a background task looping forever.");
glommio::executor().yield_task_queue_now().await;
}
})
.detach();
Timer::new(std::time::Duration::from_micros(100)).await;
})
sourcepub async fn cancel(self) -> Option<T>
pub async fn cancel(self) -> Option<T>
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 futures_lite::future;
use glommio::LocalExecutor;
let ex = LocalExecutor::default();
ex.run(async {
let task = glommio::spawn_local(async {
loop {
println!("Even though I'm in an infinite loop, you can still cancel me!");
future::yield_now().await;
}
});
task.cancel().await;
});
Trait Implementations
Auto Trait Implementations
impl<T> RefUnwindSafe for Task<T> where
T: RefUnwindSafe,
impl<T> !Send for Task<T>
impl<T> !Sync for Task<T>
impl<T> Unpin for Task<T>
impl<T> UnwindSafe for Task<T> where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<F> FutureExt for F where
F: Future + ?Sized,
impl<F> FutureExt for F where
F: Future + ?Sized,
sourcefn poll(&mut self, cx: &mut Context<'_>) -> Poll<Self::Output> where
Self: Unpin,
fn poll(&mut self, cx: &mut Context<'_>) -> Poll<Self::Output> where
Self: Unpin,
A convenience for calling Future::poll()
on !
Unpin
types.
sourcefn or<F>(self, other: F) -> Or<Self, F> where
F: Future<Output = Self::Output>,
fn or<F>(self, other: F) -> Or<Self, F> where
F: Future<Output = Self::Output>,
Returns the result of self
or other
future, preferring self
if both are ready. Read more
sourcefn race<F>(self, other: F) -> Race<Self, F> where
F: Future<Output = Self::Output>,
fn race<F>(self, other: F) -> Race<Self, F> where
F: Future<Output = Self::Output>,
Returns the result of self
or other
future, with no preference if both are ready. Read more
sourcefn catch_unwind(self) -> CatchUnwind<Self> where
Self: UnwindSafe,
fn catch_unwind(self) -> CatchUnwind<Self> where
Self: UnwindSafe,
Catches panics while polling the future. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<F> IntoFuture for F where
F: Future,
impl<F> IntoFuture for F where
F: Future,
type Output = <F as Future>::Output
type Output = <F as Future>::Output
into_future
)The output that the future will produce on completion.
type Future = F
type Future = F
into_future
)Which kind of future are we turning this into?
sourcepub fn into_future(self) -> <F as IntoFuture>::Future
pub fn into_future(self) -> <F as IntoFuture>::Future
into_future
)Creates a future from a value.
impl<T> Pointable for T
impl<T> Pointable for T
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more