1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-24 16:02:59 +01:00

use read only ref for FromRequest; remove unnecessary static

This commit is contained in:
Nikolay Kim 2018-05-02 06:07:30 -07:00
parent a1958deaae
commit 76b644365f
6 changed files with 40 additions and 46 deletions

View File

@ -805,8 +805,7 @@ mod tests {
#[test] #[test]
fn test_backtrace() { fn test_backtrace() {
let orig = ErrorBadRequest("err"); let e = ErrorBadRequest("err");
let e: Error = orig.into();
assert!(e.backtrace().is_some()); assert!(e.backtrace().is_some());
} }

View File

@ -99,13 +99,12 @@ impl<T> Path<T> {
impl<T, S> FromRequest<S> for Path<T> impl<T, S> FromRequest<S> for Path<T>
where where
T: DeserializeOwned, T: DeserializeOwned,
S: 'static,
{ {
type Config = (); type Config = ();
type Result = Result<Self, Error>; type Result = Result<Self, Error>;
#[inline] #[inline]
fn from_request(req: &mut HttpRequest<S>, _: &Self::Config) -> Self::Result { fn from_request(req: &HttpRequest<S>, _: &Self::Config) -> Self::Result {
let req = req.clone(); let req = req.clone();
de::Deserialize::deserialize(PathDeserializer::new(&req)) de::Deserialize::deserialize(PathDeserializer::new(&req))
.map_err(|e| e.into()) .map_err(|e| e.into())
@ -167,13 +166,12 @@ impl<T> Query<T> {
impl<T, S> FromRequest<S> for Query<T> impl<T, S> FromRequest<S> for Query<T>
where where
T: de::DeserializeOwned, T: de::DeserializeOwned,
S: 'static,
{ {
type Config = (); type Config = ();
type Result = Result<Self, Error>; type Result = Result<Self, Error>;
#[inline] #[inline]
fn from_request(req: &mut HttpRequest<S>, _: &Self::Config) -> Self::Result { fn from_request(req: &HttpRequest<S>, _: &Self::Config) -> Self::Result {
let req = req.clone(); let req = req.clone();
serde_urlencoded::from_str::<T>(req.query_string()) serde_urlencoded::from_str::<T>(req.query_string())
.map_err(|e| e.into()) .map_err(|e| e.into())
@ -241,7 +239,7 @@ where
type Result = Box<Future<Item = Self, Error = Error>>; type Result = Box<Future<Item = Self, Error = Error>>;
#[inline] #[inline]
fn from_request(req: &mut HttpRequest<S>, cfg: &Self::Config) -> Self::Result { fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result {
Box::new( Box::new(
UrlEncoded::new(req.clone()) UrlEncoded::new(req.clone())
.limit(cfg.limit) .limit(cfg.limit)
@ -326,7 +324,7 @@ impl<S: 'static> FromRequest<S> for Bytes {
type Result = Result<Box<Future<Item = Self, Error = Error>>, Error>; type Result = Result<Box<Future<Item = Self, Error = Error>>, Error>;
#[inline] #[inline]
fn from_request(req: &mut HttpRequest<S>, cfg: &Self::Config) -> Self::Result { fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result {
// check content-type // check content-type
cfg.check_mimetype(req)?; cfg.check_mimetype(req)?;
@ -370,7 +368,7 @@ impl<S: 'static> FromRequest<S> for String {
type Result = Result<Box<Future<Item = String, Error = Error>>, Error>; type Result = Result<Box<Future<Item = String, Error = Error>>, Error>;
#[inline] #[inline]
fn from_request(req: &mut HttpRequest<S>, cfg: &Self::Config) -> Self::Result { fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result {
// check content-type // check content-type
cfg.check_mimetype(req)?; cfg.check_mimetype(req)?;
@ -471,7 +469,7 @@ mod tests {
req.payload_mut() req.payload_mut()
.unread_data(Bytes::from_static(b"hello=world")); .unread_data(Bytes::from_static(b"hello=world"));
match Bytes::from_request(&mut req, &cfg) match Bytes::from_request(&req, &cfg)
.unwrap() .unwrap()
.poll() .poll()
.unwrap() .unwrap()
@ -490,7 +488,7 @@ mod tests {
req.payload_mut() req.payload_mut()
.unread_data(Bytes::from_static(b"hello=world")); .unread_data(Bytes::from_static(b"hello=world"));
match String::from_request(&mut req, &cfg) match String::from_request(&req, &cfg)
.unwrap() .unwrap()
.poll() .poll()
.unwrap() .unwrap()
@ -514,10 +512,7 @@ mod tests {
let mut cfg = FormConfig::default(); let mut cfg = FormConfig::default();
cfg.limit(4096); cfg.limit(4096);
match Form::<Info>::from_request(&mut req, &cfg) match Form::<Info>::from_request(&req, &cfg).poll().unwrap() {
.poll()
.unwrap()
{
Async::Ready(s) => { Async::Ready(s) => {
assert_eq!(s.hello, "world"); assert_eq!(s.hello, "world");
} }
@ -574,29 +569,29 @@ mod tests {
let (router, _) = Router::new("", ServerSettings::default(), routes); let (router, _) = Router::new("", ServerSettings::default(), routes);
assert!(router.recognize(&mut req).is_some()); assert!(router.recognize(&mut req).is_some());
let s = Path::<MyStruct>::from_request(&mut req, &()).unwrap(); let s = Path::<MyStruct>::from_request(&req, &()).unwrap();
assert_eq!(s.key, "name"); assert_eq!(s.key, "name");
assert_eq!(s.value, "user1"); assert_eq!(s.value, "user1");
let s = Path::<(String, String)>::from_request(&mut req, &()).unwrap(); let s = Path::<(String, String)>::from_request(&req, &()).unwrap();
assert_eq!(s.0, "name"); assert_eq!(s.0, "name");
assert_eq!(s.1, "user1"); assert_eq!(s.1, "user1");
let s = Query::<Id>::from_request(&mut req, &()).unwrap(); let s = Query::<Id>::from_request(&req, &()).unwrap();
assert_eq!(s.id, "test"); assert_eq!(s.id, "test");
let mut req = TestRequest::with_uri("/name/32/").finish(); let mut req = TestRequest::with_uri("/name/32/").finish();
assert!(router.recognize(&mut req).is_some()); assert!(router.recognize(&mut req).is_some());
let s = Path::<Test2>::from_request(&mut req, &()).unwrap(); let s = Path::<Test2>::from_request(&req, &()).unwrap();
assert_eq!(s.as_ref().key, "name"); assert_eq!(s.as_ref().key, "name");
assert_eq!(s.value, 32); assert_eq!(s.value, 32);
let s = Path::<(String, u8)>::from_request(&mut req, &()).unwrap(); let s = Path::<(String, u8)>::from_request(&req, &()).unwrap();
assert_eq!(s.0, "name"); assert_eq!(s.0, "name");
assert_eq!(s.1, 32); assert_eq!(s.1, 32);
let res = Path::<Vec<String>>::from_request(&mut req, &()).unwrap(); let res = Path::<Vec<String>>::from_default(&req).unwrap();
assert_eq!(res[0], "name".to_owned()); assert_eq!(res[0], "name".to_owned());
assert_eq!(res[1], "32".to_owned()); assert_eq!(res[1], "32".to_owned());
} }

View File

@ -36,10 +36,7 @@ pub trait Responder {
/// Trait implemented by types that can be extracted from request. /// Trait implemented by types that can be extracted from request.
/// ///
/// Types that implement this trait can be used with `Route::with()` method. /// Types that implement this trait can be used with `Route::with()` method.
pub trait FromRequest<S>: Sized pub trait FromRequest<S>: Sized {
where
S: 'static,
{
/// Configuration for conversion process /// Configuration for conversion process
type Config: Default; type Config: Default;
@ -47,7 +44,14 @@ where
type Result: Into<Reply<Self>>; type Result: Into<Reply<Self>>;
/// Convert request to a Self /// Convert request to a Self
fn from_request(req: &mut HttpRequest<S>, cfg: &Self::Config) -> Self::Result; fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result;
/// Convert request to a Self
///
/// This method uses default extractor configuration
fn from_default(req: &HttpRequest<S>) -> Self::Result {
Self::from_request(req, &Self::Config::default())
}
} }
/// Combines two different responder types into a single type /// Combines two different responder types into a single type
@ -505,12 +509,12 @@ impl<S> Deref for State<S> {
} }
} }
impl<S: 'static> FromRequest<S> for State<S> { impl<S> FromRequest<S> for State<S> {
type Config = (); type Config = ();
type Result = State<S>; type Result = State<S>;
#[inline] #[inline]
fn from_request(req: &mut HttpRequest<S>, _: &Self::Config) -> Self::Result { fn from_request(req: &HttpRequest<S>, _: &Self::Config) -> Self::Result {
State(req.clone()).into() State(req.clone())
} }
} }

View File

@ -500,12 +500,12 @@ impl<S> Clone for HttpRequest<S> {
} }
} }
impl<S: 'static> FromRequest<S> for HttpRequest<S> { impl<S> FromRequest<S> for HttpRequest<S> {
type Config = (); type Config = ();
type Result = Self; type Result = Self;
#[inline] #[inline]
fn from_request(req: &mut HttpRequest<S>, _: &Self::Config) -> Self::Result { fn from_request(req: &HttpRequest<S>, _: &Self::Config) -> Self::Result {
req.clone() req.clone()
} }
} }

View File

@ -136,7 +136,7 @@ where
type Result = Box<Future<Item = Self, Error = Error>>; type Result = Box<Future<Item = Self, Error = Error>>;
#[inline] #[inline]
fn from_request(req: &mut HttpRequest<S>, cfg: &Self::Config) -> Self::Result { fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result {
let req = req.clone(); let req = req.clone();
let err = Rc::clone(&cfg.ehandler); let err = Rc::clone(&cfg.ehandler);
Box::new( Box::new(

View File

@ -134,7 +134,7 @@ where
let item = if !self.started { let item = if !self.started {
self.started = true; self.started = true;
let reply = T::from_request(&mut self.req, self.cfg.as_ref()).into(); let reply = T::from_request(&self.req, self.cfg.as_ref()).into();
match reply.into() { match reply.into() {
ReplyItem::Error(err) => return Err(err), ReplyItem::Error(err) => return Err(err),
ReplyItem::Message(msg) => msg, ReplyItem::Message(msg) => msg,
@ -268,7 +268,7 @@ where
if !self.started { if !self.started {
self.started = true; self.started = true;
let reply = T1::from_request(&mut self.req, self.cfg1.as_ref()).into(); let reply = T1::from_request(&self.req, self.cfg1.as_ref()).into();
let item1 = match reply.into() { let item1 = match reply.into() {
ReplyItem::Error(err) => return Err(err), ReplyItem::Error(err) => return Err(err),
ReplyItem::Message(msg) => msg, ReplyItem::Message(msg) => msg,
@ -279,7 +279,7 @@ where
ReplyItem::None => panic!("use after resolve"), ReplyItem::None => panic!("use after resolve"),
}; };
let reply = T2::from_request(&mut self.req, self.cfg2.as_ref()).into(); let reply = T2::from_request(&self.req, self.cfg2.as_ref()).into();
let item2 = match reply.into() { let item2 = match reply.into() {
ReplyItem::Error(err) => return Err(err), ReplyItem::Error(err) => return Err(err),
ReplyItem::Message(msg) => msg, ReplyItem::Message(msg) => msg,
@ -309,8 +309,7 @@ where
if self.fut1.is_some() { if self.fut1.is_some() {
match self.fut1.as_mut().unwrap().poll()? { match self.fut1.as_mut().unwrap().poll()? {
Async::Ready(item) => { Async::Ready(item) => {
let reply = let reply = T2::from_request(&self.req, self.cfg2.as_ref()).into();
T2::from_request(&mut self.req, self.cfg2.as_ref()).into();
let item2 = match reply.into() { let item2 = match reply.into() {
ReplyItem::Error(err) => return Err(err), ReplyItem::Error(err) => return Err(err),
ReplyItem::Message(msg) => msg, ReplyItem::Message(msg) => msg,
@ -480,7 +479,7 @@ where
if !self.started { if !self.started {
self.started = true; self.started = true;
let reply = T1::from_request(&mut self.req, self.cfg1.as_ref()).into(); let reply = T1::from_request(&self.req, self.cfg1.as_ref()).into();
let item1 = match reply.into() { let item1 = match reply.into() {
ReplyItem::Error(err) => return Err(err), ReplyItem::Error(err) => return Err(err),
ReplyItem::Message(msg) => msg, ReplyItem::Message(msg) => msg,
@ -491,7 +490,7 @@ where
ReplyItem::None => panic!("use after resolve"), ReplyItem::None => panic!("use after resolve"),
}; };
let reply = T2::from_request(&mut self.req, self.cfg2.as_ref()).into(); let reply = T2::from_request(&self.req, self.cfg2.as_ref()).into();
let item2 = match reply.into() { let item2 = match reply.into() {
ReplyItem::Error(err) => return Err(err), ReplyItem::Error(err) => return Err(err),
ReplyItem::Message(msg) => msg, ReplyItem::Message(msg) => msg,
@ -503,7 +502,7 @@ where
ReplyItem::None => panic!("use after resolve"), ReplyItem::None => panic!("use after resolve"),
}; };
let reply = T3::from_request(&mut self.req, self.cfg3.as_ref()).into(); let reply = T3::from_request(&self.req, self.cfg3.as_ref()).into();
let item3 = match reply.into() { let item3 = match reply.into() {
ReplyItem::Error(err) => return Err(err), ReplyItem::Error(err) => return Err(err),
ReplyItem::Message(msg) => msg, ReplyItem::Message(msg) => msg,
@ -536,8 +535,7 @@ where
Async::Ready(item) => { Async::Ready(item) => {
self.item1 = Some(item); self.item1 = Some(item);
self.fut1.take(); self.fut1.take();
let reply = let reply = T2::from_request(&self.req, self.cfg2.as_ref()).into();
T2::from_request(&mut self.req, self.cfg2.as_ref()).into();
let item2 = match reply.into() { let item2 = match reply.into() {
ReplyItem::Error(err) => return Err(err), ReplyItem::Error(err) => return Err(err),
ReplyItem::Message(msg) => msg, ReplyItem::Message(msg) => msg,
@ -548,8 +546,7 @@ where
ReplyItem::None => panic!("use after resolve"), ReplyItem::None => panic!("use after resolve"),
}; };
let reply = let reply = T3::from_request(&self.req, self.cfg3.as_ref()).into();
T3::from_request(&mut self.req, self.cfg3.as_ref()).into();
let item3 = match reply.into() { let item3 = match reply.into() {
ReplyItem::Error(err) => return Err(err), ReplyItem::Error(err) => return Err(err),
ReplyItem::Message(msg) => msg, ReplyItem::Message(msg) => msg,
@ -584,8 +581,7 @@ where
match self.fut2.as_mut().unwrap().poll()? { match self.fut2.as_mut().unwrap().poll()? {
Async::Ready(item) => { Async::Ready(item) => {
self.fut2.take(); self.fut2.take();
let reply = let reply = T3::from_request(&self.req, self.cfg3.as_ref()).into();
T3::from_request(&mut self.req, self.cfg3.as_ref()).into();
let item3 = match reply.into() { let item3 = match reply.into() {
ReplyItem::Error(err) => return Err(err), ReplyItem::Error(err) => return Err(err),
ReplyItem::Message(msg) => msg, ReplyItem::Message(msg) => msg,