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 actix_codec::{AsyncRead, AsyncWrite};
@ -110,7 +111,7 @@ where
Ok(ClientResponse {
head,
payload: Some(Box::new(Payload::new(body))),
payload: RefCell::new(Some(Box::new(Payload::new(body)))),
})
})
.from_err()

View File

@ -1,3 +1,4 @@
use std::cell::RefCell;
use std::fmt;
use bytes::Bytes;
@ -13,7 +14,7 @@ use crate::message::{Head, ResponseHead};
#[derive(Default)]
pub struct ClientResponse {
pub(crate) head: ResponseHead,
pub(crate) payload: Option<PayloadStream>,
pub(crate) payload: RefCell<Option<PayloadStream>>,
}
impl HttpMessage for ClientResponse {
@ -24,8 +25,8 @@ impl HttpMessage for ClientResponse {
}
#[inline]
fn payload(&mut self) -> Option<Self::Stream> {
self.payload.take()
fn payload(&self) -> Option<Self::Stream> {
self.payload.borrow_mut().take()
}
}
@ -34,7 +35,7 @@ impl ClientResponse {
pub fn new() -> ClientResponse {
ClientResponse {
head: ResponseHead::default(),
payload: None,
payload: RefCell::new(None),
}
}
@ -80,7 +81,7 @@ impl ClientResponse {
/// Set response payload
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;
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()
} else {
Ok(Async::Ready(None))

View File

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

View File

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

View File

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