diff --git a/tokio/src/runtime/handle.rs b/tokio/src/runtime/handle.rs index 5030adc6d..1cf9acc19 100644 --- a/tokio/src/runtime/handle.rs +++ b/tokio/src/runtime/handle.rs @@ -1,4 +1,3 @@ -#[cfg(tokio_unstable)] use crate::runtime; use crate::runtime::{context, scheduler, RuntimeFlavor, RuntimeMetrics}; @@ -451,34 +450,27 @@ impl Handle { } } - cfg_unstable! { - /// Returns the [`Id`] of the current `Runtime`. - /// - /// # Examples - /// - /// ``` - /// use tokio::runtime::Handle; - /// - /// #[tokio::main(flavor = "current_thread")] - /// async fn main() { - /// println!("Current runtime id: {}", Handle::current().id()); - /// } - /// ``` - /// - /// **Note**: This is an [unstable API][unstable]. The public API of this type - /// may break in 1.x releases. See [the documentation on unstable - /// features][unstable] for details. - /// - /// [unstable]: crate#unstable-features - /// [`Id`]: struct@crate::runtime::Id - pub fn id(&self) -> runtime::Id { - let owned_id = match &self.inner { - scheduler::Handle::CurrentThread(handle) => handle.owned_id(), - #[cfg(feature = "rt-multi-thread")] - scheduler::Handle::MultiThread(handle) => handle.owned_id(), - }; - owned_id.into() - } + /// Returns the [`Id`] of the current `Runtime`. + /// + /// # Examples + /// + /// ``` + /// use tokio::runtime::Handle; + /// + /// #[tokio::main(flavor = "current_thread")] + /// async fn main() { + /// println!("Current runtime id: {}", Handle::current().id()); + /// } + /// ``` + /// + /// [`Id`]: struct@crate::runtime::Id + pub fn id(&self) -> runtime::Id { + let owned_id = match &self.inner { + scheduler::Handle::CurrentThread(handle) => handle.owned_id(), + #[cfg(feature = "rt-multi-thread")] + scheduler::Handle::MultiThread(handle) => handle.owned_id(), + }; + runtime::Id::new(owned_id) } /// Returns a view that lets you get information about how the runtime diff --git a/tokio/src/runtime/id.rs b/tokio/src/runtime/id.rs index dc223bdcf..379572a1d 100644 --- a/tokio/src/runtime/id.rs +++ b/tokio/src/runtime/id.rs @@ -1,5 +1,5 @@ use std::fmt; -use std::num::{NonZeroU32, NonZeroU64}; +use std::num::NonZeroU64; /// An opaque ID that uniquely identifies a runtime relative to all other currently /// running runtimes. @@ -26,25 +26,12 @@ use std::num::{NonZeroU32, NonZeroU64}; /// } /// # } /// ``` -/// -/// **Note**: This is an [unstable API][unstable]. The public API of this type -/// may break in 1.x releases. See [the documentation on unstable -/// features][unstable] for details. -/// -/// [unstable]: crate#unstable-features -#[cfg_attr(not(tokio_unstable), allow(unreachable_pub))] #[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)] pub struct Id(NonZeroU64); -impl From for Id { - fn from(value: NonZeroU64) -> Self { - Id(value) - } -} - -impl From for Id { - fn from(value: NonZeroU32) -> Self { - Id(value.into()) +impl Id { + pub(crate) fn new(integer: impl Into) -> Self { + Self(integer.into()) } } diff --git a/tokio/src/runtime/mod.rs b/tokio/src/runtime/mod.rs index 92a159b38..1a341b7b9 100644 --- a/tokio/src/runtime/mod.rs +++ b/tokio/src/runtime/mod.rs @@ -545,10 +545,6 @@ cfg_rt! { mod builder; pub use self::builder::Builder; cfg_unstable! { - mod id; - #[cfg_attr(not(tokio_unstable), allow(unreachable_pub))] - pub use id::Id; - pub use self::builder::UnhandledPanic; pub use crate::util::rand::RngSeed; @@ -575,6 +571,10 @@ cfg_rt! { mod runtime; pub use runtime::{Runtime, RuntimeFlavor}; + mod id; + pub use id::Id; + + /// Boundary value to prevent stack overflow caused by a large-sized /// Future being placed in the stack. pub(crate) const BOX_FUTURE_THRESHOLD: usize = if cfg!(debug_assertions) { diff --git a/tokio/src/runtime/scheduler/current_thread/mod.rs b/tokio/src/runtime/scheduler/current_thread/mod.rs index b505035aa..bdf15f6ed 100644 --- a/tokio/src/runtime/scheduler/current_thread/mod.rs +++ b/tokio/src/runtime/scheduler/current_thread/mod.rs @@ -629,13 +629,11 @@ cfg_unstable_metrics! { } } -cfg_unstable! { - use std::num::NonZeroU64; +use std::num::NonZeroU64; - impl Handle { - pub(crate) fn owned_id(&self) -> NonZeroU64 { - self.shared.owned.id - } +impl Handle { + pub(crate) fn owned_id(&self) -> NonZeroU64 { + self.shared.owned.id } } diff --git a/tokio/src/runtime/scheduler/multi_thread/handle.rs b/tokio/src/runtime/scheduler/multi_thread/handle.rs index 14d65294c..3d4226c40 100644 --- a/tokio/src/runtime/scheduler/multi_thread/handle.rs +++ b/tokio/src/runtime/scheduler/multi_thread/handle.rs @@ -10,6 +10,7 @@ use crate::runtime::{ use crate::util::RngSeedGenerator; use std::fmt; +use std::num::NonZeroU64; mod metrics; @@ -118,13 +119,9 @@ impl task::Schedule for Arc { } } -cfg_unstable! { - use std::num::NonZeroU64; - - impl Handle { - pub(crate) fn owned_id(&self) -> NonZeroU64 { - self.shared.owned.id - } +impl Handle { + pub(crate) fn owned_id(&self) -> NonZeroU64 { + self.shared.owned.id } } diff --git a/tokio/src/task/local.rs b/tokio/src/task/local.rs index eb83a18ea..e80307f3b 100644 --- a/tokio/src/task/local.rs +++ b/tokio/src/task/local.rs @@ -936,7 +936,7 @@ cfg_unstable! { /// [unstable]: crate#unstable-features /// [`Id`]: struct@crate::runtime::Id pub fn id(&self) -> runtime::Id { - self.context.shared.local_state.owned.id.into() + runtime::Id::new(self.context.shared.local_state.owned.id) } } } diff --git a/tokio/tests/rt_handle.rs b/tokio/tests/rt_handle.rs index bfbeff1b2..8feb7207d 100644 --- a/tokio/tests/rt_handle.rs +++ b/tokio/tests/rt_handle.rs @@ -100,27 +100,22 @@ fn drop_tasks_with_reference_cycle() { }); } -#[cfg(tokio_unstable)] -mod unstable { - use super::*; +#[test] +fn runtime_id_is_same() { + let rt = rt(); - #[test] - fn runtime_id_is_same() { - let rt = rt(); + let handle1 = rt.handle(); + let handle2 = rt.handle(); - let handle1 = rt.handle(); - let handle2 = rt.handle(); + assert_eq!(handle1.id(), handle2.id()); +} - assert_eq!(handle1.id(), handle2.id()); - } +#[test] +fn runtime_ids_different() { + let rt1 = rt(); + let rt2 = rt(); - #[test] - fn runtime_ids_different() { - let rt1 = rt(); - let rt2 = rt(); - - assert_ne!(rt1.handle().id(), rt2.handle().id()); - } + assert_ne!(rt1.handle().id(), rt2.handle().id()); } fn rt() -> Runtime {