mirror of
https://github.com/async-rs/async-std.git
synced 2025-01-16 10:49:55 +00:00
commit
d395607761
2 changed files with 23 additions and 3 deletions
|
@ -23,6 +23,26 @@ use crate::task::{Task, TaskLocalsWrapper};
|
||||||
/// # })
|
/// # })
|
||||||
/// ```
|
/// ```
|
||||||
pub fn current() -> Task {
|
pub fn current() -> Task {
|
||||||
TaskLocalsWrapper::get_current(|t| t.task().clone())
|
try_current().expect("`task::current()` called outside the context of a task")
|
||||||
.expect("`task::current()` called outside the context of a task")
|
}
|
||||||
|
|
||||||
|
/// Returns a handle to the current task if called within the context of a task created by [`block_on`],
|
||||||
|
/// [`spawn`], or [`Builder::spawn`], otherwise returns `None`.
|
||||||
|
///
|
||||||
|
/// [`block_on`]: fn.block_on.html
|
||||||
|
/// [`spawn`]: fn.spawn.html
|
||||||
|
/// [`Builder::spawn`]: struct.Builder.html#method.spawn
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use async_std::task;
|
||||||
|
///
|
||||||
|
/// match task::try_current() {
|
||||||
|
/// Some(t) => println!("The name of this task is {:?}", t.name()),
|
||||||
|
/// None => println!("Not inside a task!"),
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
pub fn try_current() -> Option<Task> {
|
||||||
|
TaskLocalsWrapper::get_current(|t| t.task().clone())
|
||||||
}
|
}
|
|
@ -133,7 +133,7 @@ cfg_std! {
|
||||||
cfg_default! {
|
cfg_default! {
|
||||||
pub use block_on::block_on;
|
pub use block_on::block_on;
|
||||||
pub use builder::Builder;
|
pub use builder::Builder;
|
||||||
pub use current::current;
|
pub use current::{current, try_current};
|
||||||
pub use task::Task;
|
pub use task::Task;
|
||||||
pub use task_id::TaskId;
|
pub use task_id::TaskId;
|
||||||
pub use join_handle::JoinHandle;
|
pub use join_handle::JoinHandle;
|
||||||
|
|
Loading…
Reference in a new issue