mirror of https://github.com/fafhrd91/actix-web
remove `AnyBody::empty` and add `empty()`
This commit is contained in:
parent
c912ec7f81
commit
9905df9a60
|
@ -1,9 +1,15 @@
|
||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## Unreleased - 2021-xx-xx
|
## Unreleased - 2021-xx-xx
|
||||||
|
### Added
|
||||||
|
* `AnyBody::empty` for quickly creating an empty body. [???]
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
* Rename `AnyBody::{Message => Stream}`. [#????]
|
* Rename `AnyBody::{Message => Stream}`. [#????]
|
||||||
|
|
||||||
|
### Removed
|
||||||
|
* `AnyBody::Empty`; an empty body can now only be represented as a zero-length `Bytes` variant. [#????]
|
||||||
|
|
||||||
[#????]: https://github.com/actix/actix-web/pull/????
|
[#????]: https://github.com/actix/actix-web/pull/????
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -20,9 +20,6 @@ pub enum AnyBody {
|
||||||
/// Empty response. `Content-Length` header is not set.
|
/// Empty response. `Content-Length` header is not set.
|
||||||
None,
|
None,
|
||||||
|
|
||||||
/// Zero sized response body. `Content-Length` header is set to `0`.
|
|
||||||
Empty,
|
|
||||||
|
|
||||||
/// Specific response body.
|
/// Specific response body.
|
||||||
Bytes(Bytes),
|
Bytes(Bytes),
|
||||||
|
|
||||||
|
@ -31,6 +28,11 @@ pub enum AnyBody {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AnyBody {
|
impl AnyBody {
|
||||||
|
/// Constructs a new, empty body.
|
||||||
|
pub fn empty() -> Self {
|
||||||
|
Self::Bytes(Bytes::new())
|
||||||
|
}
|
||||||
|
|
||||||
/// Create body from slice (copy)
|
/// Create body from slice (copy)
|
||||||
pub fn from_slice(s: &[u8]) -> Self {
|
pub fn from_slice(s: &[u8]) -> Self {
|
||||||
Self::Bytes(Bytes::copy_from_slice(s))
|
Self::Bytes(Bytes::copy_from_slice(s))
|
||||||
|
@ -52,7 +54,7 @@ impl MessageBody for AnyBody {
|
||||||
fn size(&self) -> BodySize {
|
fn size(&self) -> BodySize {
|
||||||
match self {
|
match self {
|
||||||
AnyBody::None => BodySize::None,
|
AnyBody::None => BodySize::None,
|
||||||
AnyBody::Empty => BodySize::Empty,
|
AnyBody::Bytes(ref bin) if bin.is_empty() => BodySize::Empty,
|
||||||
AnyBody::Bytes(ref bin) => BodySize::Sized(bin.len() as u64),
|
AnyBody::Bytes(ref bin) => BodySize::Sized(bin.len() as u64),
|
||||||
AnyBody::Stream(ref body) => body.size(),
|
AnyBody::Stream(ref body) => body.size(),
|
||||||
}
|
}
|
||||||
|
@ -64,7 +66,6 @@ impl MessageBody for AnyBody {
|
||||||
) -> Poll<Option<Result<Bytes, Self::Error>>> {
|
) -> Poll<Option<Result<Bytes, Self::Error>>> {
|
||||||
match self.get_mut() {
|
match self.get_mut() {
|
||||||
AnyBody::None => Poll::Ready(None),
|
AnyBody::None => Poll::Ready(None),
|
||||||
AnyBody::Empty => Poll::Ready(None),
|
|
||||||
AnyBody::Bytes(ref mut bin) => {
|
AnyBody::Bytes(ref mut bin) => {
|
||||||
let len = bin.len();
|
let len = bin.len();
|
||||||
if len == 0 {
|
if len == 0 {
|
||||||
|
@ -86,7 +87,6 @@ impl PartialEq for AnyBody {
|
||||||
fn eq(&self, other: &Body) -> bool {
|
fn eq(&self, other: &Body) -> bool {
|
||||||
match *self {
|
match *self {
|
||||||
AnyBody::None => matches!(*other, AnyBody::None),
|
AnyBody::None => matches!(*other, AnyBody::None),
|
||||||
AnyBody::Empty => matches!(*other, AnyBody::Empty),
|
|
||||||
AnyBody::Bytes(ref b) => match *other {
|
AnyBody::Bytes(ref b) => match *other {
|
||||||
AnyBody::Bytes(ref b2) => b == b2,
|
AnyBody::Bytes(ref b2) => b == b2,
|
||||||
_ => false,
|
_ => false,
|
||||||
|
@ -100,7 +100,6 @@ impl fmt::Debug for AnyBody {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
match *self {
|
match *self {
|
||||||
AnyBody::None => write!(f, "AnyBody::None"),
|
AnyBody::None => write!(f, "AnyBody::None"),
|
||||||
AnyBody::Empty => write!(f, "AnyBody::Empty"),
|
|
||||||
AnyBody::Bytes(ref b) => write!(f, "AnyBody::Bytes({:?})", b),
|
AnyBody::Bytes(ref b) => write!(f, "AnyBody::Bytes({:?})", b),
|
||||||
AnyBody::Stream(_) => write!(f, "AnyBody::Message(_)"),
|
AnyBody::Stream(_) => write!(f, "AnyBody::Message(_)"),
|
||||||
}
|
}
|
||||||
|
|
|
@ -214,7 +214,6 @@ mod tests {
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_body_debug() {
|
async fn test_body_debug() {
|
||||||
assert!(format!("{:?}", Body::None).contains("Body::None"));
|
assert!(format!("{:?}", Body::None).contains("Body::None"));
|
||||||
assert!(format!("{:?}", Body::Empty).contains("Body::Empty"));
|
|
||||||
assert!(format!("{:?}", Body::Bytes(Bytes::from_static(b"1"))).contains('1'));
|
assert!(format!("{:?}", Body::Bytes(Bytes::from_static(b"1"))).contains('1'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -252,7 +251,7 @@ mod tests {
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_to_bytes() {
|
async fn test_to_bytes() {
|
||||||
let body = Body::Empty;
|
let body = Body::empty();
|
||||||
let bytes = to_bytes(body).await.unwrap();
|
let bytes = to_bytes(body).await.unwrap();
|
||||||
assert!(bytes.is_empty());
|
assert!(bytes.is_empty());
|
||||||
|
|
||||||
|
|
|
@ -61,7 +61,6 @@ impl<B: MessageBody> Encoder<B> {
|
||||||
let body = match body {
|
let body = match body {
|
||||||
ResponseBody::Other(b) => match b {
|
ResponseBody::Other(b) => match b {
|
||||||
Body::None => return ResponseBody::Other(Body::None),
|
Body::None => return ResponseBody::Other(Body::None),
|
||||||
Body::Empty => return ResponseBody::Other(Body::Empty),
|
|
||||||
Body::Bytes(buf) => {
|
Body::Bytes(buf) => {
|
||||||
if can_encode {
|
if can_encode {
|
||||||
EncoderBody::Bytes(buf)
|
EncoderBody::Bytes(buf)
|
||||||
|
|
|
@ -380,7 +380,7 @@ where
|
||||||
// send_response would update InnerDispatcher state to SendPayload or
|
// send_response would update InnerDispatcher state to SendPayload or
|
||||||
// None(If response body is empty).
|
// None(If response body is empty).
|
||||||
// continue loop to poll it.
|
// continue loop to poll it.
|
||||||
self.as_mut().send_error_response(res, AnyBody::Empty)?;
|
self.as_mut().send_error_response(res, AnyBody::empty())?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// return with upgrade request and poll it exclusively.
|
// return with upgrade request and poll it exclusively.
|
||||||
|
@ -772,7 +772,7 @@ where
|
||||||
trace!("Slow request timeout");
|
trace!("Slow request timeout");
|
||||||
let _ = self.as_mut().send_error_response(
|
let _ = self.as_mut().send_error_response(
|
||||||
Response::with_body(StatusCode::REQUEST_TIMEOUT, ()),
|
Response::with_body(StatusCode::REQUEST_TIMEOUT, ()),
|
||||||
AnyBody::Empty,
|
AnyBody::empty(),
|
||||||
);
|
);
|
||||||
this = self.project();
|
this = self.project();
|
||||||
this.flags.insert(Flags::STARTED | Flags::SHUTDOWN);
|
this.flags.insert(Flags::STARTED | Flags::SHUTDOWN);
|
||||||
|
|
|
@ -28,7 +28,7 @@ impl Response<AnyBody> {
|
||||||
pub fn new(status: StatusCode) -> Self {
|
pub fn new(status: StatusCode) -> Self {
|
||||||
Response {
|
Response {
|
||||||
head: BoxedResponseHead::new(status),
|
head: BoxedResponseHead::new(status),
|
||||||
body: AnyBody::Empty,
|
body: AnyBody::empty(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -270,7 +270,7 @@ impl ResponseBuilder {
|
||||||
/// This `ResponseBuilder` will be left in a useless state.
|
/// This `ResponseBuilder` will be left in a useless state.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn finish(&mut self) -> Response<AnyBody> {
|
pub fn finish(&mut self) -> Response<AnyBody> {
|
||||||
self.body(AnyBody::Empty)
|
self.body(AnyBody::empty())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create an owned `ResponseBuilder`, leaving the original in a useless state.
|
/// Create an owned `ResponseBuilder`, leaving the original in a useless state.
|
||||||
|
@ -390,7 +390,7 @@ mod tests {
|
||||||
fn test_content_type() {
|
fn test_content_type() {
|
||||||
let resp = Response::build(StatusCode::OK)
|
let resp = Response::build(StatusCode::OK)
|
||||||
.content_type("text/plain")
|
.content_type("text/plain")
|
||||||
.body(Body::Empty);
|
.body(Body::empty());
|
||||||
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "text/plain")
|
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "text/plain")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -194,7 +194,7 @@ where
|
||||||
match body {
|
match body {
|
||||||
Some(ref bytes) => Body::Bytes(bytes.clone()),
|
Some(ref bytes) => Body::Bytes(bytes.clone()),
|
||||||
// TODO: should this be Body::Empty or Body::None.
|
// TODO: should this be Body::Empty or Body::None.
|
||||||
_ => Body::Empty,
|
_ => Body::empty(),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
body = None;
|
body = None;
|
||||||
|
|
|
@ -297,7 +297,7 @@ impl RequestSender {
|
||||||
timeout: Option<Duration>,
|
timeout: Option<Duration>,
|
||||||
config: &ClientConfig,
|
config: &ClientConfig,
|
||||||
) -> SendClientRequest {
|
) -> SendClientRequest {
|
||||||
self.send_body(addr, response_decompress, timeout, config, Body::Empty)
|
self.send_body(addr, response_decompress, timeout, config, Body::empty())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_header_if_none<V>(&mut self, key: HeaderName, value: V) -> Result<(), HttpError>
|
fn set_header_if_none<V>(&mut self, key: HeaderName, value: V) -> Result<(), HttpError>
|
||||||
|
|
|
@ -387,7 +387,7 @@ impl HttpResponseBuilder {
|
||||||
/// `HttpResponseBuilder` can not be used after this call.
|
/// `HttpResponseBuilder` can not be used after this call.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn finish(&mut self) -> HttpResponse {
|
pub fn finish(&mut self) -> HttpResponse {
|
||||||
self.body(AnyBody::Empty)
|
self.body(AnyBody::empty())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This method construct new `HttpResponseBuilder`
|
/// This method construct new `HttpResponseBuilder`
|
||||||
|
@ -475,7 +475,7 @@ mod tests {
|
||||||
fn test_content_type() {
|
fn test_content_type() {
|
||||||
let resp = HttpResponseBuilder::new(StatusCode::OK)
|
let resp = HttpResponseBuilder::new(StatusCode::OK)
|
||||||
.content_type("text/plain")
|
.content_type("text/plain")
|
||||||
.body(Body::Empty);
|
.body(Body::empty());
|
||||||
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "text/plain")
|
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "text/plain")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -87,13 +87,12 @@ impl HttpResponse {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::dev::Body;
|
|
||||||
use crate::http::StatusCode;
|
use crate::http::StatusCode;
|
||||||
use crate::HttpResponse;
|
use crate::HttpResponse;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_build() {
|
fn test_build() {
|
||||||
let resp = HttpResponse::Ok().body(Body::Empty);
|
let resp = HttpResponse::Ok().finish();
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue