1 use std::fmt;
2 use 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 /// use tokio::runtime::Handle;
20 ///
21 /// #[tokio::main(flavor = "multi_thread", worker_threads = 4)]
22 /// async fn main() {
23 ///   println!("Current runtime id: {}", Handle::current().id());
24 /// }
25 /// ```
26 ///
27 /// **Note**: This is an [unstable API][unstable]. The public API of this type
28 /// may break in 1.x releases. See [the documentation on unstable
29 /// features][unstable] for details.
30 ///
31 /// [unstable]: crate#unstable-features
32 #[cfg_attr(not(tokio_unstable), allow(unreachable_pub))]
33 #[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
34 pub struct Id(NonZeroU64);
35 
36 impl From<NonZeroU64> for Id {
from(value: NonZeroU64) -> Self37     fn from(value: NonZeroU64) -> Self {
38         Id(value)
39     }
40 }
41 
42 impl From<NonZeroU32> for Id {
from(value: NonZeroU32) -> Self43     fn from(value: NonZeroU32) -> Self {
44         Id(value.into())
45     }
46 }
47 
48 impl fmt::Display for Id {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result49     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50         self.0.fmt(f)
51     }
52 }
53