1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-24 07:53:00 +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]
fn test_backtrace() {
let orig = ErrorBadRequest("err");
let e: Error = orig.into();
let e = ErrorBadRequest("err");
assert!(e.backtrace().is_some());
}

View File

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

View File

@ -36,10 +36,7 @@ pub trait Responder {
/// Trait implemented by types that can be extracted from request.
///
/// Types that implement this trait can be used with `Route::with()` method.
pub trait FromRequest<S>: Sized
where
S: 'static,
{
pub trait FromRequest<S>: Sized {
/// Configuration for conversion process
type Config: Default;
@ -47,7 +44,14 @@ where
type Result: Into<Reply<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
@ -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 Result = State<S>;
#[inline]
fn from_request(req: &mut HttpRequest<S>, _: &Self::Config) -> Self::Result {
State(req.clone()).into()
fn from_request(req: &HttpRequest<S>, _: &Self::Config) -> Self::Result {
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 Result = Self;
#[inline]
fn from_request(req: &mut HttpRequest<S>, _: &Self::Config) -> Self::Result {
fn from_request(req: &HttpRequest<S>, _: &Self::Config) -> Self::Result {
req.clone()
}
}

View File

@ -136,7 +136,7 @@ where
type Result = Box<Future<Item = Self, Error = Error>>;
#[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 err = Rc::clone(&cfg.ehandler);
Box::new(

View File

@ -134,7 +134,7 @@ where
let item = if !self.started {
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() {
ReplyItem::Error(err) => return Err(err),
ReplyItem::Message(msg) => msg,
@ -268,7 +268,7 @@ where
if !self.started {
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() {
ReplyItem::Error(err) => return Err(err),
ReplyItem::Message(msg) => msg,
@ -279,7 +279,7 @@ where
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() {
ReplyItem::Error(err) => return Err(err),
ReplyItem::Message(msg) => msg,
@ -309,8 +309,7 @@ where
if self.fut1.is_some() {
match self.fut1.as_mut().unwrap().poll()? {
Async::Ready(item) => {
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() {
ReplyItem::Error(err) => return Err(err),
ReplyItem::Message(msg) => msg,
@ -480,7 +479,7 @@ where
if !self.started {
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() {
ReplyItem::Error(err) => return Err(err),
ReplyItem::Message(msg) => msg,
@ -491,7 +490,7 @@ where
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() {
ReplyItem::Error(err) => return Err(err),
ReplyItem::Message(msg) => msg,
@ -503,7 +502,7 @@ where
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() {
ReplyItem::Error(err) => return Err(err),
ReplyItem::Message(msg) => msg,
@ -536,8 +535,7 @@ where
Async::Ready(item) => {
self.item1 = Some(item);
self.fut1.take();
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() {
ReplyItem::Error(err) => return Err(err),
ReplyItem::Message(msg) => msg,
@ -548,8 +546,7 @@ where
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() {
ReplyItem::Error(err) => return Err(err),
ReplyItem::Message(msg) => msg,
@ -584,8 +581,7 @@ where
match self.fut2.as_mut().unwrap().poll()? {
Async::Ready(item) => {
self.fut2.take();
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() {
ReplyItem::Error(err) => return Err(err),
ReplyItem::Message(msg) => msg,