From 17819062e27b48570dd7f510b8bf8cf7b0e52cf4 Mon Sep 17 00:00:00 2001 From: Vrtgs Date: Sun, 11 Aug 2024 12:55:22 +0300 Subject: [PATCH] tokio: update code according to new MSRV (#6764) --- tokio-util/src/codec/length_delimited.rs | 17 +---------------- tokio/src/fs/try_exists.rs | 8 +------- tokio/src/io/util/read_int.rs | 3 +-- tokio/src/io/util/write_int.rs | 3 +-- tokio/src/runtime/blocking/pool.rs | 6 +++--- .../runtime/scheduler/multi_thread/worker.rs | 11 +---------- .../scheduler/multi_thread_alt/worker.rs | 11 +---------- tokio/src/runtime/task/core.rs | 4 +--- tokio/src/task/spawn.rs | 2 +- tokio/src/util/bit.rs | 9 ++------- tokio/tests/io_async_fd.rs | 2 +- 11 files changed, 14 insertions(+), 62 deletions(-) diff --git a/tokio-util/src/codec/length_delimited.rs b/tokio-util/src/codec/length_delimited.rs index c1f24167a..fed49ca8a 100644 --- a/tokio-util/src/codec/length_delimited.rs +++ b/tokio-util/src/codec/length_delimited.rs @@ -1049,28 +1049,13 @@ impl Builder { } fn adjust_max_frame_len(&mut self) { - // This function is basically `std::u64::saturating_add_signed`. Since it - // requires MSRV 1.66, its implementation is copied here. - // - // TODO: use the method from std when MSRV becomes >= 1.66 - fn saturating_add_signed(num: u64, rhs: i64) -> u64 { - let (res, overflow) = num.overflowing_add(rhs as u64); - if overflow == (rhs < 0) { - res - } else if overflow { - u64::MAX - } else { - 0 - } - } - // Calculate the maximum number that can be represented using `length_field_len` bytes. let max_number = match 1u64.checked_shl((8 * self.length_field_len) as u32) { Some(shl) => shl - 1, None => u64::MAX, }; - let max_allowed_len = saturating_add_signed(max_number, self.length_adjustment as i64); + let max_allowed_len = max_number.saturating_add_signed(self.length_adjustment as i64); if self.max_frame_len as u64 > max_allowed_len { self.max_frame_len = usize::try_from(max_allowed_len).unwrap_or(usize::MAX); diff --git a/tokio/src/fs/try_exists.rs b/tokio/src/fs/try_exists.rs index 069518bf9..2e8de04e0 100644 --- a/tokio/src/fs/try_exists.rs +++ b/tokio/src/fs/try_exists.rs @@ -24,11 +24,5 @@ use std::path::Path; /// ``` pub async fn try_exists(path: impl AsRef) -> io::Result { let path = path.as_ref().to_owned(); - // std's Path::try_exists is not available for current Rust min supported version. - // Current implementation is based on its internal implementation instead. - match asyncify(move || std::fs::metadata(path)).await { - Ok(_) => Ok(true), - Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(false), - Err(error) => Err(error), - } + asyncify(move || path.try_exists()).await } diff --git a/tokio/src/io/util/read_int.rs b/tokio/src/io/util/read_int.rs index 164dcf596..63324fb5c 100644 --- a/tokio/src/io/util/read_int.rs +++ b/tokio/src/io/util/read_int.rs @@ -6,13 +6,12 @@ use std::future::Future; use std::io; use std::io::ErrorKind::UnexpectedEof; use std::marker::PhantomPinned; -use std::mem::size_of; use std::pin::Pin; use std::task::{Context, Poll}; macro_rules! reader { ($name:ident, $ty:ty, $reader:ident) => { - reader!($name, $ty, $reader, size_of::<$ty>()); + reader!($name, $ty, $reader, std::mem::size_of::<$ty>()); }; ($name:ident, $ty:ty, $reader:ident, $bytes:expr) => { pin_project! { diff --git a/tokio/src/io/util/write_int.rs b/tokio/src/io/util/write_int.rs index 63cd49126..1eb889097 100644 --- a/tokio/src/io/util/write_int.rs +++ b/tokio/src/io/util/write_int.rs @@ -5,13 +5,12 @@ use pin_project_lite::pin_project; use std::future::Future; use std::io; use std::marker::PhantomPinned; -use std::mem::size_of; use std::pin::Pin; use std::task::{Context, Poll}; macro_rules! writer { ($name:ident, $ty:ty, $writer:ident) => { - writer!($name, $ty, $writer, size_of::<$ty>()); + writer!($name, $ty, $writer, std::mem::size_of::<$ty>()); }; ($name:ident, $ty:ty, $writer:ident, $bytes:expr) => { pin_project! { diff --git a/tokio/src/runtime/blocking/pool.rs b/tokio/src/runtime/blocking/pool.rs index bc854f36d..8bd44278e 100644 --- a/tokio/src/runtime/blocking/pool.rs +++ b/tokio/src/runtime/blocking/pool.rs @@ -461,7 +461,7 @@ impl Spawner { shutdown_tx: shutdown::Sender, rt: &Handle, id: usize, - ) -> std::io::Result> { + ) -> io::Result> { let mut builder = thread::Builder::new().name((self.inner.thread_name)()); if let Some(stack_size) = self.inner.stack_size { @@ -497,8 +497,8 @@ cfg_unstable_metrics! { // Tells whether the error when spawning a thread is temporary. #[inline] -fn is_temporary_os_thread_error(error: &std::io::Error) -> bool { - matches!(error.kind(), std::io::ErrorKind::WouldBlock) +fn is_temporary_os_thread_error(error: &io::Error) -> bool { + matches!(error.kind(), io::ErrorKind::WouldBlock) } impl Inner { diff --git a/tokio/src/runtime/scheduler/multi_thread/worker.rs b/tokio/src/runtime/scheduler/multi_thread/worker.rs index 9b521efc9..cefa8704b 100644 --- a/tokio/src/runtime/scheduler/multi_thread/worker.rs +++ b/tokio/src/runtime/scheduler/multi_thread/worker.rs @@ -1014,7 +1014,7 @@ impl Core { .tuned_global_queue_interval(&worker.handle.shared.config); // Smooth out jitter - if abs_diff(self.global_queue_interval, next) > 2 { + if u32::abs_diff(self.global_queue_interval, next) > 2 { self.global_queue_interval = next; } } @@ -1249,12 +1249,3 @@ fn with_current(f: impl FnOnce(Option<&Context>) -> R) -> R { _ => f(None), }) } - -// `u32::abs_diff` is not available on Tokio's MSRV. -fn abs_diff(a: u32, b: u32) -> u32 { - if a > b { - a - b - } else { - b - a - } -} diff --git a/tokio/src/runtime/scheduler/multi_thread_alt/worker.rs b/tokio/src/runtime/scheduler/multi_thread_alt/worker.rs index 9f6af9d4d..b55dc317a 100644 --- a/tokio/src/runtime/scheduler/multi_thread_alt/worker.rs +++ b/tokio/src/runtime/scheduler/multi_thread_alt/worker.rs @@ -1292,7 +1292,7 @@ impl Worker { let next = core.stats.tuned_global_queue_interval(&cx.shared().config); // Smooth out jitter - if abs_diff(self.global_queue_interval, next) > 2 { + if u32::abs_diff(self.global_queue_interval, next) > 2 { self.global_queue_interval = next; } } @@ -1592,12 +1592,3 @@ fn with_current(f: impl FnOnce(Option<&Context>) -> R) -> R { _ => f(None), }) } - -// `u32::abs_diff` is not available on Tokio's MSRV. -fn abs_diff(a: u32, b: u32) -> u32 { - if a > b { - a - b - } else { - b - a - } -} diff --git a/tokio/src/runtime/task/core.rs b/tokio/src/runtime/task/core.rs index 78977084a..590e3fbc9 100644 --- a/tokio/src/runtime/task/core.rs +++ b/tokio/src/runtime/task/core.rs @@ -489,7 +489,5 @@ impl Trailer { #[test] #[cfg(not(loom))] fn header_lte_cache_line() { - use std::mem::size_of; - - assert!(size_of::
() <= 8 * size_of::<*const ()>()); + assert!(std::mem::size_of::
() <= 8 * std::mem::size_of::<*const ()>()); } diff --git a/tokio/src/task/spawn.rs b/tokio/src/task/spawn.rs index c9142cc5f..ec7a18c81 100644 --- a/tokio/src/task/spawn.rs +++ b/tokio/src/task/spawn.rs @@ -5,7 +5,7 @@ use std::future::Future; cfg_rt! { /// Spawns a new asynchronous task, returning a - /// [`JoinHandle`](super::JoinHandle) for it. + /// [`JoinHandle`](JoinHandle) for it. /// /// The provided future will start running in the background immediately /// when `spawn` is called, even if you don't await the returned diff --git a/tokio/src/util/bit.rs b/tokio/src/util/bit.rs index 72e25e45f..d71ad548c 100644 --- a/tokio/src/util/bit.rs +++ b/tokio/src/util/bit.rs @@ -16,7 +16,7 @@ impl Pack { /// Value is packed in the `width` more-significant bits. pub(crate) const fn then(&self, width: u32) -> Pack { - let shift = pointer_width() - self.mask.leading_zeros(); + let shift = usize::BITS - self.mask.leading_zeros(); let mask = mask_for(width) << shift; Pack { mask, shift } @@ -24,7 +24,7 @@ impl Pack { /// Width, in bits, dedicated to storing the value. pub(crate) const fn width(&self) -> u32 { - pointer_width() - (self.mask >> self.shift).leading_zeros() + usize::BITS - (self.mask >> self.shift).leading_zeros() } /// Max representable value. @@ -52,11 +52,6 @@ impl fmt::Debug for Pack { } } -/// Returns the width of a pointer in bits. -pub(crate) const fn pointer_width() -> u32 { - std::mem::size_of::() as u32 * 8 -} - /// Returns a `usize` with the right-most `n` bits set. pub(crate) const fn mask_for(n: u32) -> usize { let shift = 1usize.wrapping_shl(n - 1); diff --git a/tokio/tests/io_async_fd.rs b/tokio/tests/io_async_fd.rs index 4e6f630eb..3ab1cebd8 100644 --- a/tokio/tests/io_async_fd.rs +++ b/tokio/tests/io_async_fd.rs @@ -372,7 +372,7 @@ async fn multiple_waiters() { panic!("Tasks exited unexpectedly") }, _ = barrier.wait() => {} - }; + } b.write_all(b"0").unwrap();