tokio: update code according to new MSRV (#6764)

This commit is contained in:
Vrtgs 2024-08-11 12:55:22 +03:00 committed by GitHub
parent 6ad1912353
commit 17819062e2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 14 additions and 62 deletions

View File

@ -1049,28 +1049,13 @@ impl Builder {
} }
fn adjust_max_frame_len(&mut self) { 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. // 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) { let max_number = match 1u64.checked_shl((8 * self.length_field_len) as u32) {
Some(shl) => shl - 1, Some(shl) => shl - 1,
None => u64::MAX, 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 { if self.max_frame_len as u64 > max_allowed_len {
self.max_frame_len = usize::try_from(max_allowed_len).unwrap_or(usize::MAX); self.max_frame_len = usize::try_from(max_allowed_len).unwrap_or(usize::MAX);

View File

@ -24,11 +24,5 @@ use std::path::Path;
/// ``` /// ```
pub async fn try_exists(path: impl AsRef<Path>) -> io::Result<bool> { pub async fn try_exists(path: impl AsRef<Path>) -> io::Result<bool> {
let path = path.as_ref().to_owned(); let path = path.as_ref().to_owned();
// std's Path::try_exists is not available for current Rust min supported version. asyncify(move || path.try_exists()).await
// 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),
}
} }

View File

@ -6,13 +6,12 @@ use std::future::Future;
use std::io; use std::io;
use std::io::ErrorKind::UnexpectedEof; use std::io::ErrorKind::UnexpectedEof;
use std::marker::PhantomPinned; use std::marker::PhantomPinned;
use std::mem::size_of;
use std::pin::Pin; use std::pin::Pin;
use std::task::{Context, Poll}; use std::task::{Context, Poll};
macro_rules! reader { macro_rules! reader {
($name:ident, $ty:ty, $reader:ident) => { ($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) => { ($name:ident, $ty:ty, $reader:ident, $bytes:expr) => {
pin_project! { pin_project! {

View File

@ -5,13 +5,12 @@ use pin_project_lite::pin_project;
use std::future::Future; use std::future::Future;
use std::io; use std::io;
use std::marker::PhantomPinned; use std::marker::PhantomPinned;
use std::mem::size_of;
use std::pin::Pin; use std::pin::Pin;
use std::task::{Context, Poll}; use std::task::{Context, Poll};
macro_rules! writer { macro_rules! writer {
($name:ident, $ty:ty, $writer:ident) => { ($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) => { ($name:ident, $ty:ty, $writer:ident, $bytes:expr) => {
pin_project! { pin_project! {

View File

@ -461,7 +461,7 @@ impl Spawner {
shutdown_tx: shutdown::Sender, shutdown_tx: shutdown::Sender,
rt: &Handle, rt: &Handle,
id: usize, id: usize,
) -> std::io::Result<thread::JoinHandle<()>> { ) -> io::Result<thread::JoinHandle<()>> {
let mut builder = thread::Builder::new().name((self.inner.thread_name)()); let mut builder = thread::Builder::new().name((self.inner.thread_name)());
if let Some(stack_size) = self.inner.stack_size { 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. // Tells whether the error when spawning a thread is temporary.
#[inline] #[inline]
fn is_temporary_os_thread_error(error: &std::io::Error) -> bool { fn is_temporary_os_thread_error(error: &io::Error) -> bool {
matches!(error.kind(), std::io::ErrorKind::WouldBlock) matches!(error.kind(), io::ErrorKind::WouldBlock)
} }
impl Inner { impl Inner {

View File

@ -1014,7 +1014,7 @@ impl Core {
.tuned_global_queue_interval(&worker.handle.shared.config); .tuned_global_queue_interval(&worker.handle.shared.config);
// Smooth out jitter // 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; self.global_queue_interval = next;
} }
} }
@ -1249,12 +1249,3 @@ fn with_current<R>(f: impl FnOnce(Option<&Context>) -> R) -> R {
_ => f(None), _ => 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
}
}

View File

@ -1292,7 +1292,7 @@ impl Worker {
let next = core.stats.tuned_global_queue_interval(&cx.shared().config); let next = core.stats.tuned_global_queue_interval(&cx.shared().config);
// Smooth out jitter // 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; self.global_queue_interval = next;
} }
} }
@ -1592,12 +1592,3 @@ fn with_current<R>(f: impl FnOnce(Option<&Context>) -> R) -> R {
_ => f(None), _ => 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
}
}

View File

@ -489,7 +489,5 @@ impl Trailer {
#[test] #[test]
#[cfg(not(loom))] #[cfg(not(loom))]
fn header_lte_cache_line() { fn header_lte_cache_line() {
use std::mem::size_of; assert!(std::mem::size_of::<Header>() <= 8 * std::mem::size_of::<*const ()>());
assert!(size_of::<Header>() <= 8 * size_of::<*const ()>());
} }

View File

@ -5,7 +5,7 @@ use std::future::Future;
cfg_rt! { cfg_rt! {
/// Spawns a new asynchronous task, returning a /// 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 /// The provided future will start running in the background immediately
/// when `spawn` is called, even if you don't await the returned /// when `spawn` is called, even if you don't await the returned

View File

@ -16,7 +16,7 @@ impl Pack {
/// Value is packed in the `width` more-significant bits. /// Value is packed in the `width` more-significant bits.
pub(crate) const fn then(&self, width: u32) -> Pack { 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; let mask = mask_for(width) << shift;
Pack { mask, shift } Pack { mask, shift }
@ -24,7 +24,7 @@ impl Pack {
/// Width, in bits, dedicated to storing the value. /// Width, in bits, dedicated to storing the value.
pub(crate) const fn width(&self) -> u32 { 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. /// 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::<usize>() as u32 * 8
}
/// Returns a `usize` with the right-most `n` bits set. /// Returns a `usize` with the right-most `n` bits set.
pub(crate) const fn mask_for(n: u32) -> usize { pub(crate) const fn mask_for(n: u32) -> usize {
let shift = 1usize.wrapping_shl(n - 1); let shift = 1usize.wrapping_shl(n - 1);

View File

@ -372,7 +372,7 @@ async fn multiple_waiters() {
panic!("Tasks exited unexpectedly") panic!("Tasks exited unexpectedly")
}, },
_ = barrier.wait() => {} _ = barrier.wait() => {}
}; }
b.write_all(b"0").unwrap(); b.write_all(b"0").unwrap();