Skip to content

Commit 1781906

Browse files
authored
tokio: update code according to new MSRV (#6764)
1 parent 6ad1912 commit 1781906

File tree

11 files changed

+14
-62
lines changed

11 files changed

+14
-62
lines changed

tokio-util/src/codec/length_delimited.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,28 +1049,13 @@ impl Builder {
10491049
}
10501050

10511051
fn adjust_max_frame_len(&mut self) {
1052-
// This function is basically `std::u64::saturating_add_signed`. Since it
1053-
// requires MSRV 1.66, its implementation is copied here.
1054-
//
1055-
// TODO: use the method from std when MSRV becomes >= 1.66
1056-
fn saturating_add_signed(num: u64, rhs: i64) -> u64 {
1057-
let (res, overflow) = num.overflowing_add(rhs as u64);
1058-
if overflow == (rhs < 0) {
1059-
res
1060-
} else if overflow {
1061-
u64::MAX
1062-
} else {
1063-
0
1064-
}
1065-
}
1066-
10671052
// Calculate the maximum number that can be represented using `length_field_len` bytes.
10681053
let max_number = match 1u64.checked_shl((8 * self.length_field_len) as u32) {
10691054
Some(shl) => shl - 1,
10701055
None => u64::MAX,
10711056
};
10721057

1073-
let max_allowed_len = saturating_add_signed(max_number, self.length_adjustment as i64);
1058+
let max_allowed_len = max_number.saturating_add_signed(self.length_adjustment as i64);
10741059

10751060
if self.max_frame_len as u64 > max_allowed_len {
10761061
self.max_frame_len = usize::try_from(max_allowed_len).unwrap_or(usize::MAX);

tokio/src/fs/try_exists.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,5 @@ use std::path::Path;
2424
/// ```
2525
pub async fn try_exists(path: impl AsRef<Path>) -> io::Result<bool> {
2626
let path = path.as_ref().to_owned();
27-
// std's Path::try_exists is not available for current Rust min supported version.
28-
// Current implementation is based on its internal implementation instead.
29-
match asyncify(move || std::fs::metadata(path)).await {
30-
Ok(_) => Ok(true),
31-
Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(false),
32-
Err(error) => Err(error),
33-
}
27+
asyncify(move || path.try_exists()).await
3428
}

tokio/src/io/util/read_int.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ use std::future::Future;
66
use std::io;
77
use std::io::ErrorKind::UnexpectedEof;
88
use std::marker::PhantomPinned;
9-
use std::mem::size_of;
109
use std::pin::Pin;
1110
use std::task::{Context, Poll};
1211

1312
macro_rules! reader {
1413
($name:ident, $ty:ty, $reader:ident) => {
15-
reader!($name, $ty, $reader, size_of::<$ty>());
14+
reader!($name, $ty, $reader, std::mem::size_of::<$ty>());
1615
};
1716
($name:ident, $ty:ty, $reader:ident, $bytes:expr) => {
1817
pin_project! {

tokio/src/io/util/write_int.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ use pin_project_lite::pin_project;
55
use std::future::Future;
66
use std::io;
77
use std::marker::PhantomPinned;
8-
use std::mem::size_of;
98
use std::pin::Pin;
109
use std::task::{Context, Poll};
1110

1211
macro_rules! writer {
1312
($name:ident, $ty:ty, $writer:ident) => {
14-
writer!($name, $ty, $writer, size_of::<$ty>());
13+
writer!($name, $ty, $writer, std::mem::size_of::<$ty>());
1514
};
1615
($name:ident, $ty:ty, $writer:ident, $bytes:expr) => {
1716
pin_project! {

tokio/src/runtime/blocking/pool.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ impl Spawner {
461461
shutdown_tx: shutdown::Sender,
462462
rt: &Handle,
463463
id: usize,
464-
) -> std::io::Result<thread::JoinHandle<()>> {
464+
) -> io::Result<thread::JoinHandle<()>> {
465465
let mut builder = thread::Builder::new().name((self.inner.thread_name)());
466466

467467
if let Some(stack_size) = self.inner.stack_size {
@@ -497,8 +497,8 @@ cfg_unstable_metrics! {
497497

498498
// Tells whether the error when spawning a thread is temporary.
499499
#[inline]
500-
fn is_temporary_os_thread_error(error: &std::io::Error) -> bool {
501-
matches!(error.kind(), std::io::ErrorKind::WouldBlock)
500+
fn is_temporary_os_thread_error(error: &io::Error) -> bool {
501+
matches!(error.kind(), io::ErrorKind::WouldBlock)
502502
}
503503

504504
impl Inner {

tokio/src/runtime/scheduler/multi_thread/worker.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,7 @@ impl Core {
10141014
.tuned_global_queue_interval(&worker.handle.shared.config);
10151015

10161016
// Smooth out jitter
1017-
if abs_diff(self.global_queue_interval, next) > 2 {
1017+
if u32::abs_diff(self.global_queue_interval, next) > 2 {
10181018
self.global_queue_interval = next;
10191019
}
10201020
}
@@ -1249,12 +1249,3 @@ fn with_current<R>(f: impl FnOnce(Option<&Context>) -> R) -> R {
12491249
_ => f(None),
12501250
})
12511251
}
1252-
1253-
// `u32::abs_diff` is not available on Tokio's MSRV.
1254-
fn abs_diff(a: u32, b: u32) -> u32 {
1255-
if a > b {
1256-
a - b
1257-
} else {
1258-
b - a
1259-
}
1260-
}

tokio/src/runtime/scheduler/multi_thread_alt/worker.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,7 +1292,7 @@ impl Worker {
12921292
let next = core.stats.tuned_global_queue_interval(&cx.shared().config);
12931293

12941294
// Smooth out jitter
1295-
if abs_diff(self.global_queue_interval, next) > 2 {
1295+
if u32::abs_diff(self.global_queue_interval, next) > 2 {
12961296
self.global_queue_interval = next;
12971297
}
12981298
}
@@ -1592,12 +1592,3 @@ fn with_current<R>(f: impl FnOnce(Option<&Context>) -> R) -> R {
15921592
_ => f(None),
15931593
})
15941594
}
1595-
1596-
// `u32::abs_diff` is not available on Tokio's MSRV.
1597-
fn abs_diff(a: u32, b: u32) -> u32 {
1598-
if a > b {
1599-
a - b
1600-
} else {
1601-
b - a
1602-
}
1603-
}

tokio/src/runtime/task/core.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,5 @@ impl Trailer {
489489
#[test]
490490
#[cfg(not(loom))]
491491
fn header_lte_cache_line() {
492-
use std::mem::size_of;
493-
494-
assert!(size_of::<Header>() <= 8 * size_of::<*const ()>());
492+
assert!(std::mem::size_of::<Header>() <= 8 * std::mem::size_of::<*const ()>());
495493
}

tokio/src/task/spawn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::future::Future;
55

66
cfg_rt! {
77
/// Spawns a new asynchronous task, returning a
8-
/// [`JoinHandle`](super::JoinHandle) for it.
8+
/// [`JoinHandle`](JoinHandle) for it.
99
///
1010
/// The provided future will start running in the background immediately
1111
/// when `spawn` is called, even if you don't await the returned

tokio/src/util/bit.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ impl Pack {
1616

1717
/// Value is packed in the `width` more-significant bits.
1818
pub(crate) const fn then(&self, width: u32) -> Pack {
19-
let shift = pointer_width() - self.mask.leading_zeros();
19+
let shift = usize::BITS - self.mask.leading_zeros();
2020
let mask = mask_for(width) << shift;
2121

2222
Pack { mask, shift }
2323
}
2424

2525
/// Width, in bits, dedicated to storing the value.
2626
pub(crate) const fn width(&self) -> u32 {
27-
pointer_width() - (self.mask >> self.shift).leading_zeros()
27+
usize::BITS - (self.mask >> self.shift).leading_zeros()
2828
}
2929

3030
/// Max representable value.
@@ -52,11 +52,6 @@ impl fmt::Debug for Pack {
5252
}
5353
}
5454

55-
/// Returns the width of a pointer in bits.
56-
pub(crate) const fn pointer_width() -> u32 {
57-
std::mem::size_of::<usize>() as u32 * 8
58-
}
59-
6055
/// Returns a `usize` with the right-most `n` bits set.
6156
pub(crate) const fn mask_for(n: u32) -> usize {
6257
let shift = 1usize.wrapping_shl(n - 1);

tokio/tests/io_async_fd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ async fn multiple_waiters() {
372372
panic!("Tasks exited unexpectedly")
373373
},
374374
_ = barrier.wait() => {}
375-
};
375+
}
376376

377377
b.write_all(b"0").unwrap();
378378

0 commit comments

Comments
 (0)