tokio/runtime/
task_hooks.rs

1use std::marker::PhantomData;
2
3impl TaskHooks {
4    pub(crate) fn spawn(&self, meta: &TaskMeta<'_>) {
5        if let Some(f) = self.task_spawn_callback.as_ref() {
6            f(meta)
7        }
8    }
9}
10
11#[derive(Clone)]
12pub(crate) struct TaskHooks {
13    pub(crate) task_spawn_callback: Option<TaskCallback>,
14    pub(crate) task_terminate_callback: Option<TaskCallback>,
15}
16
17/// Task metadata supplied to user-provided hooks for task events.
18///
19/// **Note**: This is an [unstable API][unstable]. The public API of this type  
20/// may break in 1.x releases. See [the documentation on unstable  
21/// features][unstable] for details.  
22///  
23/// [unstable]: crate#unstable-features  
24#[allow(missing_debug_implementations)]
25#[cfg_attr(not(tokio_unstable), allow(unreachable_pub))]
26pub struct TaskMeta<'a> {
27    /// The opaque ID of the task.
28    pub(crate) id: super::task::Id,
29    pub(crate) _phantom: PhantomData<&'a ()>,
30}
31
32impl<'a> TaskMeta<'a> {
33    /// Return the opaque ID of the task.
34    #[cfg_attr(not(tokio_unstable), allow(unreachable_pub, dead_code))]
35    pub fn id(&self) -> super::task::Id {
36        self.id
37    }
38}
39
40/// Runs on specific task-related events
41pub(crate) type TaskCallback = std::sync::Arc<dyn Fn(&TaskMeta<'_>) + Send + Sync>;