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
|
||||
|
||||
## Unreleased - 2021-xx-xx
|
||||
### Added
|
||||
* `AnyBody::empty` for quickly creating an empty body. [???]
|
||||
|
||||
### Changed
|
||||
* 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/????
|
||||
|
||||
|
||||
|
|
|
@ -20,9 +20,6 @@ pub enum AnyBody {
|
|||
/// Empty response. `Content-Length` header is not set.
|
||||
None,
|
||||
|
||||
/// Zero sized response body. `Content-Length` header is set to `0`.
|
||||
Empty,
|
||||
|
||||
/// Specific response body.
|
||||
Bytes(Bytes),
|
||||
|
||||
|
@ -31,6 +28,11 @@ pub enum AnyBody {
|
|||
}
|
||||
|
||||
impl AnyBody {
|
||||
/// Constructs a new, empty body.
|
||||
pub fn empty() -> Self {
|
||||
Self::Bytes(Bytes::new())
|
||||
}
|
||||
|
||||
/// Create body from slice (copy)
|
||||
pub fn from_slice(s: &[u8]) -> Self {
|
||||
Self::Bytes(Bytes::copy_from_slice(s))
|
||||
|
@ -52,7 +54,7 @@ impl MessageBody for AnyBody {
|
|||
fn size(&self) -> BodySize {
|
||||
match self {
|
||||
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::Stream(ref body) => body.size(),
|
||||
}
|
||||
|
@ -64,7 +66,6 @@ impl MessageBody for AnyBody {
|
|||
) -> Poll<Option<Result<Bytes, Self::Error>>> {
|
||||
match self.get_mut() {
|
||||
AnyBody::None => Poll::Ready(None),
|
||||
AnyBody::Empty => Poll::Ready(None),
|
||||
AnyBody::Bytes(ref mut bin) => {
|
||||
let len = bin.len();
|
||||
if len == 0 {
|
||||
|
@ -86,7 +87,6 @@ impl PartialEq for AnyBody {
|
|||
fn eq(&self, other: &Body) -> bool {
|
||||
match *self {
|
||||
AnyBody::None => matches!(*other, AnyBody::None),
|
||||
AnyBody::Empty => matches!(*other, AnyBody::Empty),
|
||||
AnyBody::Bytes(ref b) => match *other {
|
||||
AnyBody::Bytes(ref b2) => b == b2,
|
||||
_ => false,
|
||||
|
@ -100,7 +100,6 @@ impl fmt::Debug for AnyBody {
|
|||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match *self {
|
||||
AnyBody::None => write!(f, "AnyBody::None"),
|
||||
AnyBody::Empty => write!(f, "AnyBody::Empty"),
|
||||
AnyBody::Bytes(ref b) => write!(f, "AnyBody::Bytes({:?})", b),
|
||||
AnyBody::Stream(_) => write!(f, "AnyBody::Message(_)"),
|
||||
}
|
||||
|
|
|
@ -214,7 +214,6 @@ mod tests {
|
|||
#[actix_rt::test]
|
||||
async fn test_body_debug() {
|
||||
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'));
|
||||
}
|
||||
|
||||
|
@ -252,7 +251,7 @@ mod tests {
|
|||
|
||||
#[actix_rt::test]
|
||||
async fn test_to_bytes() {
|
||||
let body = Body::Empty;
|
||||
let body = Body::empty();
|
||||
let bytes = to_bytes(body).await.unwrap();
|
||||
assert!(bytes.is_empty());
|
||||
|
||||
|
|
|
@ -61,7 +61,6 @@ impl<B: MessageBody> Encoder<B> {
|
|||
let body = match body {
|
||||
ResponseBody::Other(b) => match b {
|
||||
Body::None => return ResponseBody::Other(Body::None),
|
||||
Body::Empty => return ResponseBody::Other(Body::Empty),
|
||||
Body::Bytes(buf) => {
|
||||
if can_encode {
|
||||
EncoderBody::Bytes(buf)
|
||||
|
|
|
@ -380,7 +380,7 @@ where
|
|||
// send_response would update InnerDispatcher state to SendPayload or
|
||||
// None(If response body is empty).
|
||||
// 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.
|
||||
|
@ -772,7 +772,7 @@ where
|
|||
trace!("Slow request timeout");
|
||||
let _ = self.as_mut().send_error_response(
|
||||
Response::with_body(StatusCode::REQUEST_TIMEOUT, ()),
|
||||
AnyBody::Empty,
|
||||
AnyBody::empty(),
|
||||
);
|
||||
this = self.project();
|
||||
this.flags.insert(Flags::STARTED | Flags::SHUTDOWN);
|
||||
|
|
|
@ -28,7 +28,7 @@ impl Response<AnyBody> {
|
|||
pub fn new(status: StatusCode) -> Self {
|
||||
Response {
|
||||
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.
|
||||
#[inline]
|
||||
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.
|
||||
|
@ -390,7 +390,7 @@ mod tests {
|
|||
fn test_content_type() {
|
||||
let resp = Response::build(StatusCode::OK)
|
||||
.content_type("text/plain")
|
||||
.body(Body::Empty);
|
||||
.body(Body::empty());
|
||||
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "text/plain")
|
||||
}
|
||||
|
||||
|
|
|
@ -194,7 +194,7 @@ where
|
|||
match body {
|
||||
Some(ref bytes) => Body::Bytes(bytes.clone()),
|
||||
// TODO: should this be Body::Empty or Body::None.
|
||||
_ => Body::Empty,
|
||||
_ => Body::empty(),
|
||||
}
|
||||
} else {
|
||||
body = None;
|
||||
|
|
|
@ -297,7 +297,7 @@ impl RequestSender {
|
|||
timeout: Option<Duration>,
|
||||
config: &ClientConfig,
|
||||
) -> 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>
|
||||
|
|
|
@ -387,7 +387,7 @@ impl HttpResponseBuilder {
|
|||
/// `HttpResponseBuilder` can not be used after this call.
|
||||
#[inline]
|
||||
pub fn finish(&mut self) -> HttpResponse {
|
||||
self.body(AnyBody::Empty)
|
||||
self.body(AnyBody::empty())
|
||||
}
|
||||
|
||||
/// This method construct new `HttpResponseBuilder`
|
||||
|
@ -475,7 +475,7 @@ mod tests {
|
|||
fn test_content_type() {
|
||||
let resp = HttpResponseBuilder::new(StatusCode::OK)
|
||||
.content_type("text/plain")
|
||||
.body(Body::Empty);
|
||||
.body(Body::empty());
|
||||
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "text/plain")
|
||||
}
|
||||
|
||||
|
|
|
@ -87,13 +87,12 @@ impl HttpResponse {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::dev::Body;
|
||||
use crate::http::StatusCode;
|
||||
use crate::HttpResponse;
|
||||
|
||||
#[test]
|
||||
fn test_build() {
|
||||
let resp = HttpResponse::Ok().body(Body::Empty);
|
||||
let resp = HttpResponse::Ok().finish();
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue