From 0c16a9b0cb23bab5bcf18ec1f67acbc3183ab170 Mon Sep 17 00:00:00 2001 From: Yiyu Lin Date: Sat, 30 Mar 2024 17:24:38 +0800 Subject: [PATCH] chore: some code clean up and typo fix (#786) * chore: some code clean up and typo fix * more --------- Co-authored-by: hzlinyiyu --- poem-grpc/src/reflection.rs | 2 +- poem-openapi/src/openapi.rs | 2 +- poem/src/listener/mod.rs | 3 +-- poem/src/listener/rustls.rs | 10 +++++----- poem/src/middleware/cookie_jar_manager.rs | 2 +- poem/src/route/router.rs | 2 +- poem/src/route/router_method.rs | 6 +----- poem/src/web/accept.rs | 2 -- 8 files changed, 11 insertions(+), 18 deletions(-) diff --git a/poem-grpc/src/reflection.rs b/poem-grpc/src/reflection.rs index 52c0540133..f9e80d2400 100644 --- a/poem-grpc/src/reflection.rs +++ b/poem-grpc/src/reflection.rs @@ -138,7 +138,7 @@ impl Reflection { let fd_iter = std::mem::take(&mut this.file_descriptor_sets) .into_iter() .flat_map(|fds| fds.file.into_iter()); - let mut files = HashMap::new(); + let mut files = HashMap::with_capacity(fd_iter.size_hint().0); for fd in fd_iter { let fd = Arc::new(fd); diff --git a/poem-openapi/src/openapi.rs b/poem-openapi/src/openapi.rs index 2a48bf4e69..77efd0eab1 100644 --- a/poem-openapi/src/openapi.rs +++ b/poem-openapi/src/openapi.rs @@ -651,7 +651,7 @@ impl IntoEndpoint for OpenApiService { #[cfg(test)] mod tests { use super::*; - use crate::{types::Type, OpenApi}; + use crate::OpenApi; #[test] fn extra_response_headers() { diff --git a/poem/src/listener/mod.rs b/poem/src/listener/mod.rs index 68c4f169d7..1c9d0f093b 100644 --- a/poem/src/listener/mod.rs +++ b/poem/src/listener/mod.rs @@ -387,8 +387,7 @@ impl Listener for BoxListener { #[cfg(test)] mod tests { - use super::{AcceptorExt, *}; - use crate::listener::TcpListener; + use super::*; #[tokio::test] async fn combined_listener() { diff --git a/poem/src/listener/rustls.rs b/poem/src/listener/rustls.rs index 93277f947c..5b2be369c2 100644 --- a/poem/src/listener/rustls.rs +++ b/poem/src/listener/rustls.rs @@ -222,10 +222,10 @@ impl RustlsConfig { .map(|fallback| fallback.create_certificate_key()) .transpose()? .map(Arc::new); - let mut certifcate_keys = HashMap::new(); + let mut certificate_keys = HashMap::with_capacity(self.certificates.len()); for (name, certificate) in &self.certificates { - certifcate_keys.insert( + certificate_keys.insert( name.clone(), Arc::new(certificate.create_certificate_key()?), ); @@ -252,7 +252,7 @@ impl RustlsConfig { }; let mut server_config = builder.with_cert_resolver(Arc::new(ResolveServerCert { - certifcate_keys, + certificate_keys, fallback, })); server_config.alpn_protocols = vec!["h2".into(), "http/1.1".into()]; @@ -401,7 +401,7 @@ where #[derive(Debug)] struct ResolveServerCert { - certifcate_keys: HashMap>, + certificate_keys: HashMap>, fallback: Option>, } @@ -409,7 +409,7 @@ impl ResolvesServerCert for ResolveServerCert { fn resolve(&self, client_hello: ClientHello) -> Option> { client_hello .server_name() - .and_then(|name| self.certifcate_keys.get(name).cloned()) + .and_then(|name| self.certificate_keys.get(name).cloned()) .or_else(|| self.fallback.clone()) } } diff --git a/poem/src/middleware/cookie_jar_manager.rs b/poem/src/middleware/cookie_jar_manager.rs index 5a4da25478..24a153a61a 100644 --- a/poem/src/middleware/cookie_jar_manager.rs +++ b/poem/src/middleware/cookie_jar_manager.rs @@ -55,7 +55,7 @@ impl Endpoint for CookieJarManagerEndpoint { async fn call(&self, mut req: Request) -> Result { if req.state().cookie_jar.is_none() { let mut cookie_jar = CookieJar::extract_from_headers(req.headers()); - cookie_jar.key = self.key.clone(); + cookie_jar.key.clone_from(&self.key); req.state_mut().cookie_jar = Some(cookie_jar.clone()); let mut resp = self.inner.call(req).await?.into_response(); cookie_jar.append_delta_to_headers(resp.headers_mut()); diff --git a/poem/src/route/router.rs b/poem/src/route/router.rs index 490f978069..7a6149a468 100644 --- a/poem/src/route/router.rs +++ b/poem/src/route/router.rs @@ -387,7 +387,7 @@ fn normalize_path(path: &str) -> String { #[cfg(test)] mod tests { use futures_util::lock::Mutex; - use http::{StatusCode, Uri}; + use http::StatusCode; use super::*; use crate::{endpoint::make_sync, handler, test::TestClient, Error}; diff --git a/poem/src/route/router_method.rs b/poem/src/route/router_method.rs index 778fadfd8d..566134c5de 100644 --- a/poem/src/route/router_method.rs +++ b/poem/src/route/router_method.rs @@ -281,11 +281,7 @@ where #[cfg(test)] mod tests { use super::*; - use crate::{ - handler, - http::{Method, StatusCode}, - test::TestClient, - }; + use crate::{handler, http::StatusCode, test::TestClient}; #[tokio::test] async fn method_not_allowed() { diff --git a/poem/src/web/accept.rs b/poem/src/web/accept.rs index 142b9f10ac..ed4cfa3d95 100644 --- a/poem/src/web/accept.rs +++ b/poem/src/web/accept.rs @@ -36,8 +36,6 @@ impl<'a> FromRequest<'a> for Accept { mod tests { use std::str::FromStr; - use http::header; - use super::*; #[tokio::test]