tokio/runtime/
id.rs

1use std::fmt;
2use std::num::{NonZeroU32, 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///
30/// **Note**: This is an [unstable API][unstable]. The public API of this type
31/// may break in 1.x releases. See [the documentation on unstable
32/// features][unstable] for details.
33///
34/// [unstable]: crate#unstable-features
35#[cfg_attr(not(tokio_unstable), allow(unreachable_pub))]
36#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
37pub struct Id(NonZeroU64);
38
39impl From<NonZeroU64> for Id {
40    fn from(value: NonZeroU64) -> Self {
41        Id(value)
42    }
43}
44
45impl From<NonZeroU32> for Id {
46    fn from(value: NonZeroU32) -> Self {
47        Id(value.into())
48    }
49}
50
51impl fmt::Display for Id {
52    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53        self.0.fmt(f)
54    }
55}