async_std/task/
task.rs

1use std::fmt;
2use std::sync::Arc;
3
4use crate::task::TaskId;
5
6/// A handle to a task.
7#[derive(Clone)]
8pub struct Task {
9    /// The task ID.
10    id: TaskId,
11
12    /// The optional task name.
13    name: Option<Arc<String>>,
14}
15
16impl Task {
17    /// Creates a new task handle.
18    #[inline]
19    pub(crate) fn new(name: Option<Arc<String>>) -> Task {
20        Task {
21            id: TaskId::generate(),
22            name,
23        }
24    }
25
26    /// Gets the task's unique identifier.
27    #[inline]
28    pub fn id(&self) -> TaskId {
29        self.id
30    }
31
32    /// Returns the name of this task.
33    ///
34    /// The name is configured by [`Builder::name`] before spawning.
35    ///
36    /// [`Builder::name`]: struct.Builder.html#method.name
37    pub fn name(&self) -> Option<&str> {
38        self.name.as_ref().map(|s| s.as_str())
39    }
40}
41
42impl fmt::Debug for Task {
43    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44        f.debug_struct("Task")
45            .field("id", &self.id())
46            .field("name", &self.name())
47            .finish()
48    }
49}