1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-07-01 00:44:26 +02:00

Various refactorings (#2281)

Co-authored-by: Rob Ede <robjtede@icloud.com>
This commit is contained in:
Igor Aleksanov
2021-06-26 18:33:43 +04:00
committed by GitHub
parent 5eba95b731
commit 262c6bc828
30 changed files with 98 additions and 121 deletions

View File

@ -131,9 +131,9 @@ where
let service = endpoint_fut.await?;
// populate app data container from (async) data factories.
async_data_factories.iter().for_each(|factory| {
for factory in &async_data_factories {
factory.create(&mut app_data);
});
}
Ok(AppInitService {
service,

View File

@ -410,41 +410,33 @@ impl ContentDisposition {
/// Return the value of *name* if exists.
pub fn get_name(&self) -> Option<&str> {
self.parameters.iter().filter_map(|p| p.as_name()).next()
self.parameters.iter().find_map(DispositionParam::as_name)
}
/// Return the value of *filename* if exists.
pub fn get_filename(&self) -> Option<&str> {
self.parameters
.iter()
.filter_map(|p| p.as_filename())
.next()
.find_map(DispositionParam::as_filename)
}
/// Return the value of *filename\** if exists.
pub fn get_filename_ext(&self) -> Option<&ExtendedValue> {
self.parameters
.iter()
.filter_map(|p| p.as_filename_ext())
.next()
.find_map(DispositionParam::as_filename_ext)
}
/// Return the value of the parameter which the `name` matches.
pub fn get_unknown(&self, name: impl AsRef<str>) -> Option<&str> {
let name = name.as_ref();
self.parameters
.iter()
.filter_map(|p| p.as_unknown(name))
.next()
self.parameters.iter().find_map(|p| p.as_unknown(name))
}
/// Return the value of the extended parameter which the `name` matches.
pub fn get_unknown_ext(&self, name: impl AsRef<str>) -> Option<&ExtendedValue> {
let name = name.as_ref();
self.parameters
.iter()
.filter_map(|p| p.as_unknown_ext(name))
.next()
self.parameters.iter().find_map(|p| p.as_unknown_ext(name))
}
}

View File

@ -200,8 +200,7 @@ impl AcceptEncoding {
let mut encodings = raw
.replace(' ', "")
.split(',')
.map(|l| AcceptEncoding::new(l))
.flatten()
.filter_map(|l| AcceptEncoding::new(l))
.collect::<Vec<_>>();
encodings.sort();

View File

@ -111,11 +111,7 @@ impl HttpRequest {
/// E.g., id=10
#[inline]
pub fn query_string(&self) -> &str {
if let Some(query) = self.uri().query().as_ref() {
query
} else {
""
}
self.uri().query().unwrap_or_default()
}
/// Get a reference to the Path parameters.

View File

@ -465,7 +465,7 @@ impl Service<ServiceRequest> for ResourceService {
actix_service::always_ready!();
fn call(&self, mut req: ServiceRequest) -> Self::Future {
for route in self.routes.iter() {
for route in &self.routes {
if route.check(&mut req) {
return route.call(req);
}

View File

@ -406,7 +406,7 @@ impl HttpResponseBuilder {
return None;
}
self.res.as_mut().map(|res| res.head_mut())
self.res.as_mut().map(Response::head_mut)
}
}

View File

@ -292,15 +292,15 @@ where
let c = cfg.lock().unwrap();
let host = c.host.clone().unwrap_or_else(|| format!("{}", addr));
let svc = HttpService::build()
let mut svc = HttpService::build()
.keep_alive(c.keep_alive)
.client_timeout(c.client_timeout)
.local_addr(addr);
let svc = if let Some(handler) = on_connect_fn.clone() {
svc.on_connect_ext(move |io: &_, ext: _| (handler)(io as &dyn Any, ext))
} else {
svc
if let Some(handler) = on_connect_fn.clone() {
svc = svc.on_connect_ext(move |io: &_, ext: _| {
(handler)(io as &dyn Any, ext)
})
};
let fac = factory()
@ -461,17 +461,15 @@ where
}
}
if !success {
if let Some(e) = err.take() {
Err(e)
} else {
Err(io::Error::new(
io::ErrorKind::Other,
"Can not bind to address.",
))
}
} else {
if success {
Ok(sockets)
} else if let Some(e) = err.take() {
Err(e)
} else {
Err(io::Error::new(
io::ErrorKind::Other,
"Can not bind to address.",
))
}
}
@ -537,15 +535,14 @@ where
);
fn_service(|io: UnixStream| async { Ok((io, Protocol::Http1, None)) }).and_then({
let svc = HttpService::build()
let mut svc = HttpService::build()
.keep_alive(c.keep_alive)
.client_timeout(c.client_timeout);
let svc = if let Some(handler) = on_connect_fn.clone() {
svc.on_connect_ext(move |io: &_, ext: _| (&*handler)(io as &dyn Any, ext))
} else {
svc
};
if let Some(handler) = on_connect_fn.clone() {
svc = svc
.on_connect_ext(move |io: &_, ext: _| (&*handler)(io as &dyn Any, ext));
}
let fac = factory()
.into_factory()

View File

@ -167,11 +167,7 @@ impl ServiceRequest {
/// E.g., id=10
#[inline]
pub fn query_string(&self) -> &str {
if let Some(query) = self.uri().query().as_ref() {
query
} else {
""
}
self.uri().query().unwrap_or_default()
}
/// Peer socket address.

View File

@ -1,6 +1,7 @@
//! For URL encoded form helper documentation, see [`Form`].
use std::{
borrow::Cow,
fmt,
future::Future,
ops,
@ -384,7 +385,7 @@ where
} else {
let body = encoding
.decode_without_bom_handling_and_without_replacement(&body)
.map(|s| s.into_owned())
.map(Cow::into_owned)
.ok_or(UrlencodedError::Encoding)?;
serde_urlencoded::from_str::<T>(&body).map_err(UrlencodedError::Parse)

View File

@ -103,8 +103,7 @@ where
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
let error_handler = req
.app_data::<Self::Config>()
.map(|c| c.ehandler.clone())
.unwrap_or(None);
.and_then(|c| c.ehandler.clone());
ready(
de::Deserialize::deserialize(PathDeserializer::new(req.match_info()))

View File

@ -1,6 +1,7 @@
//! Basic binary and string payload extractors.
use std::{
borrow::Cow,
future::Future,
pin::Pin,
str,
@ -190,7 +191,7 @@ fn bytes_to_string(body: Bytes, encoding: &'static Encoding) -> Result<String, E
} else {
Ok(encoding
.decode_without_bom_handling_and_without_replacement(&body)
.map(|s| s.into_owned())
.map(Cow::into_owned)
.ok_or_else(|| ErrorBadRequest("Can not decode body"))?)
}
}

View File

@ -119,8 +119,7 @@ where
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
let error_handler = req
.app_data::<Self::Config>()
.map(|c| c.err_handler.clone())
.unwrap_or(None);
.and_then(|c| c.err_handler.clone());
serde_urlencoded::from_str::<T>(req.query_string())
.map(|val| ok(Query(val)))