runtime: stabilize runtime::id::Id (#7125)

This commit is contained in:
Chinedu Francis Nwafili
2025-12-16 00:29:10 +08:00
committed by GitHub
parent d3fe35593d
commit 0fa7755e97
7 changed files with 50 additions and 81 deletions
+21 -29
View File
@@ -1,4 +1,3 @@
#[cfg(tokio_unstable)]
use crate::runtime; use crate::runtime;
use crate::runtime::{context, scheduler, RuntimeFlavor, RuntimeMetrics}; use crate::runtime::{context, scheduler, RuntimeFlavor, RuntimeMetrics};
@@ -451,34 +450,27 @@ impl Handle {
} }
} }
cfg_unstable! { /// Returns the [`Id`] of the current `Runtime`.
/// Returns the [`Id`] of the current `Runtime`. ///
/// /// # Examples
/// # Examples ///
/// /// ```
/// ``` /// use tokio::runtime::Handle;
/// use tokio::runtime::Handle; ///
/// /// #[tokio::main(flavor = "current_thread")]
/// #[tokio::main(flavor = "current_thread")] /// async fn main() {
/// async fn main() { /// println!("Current runtime id: {}", Handle::current().id());
/// println!("Current runtime id: {}", Handle::current().id()); /// }
/// } /// ```
/// ``` ///
/// /// [`Id`]: struct@crate::runtime::Id
/// **Note**: This is an [unstable API][unstable]. The public API of this type pub fn id(&self) -> runtime::Id {
/// may break in 1.x releases. See [the documentation on unstable let owned_id = match &self.inner {
/// features][unstable] for details. scheduler::Handle::CurrentThread(handle) => handle.owned_id(),
/// #[cfg(feature = "rt-multi-thread")]
/// [unstable]: crate#unstable-features scheduler::Handle::MultiThread(handle) => handle.owned_id(),
/// [`Id`]: struct@crate::runtime::Id };
pub fn id(&self) -> runtime::Id { runtime::Id::new(owned_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 a view that lets you get information about how the runtime /// Returns a view that lets you get information about how the runtime
+4 -17
View File
@@ -1,5 +1,5 @@
use std::fmt; 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 /// An opaque ID that uniquely identifies a runtime relative to all other currently
/// running runtimes. /// 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)] #[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
pub struct Id(NonZeroU64); pub struct Id(NonZeroU64);
impl From<NonZeroU64> for Id { impl Id {
fn from(value: NonZeroU64) -> Self { pub(crate) fn new(integer: impl Into<NonZeroU64>) -> Self {
Id(value) Self(integer.into())
}
}
impl From<NonZeroU32> for Id {
fn from(value: NonZeroU32) -> Self {
Id(value.into())
} }
} }
+4 -4
View File
@@ -545,10 +545,6 @@ cfg_rt! {
mod builder; mod builder;
pub use self::builder::Builder; pub use self::builder::Builder;
cfg_unstable! { cfg_unstable! {
mod id;
#[cfg_attr(not(tokio_unstable), allow(unreachable_pub))]
pub use id::Id;
pub use self::builder::UnhandledPanic; pub use self::builder::UnhandledPanic;
pub use crate::util::rand::RngSeed; pub use crate::util::rand::RngSeed;
@@ -575,6 +571,10 @@ cfg_rt! {
mod runtime; mod runtime;
pub use runtime::{Runtime, RuntimeFlavor}; pub use runtime::{Runtime, RuntimeFlavor};
mod id;
pub use id::Id;
/// Boundary value to prevent stack overflow caused by a large-sized /// Boundary value to prevent stack overflow caused by a large-sized
/// Future being placed in the stack. /// Future being placed in the stack.
pub(crate) const BOX_FUTURE_THRESHOLD: usize = if cfg!(debug_assertions) { pub(crate) const BOX_FUTURE_THRESHOLD: usize = if cfg!(debug_assertions) {
@@ -629,13 +629,11 @@ cfg_unstable_metrics! {
} }
} }
cfg_unstable! { use std::num::NonZeroU64;
use std::num::NonZeroU64;
impl Handle { impl Handle {
pub(crate) fn owned_id(&self) -> NonZeroU64 { pub(crate) fn owned_id(&self) -> NonZeroU64 {
self.shared.owned.id self.shared.owned.id
}
} }
} }
@@ -10,6 +10,7 @@ use crate::runtime::{
use crate::util::RngSeedGenerator; use crate::util::RngSeedGenerator;
use std::fmt; use std::fmt;
use std::num::NonZeroU64;
mod metrics; mod metrics;
@@ -118,13 +119,9 @@ impl task::Schedule for Arc<Handle> {
} }
} }
cfg_unstable! { impl Handle {
use std::num::NonZeroU64; pub(crate) fn owned_id(&self) -> NonZeroU64 {
self.shared.owned.id
impl Handle {
pub(crate) fn owned_id(&self) -> NonZeroU64 {
self.shared.owned.id
}
} }
} }
+1 -1
View File
@@ -936,7 +936,7 @@ cfg_unstable! {
/// [unstable]: crate#unstable-features /// [unstable]: crate#unstable-features
/// [`Id`]: struct@crate::runtime::Id /// [`Id`]: struct@crate::runtime::Id
pub fn id(&self) -> 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)
} }
} }
} }
+12 -17
View File
@@ -100,27 +100,22 @@ fn drop_tasks_with_reference_cycle() {
}); });
} }
#[cfg(tokio_unstable)] #[test]
mod unstable { fn runtime_id_is_same() {
use super::*; let rt = rt();
#[test] let handle1 = rt.handle();
fn runtime_id_is_same() { let handle2 = rt.handle();
let rt = rt();
let handle1 = rt.handle(); assert_eq!(handle1.id(), handle2.id());
let handle2 = rt.handle(); }
assert_eq!(handle1.id(), handle2.id()); #[test]
} fn runtime_ids_different() {
let rt1 = rt();
let rt2 = rt();
#[test] assert_ne!(rt1.handle().id(), rt2.handle().id());
fn runtime_ids_different() {
let rt1 = rt();
let rt2 = rt();
assert_ne!(rt1.handle().id(), rt2.handle().id());
}
} }
fn rt() -> Runtime { fn rt() -> Runtime {