Skip to content

Commit

Permalink
chore: fix clippy warnings (#4727)
Browse files Browse the repository at this point in the history
  • Loading branch information
Darksonn committed May 31, 2022
1 parent 83d0e7f commit f948cd7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
2 changes: 1 addition & 1 deletion tokio/src/future/poll_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ where
type Output = T;

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<T> {
(&mut self.f)(cx)
(self.f)(cx)
}
}
25 changes: 12 additions & 13 deletions tokio/src/io/util/vec_with_initialized.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
use crate::io::ReadBuf;
use std::mem::MaybeUninit;

mod private {
pub trait Sealed {}

impl Sealed for Vec<u8> {}
impl Sealed for &mut Vec<u8> {}
}
/// Something that looks like a `Vec<u8>`.
///
/// # Safety
///
/// The implementor must guarantee that the vector returned by the
/// `as_mut` and `as_mut` methods do not change from one call to
/// another.
pub(crate) unsafe trait VecU8: AsRef<Vec<u8>> + AsMut<Vec<u8>> {}

/// A sealed trait that constrains the generic type parameter in `VecWithInitialized<V>`. That struct's safety relies
/// on certain invariants upheld by `Vec<u8>`.
pub(crate) trait VecU8: AsMut<Vec<u8>> + private::Sealed {}
unsafe impl VecU8 for Vec<u8> {}
unsafe impl VecU8 for &mut Vec<u8> {}

impl VecU8 for Vec<u8> {}
impl VecU8 for &mut Vec<u8> {}
/// This struct wraps a `Vec<u8>` or `&mut Vec<u8>`, combining it with a
/// `num_initialized`, which keeps track of the number of initialized bytes
/// in the unused capacity.
Expand Down Expand Up @@ -64,8 +63,8 @@ where
}

#[cfg(feature = "io-util")]
pub(crate) fn is_empty(&mut self) -> bool {
self.vec.as_mut().is_empty()
pub(crate) fn is_empty(&self) -> bool {
self.vec.as_ref().is_empty()
}

pub(crate) fn get_read_buf<'a>(&'a mut self) -> ReadBuf<'a> {
Expand Down
18 changes: 12 additions & 6 deletions tokio/src/net/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,22 @@ impl sealed::ToSocketAddrsPriv for &[SocketAddr] {
type Future = ReadyFuture<Self::Iter>;

fn to_socket_addrs(&self, _: sealed::Internal) -> Self::Future {
// Clippy doesn't like the `to_vec()` call here (as it will allocate,
// while `self.iter().copied()` would not), but it's actually necessary
// in order to ensure that the returned iterator is valid for the
// `'static` lifetime, which the borrowed `slice::Iter` iterator would
// not be.
#[inline]
fn slice_to_vec(addrs: &[SocketAddr]) -> Vec<SocketAddr> {
addrs.to_vec()
}

// This uses a helper method because clippy doesn't like the `to_vec()`
// call here (it will allocate, whereas `self.iter().copied()` would
// not), but it's actually necessary in order to ensure that the
// returned iterator is valid for the `'static` lifetime, which the
// borrowed `slice::Iter` iterator would not be.
//
// Note that we can't actually add an `allow` attribute for
// `clippy::unnecessary_to_owned` here, as Tokio's CI runs clippy lints
// on Rust 1.52 to avoid breaking LTS releases of Tokio. Users of newer
// Rust versions who see this lint should just ignore it.
let iter = self.to_vec().into_iter();
let iter = slice_to_vec(self).into_iter();
future::ready(Ok(iter))
}
}
Expand Down

0 comments on commit f948cd7

Please sign in to comment.