tokio/runtime/id.rs
1use std::fmt;
2use std::num::NonZeroU64;
3
4/// An opaque ID that uniquely identifies a runtime relative to all other currently
5/// running runtimes.
6///
7/// # Notes
8///
9/// - Runtime IDs are unique relative to other *currently running* runtimes.
10/// When a runtime completes, the same ID may be used for another runtime.
11/// - Runtime IDs are *not* sequential, and do not indicate the order in which
12/// runtimes are started or any other data.
13/// - The runtime ID of the currently running task can be obtained from the
14/// Handle.
15///
16/// # Examples
17///
18/// ```
19/// # #[cfg(not(target_family = "wasm"))]
20/// # {
21/// use tokio::runtime::Handle;
22///
23/// #[tokio::main(flavor = "multi_thread", worker_threads = 4)]
24/// async fn main() {
25/// println!("Current runtime id: {}", Handle::current().id());
26/// }
27/// # }
28/// ```
29#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
30pub struct Id(NonZeroU64);
31
32impl Id {
33 pub(crate) fn new(integer: impl Into<NonZeroU64>) -> Self {
34 Self(integer.into())
35 }
36}
37
38impl fmt::Display for Id {
39 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40 self.0.fmt(f)
41 }
42}