1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-01-31 02:52:53 +01:00

use non mutable self for HttpMessage::payload() for ergonomic reasons

This commit is contained in:
Nikolay Kim 2019-02-07 19:26:12 -08:00
parent 5575ee7d2d
commit 2a6e4dc7ab
5 changed files with 20 additions and 18 deletions

View File

@ -1,3 +1,4 @@
use std::cell::RefCell;
use std::time; use std::time;
use actix_codec::{AsyncRead, AsyncWrite}; use actix_codec::{AsyncRead, AsyncWrite};
@ -110,7 +111,7 @@ where
Ok(ClientResponse { Ok(ClientResponse {
head, head,
payload: Some(Box::new(Payload::new(body))), payload: RefCell::new(Some(Box::new(Payload::new(body)))),
}) })
}) })
.from_err() .from_err()

View File

@ -1,3 +1,4 @@
use std::cell::RefCell;
use std::fmt; use std::fmt;
use bytes::Bytes; use bytes::Bytes;
@ -13,7 +14,7 @@ use crate::message::{Head, ResponseHead};
#[derive(Default)] #[derive(Default)]
pub struct ClientResponse { pub struct ClientResponse {
pub(crate) head: ResponseHead, pub(crate) head: ResponseHead,
pub(crate) payload: Option<PayloadStream>, pub(crate) payload: RefCell<Option<PayloadStream>>,
} }
impl HttpMessage for ClientResponse { impl HttpMessage for ClientResponse {
@ -24,8 +25,8 @@ impl HttpMessage for ClientResponse {
} }
#[inline] #[inline]
fn payload(&mut self) -> Option<Self::Stream> { fn payload(&self) -> Option<Self::Stream> {
self.payload.take() self.payload.borrow_mut().take()
} }
} }
@ -34,7 +35,7 @@ impl ClientResponse {
pub fn new() -> ClientResponse { pub fn new() -> ClientResponse {
ClientResponse { ClientResponse {
head: ResponseHead::default(), head: ResponseHead::default(),
payload: None, payload: RefCell::new(None),
} }
} }
@ -80,7 +81,7 @@ impl ClientResponse {
/// Set response payload /// Set response payload
pub fn set_payload(&mut self, payload: PayloadStream) { pub fn set_payload(&mut self, payload: PayloadStream) {
self.payload = Some(payload); *self.payload.get_mut() = Some(payload);
} }
} }
@ -89,7 +90,7 @@ impl Stream for ClientResponse {
type Error = PayloadError; type Error = PayloadError;
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> { fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
if let Some(ref mut payload) = self.payload { if let Some(ref mut payload) = self.payload.get_mut() {
payload.poll() payload.poll()
} else { } else {
Ok(Async::Ready(None)) Ok(Async::Ready(None))

View File

@ -25,7 +25,7 @@ pub trait HttpMessage: Sized {
fn headers(&self) -> &HeaderMap; fn headers(&self) -> &HeaderMap;
/// Message payload stream /// Message payload stream
fn payload(&mut self) -> Option<Self::Stream>; fn payload(&self) -> Option<Self::Stream>;
#[doc(hidden)] #[doc(hidden)]
/// Get a header /// Get a header

View File

@ -75,7 +75,7 @@ mod extensions;
mod header; mod header;
mod helpers; mod helpers;
mod httpcodes; mod httpcodes;
mod httpmessage; pub mod httpmessage;
mod json; mod json;
mod message; mod message;
mod request; mod request;

View File

@ -1,4 +1,4 @@
use std::cell::{Ref, RefMut}; use std::cell::{Ref, RefCell, RefMut};
use std::fmt; use std::fmt;
use std::rc::Rc; use std::rc::Rc;
@ -14,7 +14,7 @@ use crate::payload::Payload;
/// Request /// Request
pub struct Request<P = Payload> { pub struct Request<P = Payload> {
pub(crate) payload: Option<P>, pub(crate) payload: RefCell<Option<P>>,
pub(crate) inner: Rc<Message<RequestHead>>, pub(crate) inner: Rc<Message<RequestHead>>,
} }
@ -29,8 +29,8 @@ where
} }
#[inline] #[inline]
fn payload(&mut self) -> Option<P> { fn payload(&self) -> Option<P> {
self.payload.take() self.payload.borrow_mut().take()
} }
} }
@ -38,7 +38,7 @@ impl Request<Payload> {
/// Create new Request instance /// Create new Request instance
pub fn new() -> Request<Payload> { pub fn new() -> Request<Payload> {
Request { Request {
payload: None, payload: RefCell::new(None),
inner: MessagePool::get_message(), inner: MessagePool::get_message(),
} }
} }
@ -48,7 +48,7 @@ impl<Payload> Request<Payload> {
/// Create new Request instance /// Create new Request instance
pub fn with_payload(payload: Payload) -> Request<Payload> { pub fn with_payload(payload: Payload) -> Request<Payload> {
Request { Request {
payload: Some(payload), payload: RefCell::new(Some(payload.into())),
inner: MessagePool::get_message(), inner: MessagePool::get_message(),
} }
} }
@ -59,7 +59,7 @@ impl<Payload> Request<Payload> {
I: Into<P>, I: Into<P>,
{ {
Request { Request {
payload: Some(payload.into()), payload: RefCell::new(Some(payload.into())),
inner: self.inner.clone(), inner: self.inner.clone(),
} }
} }
@ -67,9 +67,9 @@ impl<Payload> Request<Payload> {
/// Take request's payload /// Take request's payload
pub fn take_payload(mut self) -> (Option<Payload>, Request<()>) { pub fn take_payload(mut self) -> (Option<Payload>, Request<()>) {
( (
self.payload.take(), self.payload.get_mut().take(),
Request { Request {
payload: Some(()), payload: RefCell::new(None),
inner: self.inner.clone(), inner: self.inner.clone(),
}, },
) )