1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-11-20 08:31:09 +00:00

make HttpRequest::extensions() readonly

This commit is contained in:
Nikolay Kim 2018-05-01 09:05:50 -07:00
parent 48e05a2d87
commit d9a4fadaae
5 changed files with 11 additions and 15 deletions

View file

@ -6,6 +6,9 @@
* `HttpServer::start_ssl()` and `HttpServer::start_tls()` deprecated. * `HttpServer::start_ssl()` and `HttpServer::start_tls()` deprecated.
Use `HttpServer::bind_ssl()` and `HttpServer::bind_tls()` instead. Use `HttpServer::bind_ssl()` and `HttpServer::bind_tls()` instead.
* `HttpRequest::extensions()` returns read only reference to the request's Extension
`HttpRequest::extensions_mut()` returns mutable reference.
## Migration from 0.4 to 0.5 ## Migration from 0.4 to 0.5

View file

@ -195,17 +195,10 @@ impl<S> HttpRequest<S> {
/// Request extensions /// Request extensions
#[inline] #[inline]
pub fn extensions(&mut self) -> &mut Extensions { pub fn extensions(&self) -> &Extensions {
&mut self.as_mut().extensions &mut self.as_mut().extensions
} }
/// Request extensions
#[inline]
#[doc(hidden)]
pub fn extensions_ro(&self) -> &Extensions {
&self.as_ref().extensions
}
/// Mutable refernece to a the request's extensions /// Mutable refernece to a the request's extensions
#[inline] #[inline]
pub fn extensions_mut(&mut self) -> &mut Extensions { pub fn extensions_mut(&mut self) -> &mut Extensions {

View file

@ -100,7 +100,7 @@ pub trait RequestIdentity {
impl<S> RequestIdentity for HttpRequest<S> { impl<S> RequestIdentity for HttpRequest<S> {
fn identity(&self) -> Option<&str> { fn identity(&self) -> Option<&str> {
if let Some(id) = self.extensions_ro().get::<IdentityBox>() { if let Some(id) = self.extensions().get::<IdentityBox>() {
return id.0.identity(); return id.0.identity();
} }
None None
@ -183,7 +183,7 @@ impl<S: 'static, T: IdentityPolicy<S>> Middleware<S> for IdentityService<T> {
.from_request(&mut req) .from_request(&mut req)
.then(move |res| match res { .then(move |res| match res {
Ok(id) => { Ok(id) => {
req.extensions().insert(IdentityBox(Box::new(id))); req.extensions_mut().insert(IdentityBox(Box::new(id)));
FutOk(None) FutOk(None)
} }
Err(err) => FutErr(err), Err(err) => FutErr(err),
@ -194,7 +194,7 @@ impl<S: 'static, T: IdentityPolicy<S>> Middleware<S> for IdentityService<T> {
fn response( fn response(
&self, req: &mut HttpRequest<S>, resp: HttpResponse, &self, req: &mut HttpRequest<S>, resp: HttpResponse,
) -> Result<Response> { ) -> Result<Response> {
if let Some(mut id) = req.extensions().remove::<IdentityBox>() { if let Some(mut id) = req.extensions_mut().remove::<IdentityBox>() {
id.0.write(resp) id.0.write(resp)
} else { } else {
Ok(Response::Done(resp)) Ok(Response::Done(resp))

View file

@ -116,7 +116,7 @@ impl Logger {
impl<S> Middleware<S> for Logger { impl<S> Middleware<S> for Logger {
fn start(&self, req: &mut HttpRequest<S>) -> Result<Started> { fn start(&self, req: &mut HttpRequest<S>) -> Result<Started> {
req.extensions().insert(StartTime(time::now())); req.extensions_mut().insert(StartTime(time::now()));
Ok(Started::Done) Ok(Started::Done)
} }

View file

@ -106,7 +106,7 @@ pub trait RequestSession {
impl<S> RequestSession for HttpRequest<S> { impl<S> RequestSession for HttpRequest<S> {
fn session(&mut self) -> Session { fn session(&mut self) -> Session {
if let Some(s_impl) = self.extensions().get_mut::<Arc<SessionImplBox>>() { if let Some(s_impl) = self.extensions_mut().get_mut::<Arc<SessionImplBox>>() {
if let Some(s) = Arc::get_mut(s_impl) { if let Some(s) = Arc::get_mut(s_impl) {
return Session(s.0.as_mut()); return Session(s.0.as_mut());
} }
@ -206,7 +206,7 @@ impl<S: 'static, T: SessionBackend<S>> Middleware<S> for SessionStorage<T, S> {
.from_request(&mut req) .from_request(&mut req)
.then(move |res| match res { .then(move |res| match res {
Ok(sess) => { Ok(sess) => {
req.extensions() req.extensions_mut()
.insert(Arc::new(SessionImplBox(Box::new(sess)))); .insert(Arc::new(SessionImplBox(Box::new(sess))));
FutOk(None) FutOk(None)
} }
@ -218,7 +218,7 @@ impl<S: 'static, T: SessionBackend<S>> Middleware<S> for SessionStorage<T, S> {
fn response( fn response(
&self, req: &mut HttpRequest<S>, resp: HttpResponse, &self, req: &mut HttpRequest<S>, resp: HttpResponse,
) -> Result<Response> { ) -> Result<Response> {
if let Some(s_box) = req.extensions().remove::<Arc<SessionImplBox>>() { if let Some(s_box) = req.extensions_mut().remove::<Arc<SessionImplBox>>() {
s_box.0.write(resp) s_box.0.write(resp)
} else { } else {
Ok(Response::Done(resp)) Ok(Response::Done(resp))