From 3e6c2eae79124d590cbb717e74d586231908a149 Mon Sep 17 00:00:00 2001 From: Igor Katson Date: Thu, 30 Jan 2025 11:24:43 +0000 Subject: [PATCH] Clippy --- crates/librqbit/src/http_api/handlers/mod.rs | 2 +- crates/librqbit/src/peer_connection.rs | 47 +++++++++++++------ crates/librqbit/src/session.rs | 2 +- crates/librqbit/src/torrent_state/live/mod.rs | 5 +- .../src/torrent_state/live/peer/mod.rs | 4 +- 5 files changed, 36 insertions(+), 24 deletions(-) diff --git a/crates/librqbit/src/http_api/handlers/mod.rs b/crates/librqbit/src/http_api/handlers/mod.rs index 5094974..ff3b43d 100644 --- a/crates/librqbit/src/http_api/handlers/mod.rs +++ b/crates/librqbit/src/http_api/handlers/mod.rs @@ -26,7 +26,7 @@ async fn h_api_root(parts: Parts) -> impl IntoResponse { .headers .get("Accept") .and_then(|h| h.to_str().ok()) - .map_or(false, |h| h.contains("text/html")) + .is_some_and(|h| h.contains("text/html")) { return Redirect::temporary("./web/").into_response(); } diff --git a/crates/librqbit/src/peer_connection.rs b/crates/librqbit/src/peer_connection.rs index 85c48bf..523da17 100644 --- a/crates/librqbit/src/peer_connection.rs +++ b/crates/librqbit/src/peer_connection.rs @@ -88,6 +88,16 @@ where } } +struct ManagePeerArgs { + handshake_supports_extended: bool, + read_buf: ReadBuf, + write_buf: Vec, + read: R, + write: W, + outgoing_chan: tokio::sync::mpsc::UnboundedReceiver, + have_broadcast: tokio::sync::broadcast::Receiver, +} + impl PeerConnection { pub fn new( addr: SocketAddr, @@ -147,21 +157,21 @@ impl PeerConnection { .context("error writing handshake")?; write_buf.clear(); - let h_supports_extended = handshake.supports_extended(); + let handshake_supports_extended = handshake.supports_extended(); self.handler.on_handshake(handshake)?; let (read, write) = conn.into_split(); - self.manage_peer( - h_supports_extended, + self.manage_peer(ManagePeerArgs { + handshake_supports_extended, read_buf, write_buf, read, write, outgoing_chan, have_broadcast, - ) + }) .await } @@ -201,7 +211,7 @@ impl PeerConnection { .read_handshake(&mut read, rwtimeout) .await .context("error reading handshake")?; - let h_supports_extended = h.supports_extended(); + let handshake_supports_extended = h.supports_extended(); trace!( peer_id=?Id20::new(h.peer_id), decoded_id=?try_decode_peer_id(Id20::new(h.peer_id)), @@ -217,28 +227,35 @@ impl PeerConnection { self.handler.on_handshake(h)?; - self.manage_peer( - h_supports_extended, + self.manage_peer(ManagePeerArgs { + handshake_supports_extended, read_buf, write_buf, read, write, outgoing_chan, have_broadcast, - ) + }) .await } async fn manage_peer( &self, - handshake_supports_extended: bool, - mut read_buf: ReadBuf, - mut write_buf: Vec, - mut read: impl tokio::io::AsyncRead + Unpin + Send, - mut write: impl tokio::io::AsyncWrite + Unpin + Send, - mut outgoing_chan: tokio::sync::mpsc::UnboundedReceiver, - mut have_broadcast: tokio::sync::broadcast::Receiver, + args: ManagePeerArgs< + impl tokio::io::AsyncRead + Send + Unpin, + impl tokio::io::AsyncWrite + Send + Unpin, + >, ) -> anyhow::Result<()> { + let ManagePeerArgs { + handshake_supports_extended, + mut read_buf, + mut write_buf, + mut read, + mut write, + mut outgoing_chan, + mut have_broadcast, + } = args; + use tokio::io::AsyncWriteExt; let rwtimeout = self diff --git a/crates/librqbit/src/session.rs b/crates/librqbit/src/session.rs index 6606f7a..0f6d2ab 100644 --- a/crates/librqbit/src/session.rs +++ b/crates/librqbit/src/session.rs @@ -996,7 +996,7 @@ impl Session { name, } = add_res; - let private = metadata.as_ref().map_or(false, |m| m.info.private); + let private = metadata.as_ref().is_some_and(|m| m.info.private); let make_peer_rx = || { self.make_peer_rx( diff --git a/crates/librqbit/src/torrent_state/live/mod.rs b/crates/librqbit/src/torrent_state/live/mod.rs index 0db812a..d7f9748 100644 --- a/crates/librqbit/src/torrent_state/live/mod.rs +++ b/crates/librqbit/src/torrent_state/live/mod.rs @@ -1253,10 +1253,7 @@ impl PeerHandler { /// /// If this returns, an existing in-flight piece was marked to be ours. fn try_steal_old_slow_piece(&self, threshold: f64) -> Option { - let my_avg_time = match self.counters.average_piece_download_time() { - Some(t) => t, - None => return None, - }; + let my_avg_time = self.counters.average_piece_download_time()?; let (stolen_idx, from_peer) = { let mut g = self.state.lock_write("try_steal_old_slow_piece"); diff --git a/crates/librqbit/src/torrent_state/live/peer/mod.rs b/crates/librqbit/src/torrent_state/live/peer/mod.rs index fc1d04f..5d80650 100644 --- a/crates/librqbit/src/torrent_state/live/peer/mod.rs +++ b/crates/librqbit/src/torrent_state/live/peer/mod.rs @@ -263,8 +263,6 @@ impl LivePeerState { } pub fn has_full_torrent(&self, total_pieces: usize) -> bool { - self.bitfield - .get(0..total_pieces) - .map_or(false, |s| s.all()) + self.bitfield.get(0..total_pieces).is_some_and(|s| s.all()) } }