mirror of
https://github.com/tokio-rs/tokio.git
synced 2025-09-25 12:00:35 +00:00
tokio: update code according to new MSRV (#6764)
This commit is contained in:
parent
6ad1912353
commit
17819062e2
@ -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);
|
||||
|
@ -24,11 +24,5 @@ use std::path::Path;
|
||||
/// ```
|
||||
pub async fn try_exists(path: impl AsRef<Path>) -> io::Result<bool> {
|
||||
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
|
||||
}
|
||||
|
@ -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! {
|
||||
|
@ -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! {
|
||||
|
@ -461,7 +461,7 @@ impl Spawner {
|
||||
shutdown_tx: shutdown::Sender,
|
||||
rt: &Handle,
|
||||
id: usize,
|
||||
) -> std::io::Result<thread::JoinHandle<()>> {
|
||||
) -> io::Result<thread::JoinHandle<()>> {
|
||||
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 {
|
||||
|
@ -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<R>(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
|
||||
}
|
||||
}
|
||||
|
@ -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<R>(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
|
||||
}
|
||||
}
|
||||
|
@ -489,7 +489,5 @@ impl Trailer {
|
||||
#[test]
|
||||
#[cfg(not(loom))]
|
||||
fn header_lte_cache_line() {
|
||||
use std::mem::size_of;
|
||||
|
||||
assert!(size_of::<Header>() <= 8 * size_of::<*const ()>());
|
||||
assert!(std::mem::size_of::<Header>() <= 8 * std::mem::size_of::<*const ()>());
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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::<usize>() 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);
|
||||
|
@ -372,7 +372,7 @@ async fn multiple_waiters() {
|
||||
panic!("Tasks exited unexpectedly")
|
||||
},
|
||||
_ = barrier.wait() => {}
|
||||
};
|
||||
}
|
||||
|
||||
b.write_all(b"0").unwrap();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user