From 3816cbf4e03093b82e2cd871ddaf58b9104252c2 Mon Sep 17 00:00:00 2001 From: Emmanuel Bosquet Date: Wed, 24 Apr 2024 17:47:18 +0200 Subject: [PATCH] apply clippy suggestions --- bin/Cargo.toml | 1 - lib/src/http.rs | 2 +- lib/src/https.rs | 2 +- lib/src/protocol/kawa_h1/answers.rs | 2 +- lib/src/protocol/kawa_h1/diagnostics.rs | 11 ++++------- lib/src/protocol/kawa_h1/mod.rs | 10 +++++----- lib/src/server.rs | 4 ++-- lib/src/tcp.rs | 2 +- lib/src/timer.rs | 22 ++++++++++++---------- 9 files changed, 27 insertions(+), 29 deletions(-) diff --git a/bin/Cargo.toml b/bin/Cargo.toml index bc042aa5b..a43e3cf94 100644 --- a/bin/Cargo.toml +++ b/bin/Cargo.toml @@ -30,7 +30,6 @@ paw = "^1.0.0" serde = { version = "^1.0.197", features = ["derive"] } serde_json = "^1.0.116" prost = "^0.12.4" -# time = "^0.3.36" tempfile = "^3.10.1" termion = "^3.0.0" thiserror = "^1.0.58" diff --git a/lib/src/http.rs b/lib/src/http.rs index 716bea2db..b1be32ec9 100644 --- a/lib/src/http.rs +++ b/lib/src/http.rs @@ -580,7 +580,7 @@ impl HttpProxy { .listeners .values() .find(|listener| listener.borrow().address == address) - .ok_or(ProxyError::NoListenerFound(address.clone()))?; + .ok_or(ProxyError::NoListenerFound(address))?; let mut owned = listener.borrow_mut(); diff --git a/lib/src/https.rs b/lib/src/https.rs index 63ea28a83..abe29af4a 100644 --- a/lib/src/https.rs +++ b/lib/src/https.rs @@ -987,7 +987,7 @@ impl HttpsProxy { .listeners .values() .find(|listener| listener.borrow().address == address) - .ok_or(ProxyError::NoListenerFound(address.clone()))?; + .ok_or(ProxyError::NoListenerFound(address))?; let mut owned = listener.borrow_mut(); diff --git a/lib/src/protocol/kawa_h1/answers.rs b/lib/src/protocol/kawa_h1/answers.rs index 6c4ab2056..f589a972a 100644 --- a/lib/src/protocol/kawa_h1/answers.rs +++ b/lib/src/protocol/kawa_h1/answers.rs @@ -114,7 +114,7 @@ impl Template { .collect::>(); let answer = answer .replace("\r\n", "\n") - .replace("\n", "\r\n") + .replace('\n', "\r\n") .into_bytes(); let len = answer.len(); diff --git a/lib/src/protocol/kawa_h1/diagnostics.rs b/lib/src/protocol/kawa_h1/diagnostics.rs index 3cb787748..be9fcd431 100644 --- a/lib/src/protocol/kawa_h1/diagnostics.rs +++ b/lib/src/protocol/kawa_h1/diagnostics.rs @@ -16,11 +16,7 @@ fn hex_dump(buffer: &[u8], window: usize, start: usize, end: usize) -> String { for c in slice { let _ = write!(result, " {c:02x}"); } - result.push_str( - &std::iter::repeat(' ') - .take((window + start - end) * 3 + 4) - .collect::(), - ); + result.push_str(&" ".repeat((window + start - end) * 3 + 4)); result.push_str(&String::from_utf8_lossy(slice).escape_debug().to_string()); } else { let slice1 = &buffer[start..start + window - 1]; @@ -112,10 +108,11 @@ Invalid: pub fn diagnostic_413_507(parsing_phase: ParsingPhase) -> String { match parsing_phase { kawa::ParsingPhase::StatusLine => { - format!("Request line is too long. Note that an URL should not exceed 2083 characters.") + "Request line is too long. Note that an URL should not exceed 2083 characters." + .to_string() } kawa::ParsingPhase::Headers | kawa::ParsingPhase::Cookies { .. } => { - format!("Headers are too long. All headers should fit in a single buffer.") + "Headers are too long. All headers should fit in a single buffer.".to_string() } phase => format!("Unexpected parsing phase: {phase:?}"), } diff --git a/lib/src/protocol/kawa_h1/mod.rs b/lib/src/protocol/kawa_h1/mod.rs index 22e55911b..abceb7715 100644 --- a/lib/src/protocol/kawa_h1/mod.rs +++ b/lib/src/protocol/kawa_h1/mod.rs @@ -1212,7 +1212,7 @@ impl Http SessionState // we do not have a complete answer TimeoutStatus::Request => { self.set_answer(DefaultAnswer::Answer408 { - duration: self.container_frontend_timeout.duration_fmt(), + duration: self.container_frontend_timeout.to_string(), }); self.writable(metrics) } @@ -1854,7 +1854,7 @@ impl SessionState // this case is ambiguous, as it is the frontend timeout that triggers while we were waiting for response // the timeout responsibility should have switched before self.set_answer(DefaultAnswer::Answer504 { - duration: self.container_backend_timeout.duration_fmt(), + duration: self.container_backend_timeout.to_string(), }); self.writable(metrics) } @@ -1875,13 +1875,13 @@ impl SessionState "got backend timeout while waiting for a request, this should not happen" ); self.set_answer(DefaultAnswer::Answer504 { - duration: self.container_backend_timeout.duration_fmt(), + duration: self.container_backend_timeout.to_string(), }); self.writable(metrics) } TimeoutStatus::WaitingForResponse => { self.set_answer(DefaultAnswer::Answer504 { - duration: self.container_backend_timeout.duration_fmt(), + duration: self.container_backend_timeout.to_string(), }); self.writable(metrics) } diff --git a/lib/src/server.rs b/lib/src/server.rs index 50879a024..5a1d1488e 100644 --- a/lib/src/server.rs +++ b/lib/src/server.rs @@ -606,7 +606,7 @@ impl Server { } } - fn reset_loop_time_and_get_timeout(&mut self) -> Option { + fn reset_loop_time_and_get_timeout(&mut self) -> Option { let now = Instant::now(); time!("event_loop_time", (now - self.loop_start).as_millis()); @@ -632,7 +632,7 @@ impl Server { }; self.loop_start = now; - timeout.and_then(|t| std::time::Duration::try_from(t).ok()) + timeout } /// Returns true if hardstop diff --git a/lib/src/tcp.rs b/lib/src/tcp.rs index 4e4c51209..669b6d5fc 100644 --- a/lib/src/tcp.rs +++ b/lib/src/tcp.rs @@ -1064,7 +1064,7 @@ impl TcpListener { registry .register(&mut listener, self.token, Interest::READABLE) - .map_err(|io_err| ProxyError::RegisterListener(io_err))?; + .map_err(ProxyError::RegisterListener)?; self.listener = Some(listener); self.active = true; diff --git a/lib/src/timer.rs b/lib/src/timer.rs index 0a7e632e1..9d9b98521 100644 --- a/lib/src/timer.rs +++ b/lib/src/timer.rs @@ -3,7 +3,9 @@ //! code imported from mio-extras //! License: MIT or Apache 2.0 use std::{ - cmp, iter, + cmp, + fmt::Display, + iter, time::{Duration, Instant}, u64, usize, }; @@ -146,11 +148,6 @@ impl TimeoutContainer { self.duration } - /// format timeout duration - pub fn duration_fmt(&self) -> String { - format!("{:?}", self.duration) - } - pub fn cancel(&mut self) -> bool { match self.timeout.take() { None => { @@ -187,10 +184,16 @@ impl TimeoutContainer { } } +impl Display for TimeoutContainer { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{:?}", self.duration) + } +} + impl std::ops::Drop for TimeoutContainer { fn drop(&mut self) { if self.cancel() { - debug!("Cancel a dangling timeout that haven't be handled in session lifecycle, token ({:?}), duration {}", self.token, self.duration_fmt()); + debug!("Cancel a dangling timeout that haven't be handled in session lifecycle, token ({:?}), duration {}", self.token, self); } } } @@ -475,9 +478,8 @@ impl Timer { } pub fn next_poll_date(&self) -> Option { - self.next_tick().map(|tick| { - self.start + Duration::from_millis(self.tick_ms.saturating_mul(tick) as u64) - }) + self.next_tick() + .map(|tick| self.start + Duration::from_millis(self.tick_ms.saturating_mul(tick))) } fn slot_for(&self, tick: Tick) -> usize {