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::{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
+4 -17
View File
@@ -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<NonZeroU64> for Id {
fn from(value: NonZeroU64) -> Self {
Id(value)
}
}
impl From<NonZeroU32> for Id {
fn from(value: NonZeroU32) -> Self {
Id(value.into())
impl Id {
pub(crate) fn new(integer: impl Into<NonZeroU64>) -> Self {
Self(integer.into())
}
}
+4 -4
View File
@@ -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) {
@@ -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
}
}
@@ -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<Handle> {
}
}
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
}
}
+1 -1
View File
@@ -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)
}
}
}
+12 -17
View File
@@ -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 {