Skip to content

Commit 668a33c

Browse files
committed
remove internal usage of Body
1 parent d8cbb87 commit 668a33c

23 files changed

+137
-111
lines changed

actix-http/examples/echo2.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use std::io;
22

3-
use actix_http::{body::Body, http::HeaderValue, http::StatusCode};
3+
use actix_http::{body::AnyBody, http::HeaderValue, http::StatusCode};
44
use actix_http::{Error, HttpService, Request, Response};
55
use actix_server::Server;
66
use bytes::BytesMut;
77
use futures_util::StreamExt as _;
88

9-
async fn handle_request(mut req: Request) -> Result<Response<Body>, Error> {
9+
async fn handle_request(mut req: Request) -> Result<Response<AnyBody>, Error> {
1010
let mut body = BytesMut::new();
1111
while let Some(item) = req.payload().next().await {
1212
body.extend_from_slice(&item?)

actix-http/src/body/body.rs

+32-11
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::error::Error;
1414

1515
use super::{BodySize, BodyStream, MessageBody, MessageBodyMapErr, SizedStream};
1616

17+
#[deprecated(since = "4.0.0", note = "Renamed to `AnyBody`.")]
1718
pub type Body = AnyBody;
1819

1920
/// Represents various types of HTTP message body.
@@ -116,7 +117,7 @@ where
116117
}
117118

118119
impl PartialEq for AnyBody {
119-
fn eq(&self, other: &Body) -> bool {
120+
fn eq(&self, other: &AnyBody) -> bool {
120121
match *self {
121122
AnyBody::None => matches!(*other, AnyBody::None),
122123
AnyBody::Bytes(ref b) => match *other {
@@ -139,37 +140,37 @@ impl<S: fmt::Debug> fmt::Debug for AnyBody<S> {
139140
}
140141

141142
impl From<&'static str> for AnyBody {
142-
fn from(string: &'static str) -> Body {
143+
fn from(string: &'static str) -> AnyBody {
143144
AnyBody::Bytes(Bytes::from_static(string.as_ref()))
144145
}
145146
}
146147

147148
impl From<&'static [u8]> for AnyBody {
148-
fn from(bytes: &'static [u8]) -> Body {
149+
fn from(bytes: &'static [u8]) -> AnyBody {
149150
AnyBody::Bytes(Bytes::from_static(bytes))
150151
}
151152
}
152153

153154
impl From<Vec<u8>> for AnyBody {
154-
fn from(vec: Vec<u8>) -> Body {
155+
fn from(vec: Vec<u8>) -> AnyBody {
155156
AnyBody::Bytes(Bytes::from(vec))
156157
}
157158
}
158159

159160
impl From<String> for AnyBody {
160-
fn from(string: String) -> Body {
161+
fn from(string: String) -> AnyBody {
161162
string.into_bytes().into()
162163
}
163164
}
164165

165166
impl From<&'_ String> for AnyBody {
166-
fn from(string: &String) -> Body {
167+
fn from(string: &String) -> AnyBody {
167168
AnyBody::Bytes(Bytes::copy_from_slice(AsRef::<[u8]>::as_ref(&string)))
168169
}
169170
}
170171

171172
impl From<Cow<'_, str>> for AnyBody {
172-
fn from(string: Cow<'_, str>) -> Body {
173+
fn from(string: Cow<'_, str>) -> AnyBody {
173174
match string {
174175
Cow::Owned(s) => AnyBody::from(s),
175176
Cow::Borrowed(s) => {
@@ -180,33 +181,53 @@ impl From<Cow<'_, str>> for AnyBody {
180181
}
181182

182183
impl From<Bytes> for AnyBody {
183-
fn from(bytes: Bytes) -> Body {
184+
fn from(bytes: Bytes) -> Self {
184185
AnyBody::Bytes(bytes)
185186
}
186187
}
187188

188189
impl From<BytesMut> for AnyBody {
189-
fn from(bytes: BytesMut) -> Body {
190+
fn from(bytes: BytesMut) -> Self {
190191
AnyBody::Bytes(bytes.freeze())
191192
}
192193
}
193194

195+
impl<S, E> From<SizedStream<S>> for AnyBody<SizedStream<S>>
196+
where
197+
S: Stream<Item = Result<Bytes, E>> + 'static,
198+
E: Into<Box<dyn StdError>> + 'static,
199+
{
200+
fn from(stream: SizedStream<S>) -> Self {
201+
AnyBody::new(stream)
202+
}
203+
}
204+
194205
impl<S, E> From<SizedStream<S>> for AnyBody
195206
where
196207
S: Stream<Item = Result<Bytes, E>> + 'static,
197208
E: Into<Box<dyn StdError>> + 'static,
198209
{
199-
fn from(stream: SizedStream<S>) -> Body {
210+
fn from(stream: SizedStream<S>) -> Self {
200211
AnyBody::new_boxed(stream)
201212
}
202213
}
203214

215+
impl<S, E> From<BodyStream<S>> for AnyBody<BodyStream<S>>
216+
where
217+
S: Stream<Item = Result<Bytes, E>> + 'static,
218+
E: Into<Box<dyn StdError>> + 'static,
219+
{
220+
fn from(stream: BodyStream<S>) -> Self {
221+
AnyBody::new(stream)
222+
}
223+
}
224+
204225
impl<S, E> From<BodyStream<S>> for AnyBody
205226
where
206227
S: Stream<Item = Result<Bytes, E>> + 'static,
207228
E: Into<Box<dyn StdError>> + 'static,
208229
{
209-
fn from(stream: BodyStream<S>) -> Body {
230+
fn from(stream: BodyStream<S>) -> Self {
210231
AnyBody::new_boxed(stream)
211232
}
212233
}

actix-http/src/body/mod.rs

+38-30
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ mod message_body;
1414
mod size;
1515
mod sized_stream;
1616

17+
#[allow(deprecated)]
1718
pub use self::body::{AnyBody, Body, BoxBody};
1819
pub use self::body_stream::BodyStream;
1920
pub use self::message_body::MessageBody;
@@ -76,20 +77,20 @@ mod tests {
7677

7778
use super::*;
7879

79-
impl Body {
80+
impl AnyBody {
8081
pub(crate) fn get_ref(&self) -> &[u8] {
8182
match *self {
82-
Body::Bytes(ref bin) => bin,
83+
AnyBody::Bytes(ref bin) => bin,
8384
_ => panic!(),
8485
}
8586
}
8687
}
8788

8889
#[actix_rt::test]
8990
async fn test_static_str() {
90-
assert_eq!(Body::from("").size(), BodySize::Sized(0));
91-
assert_eq!(Body::from("test").size(), BodySize::Sized(4));
92-
assert_eq!(Body::from("test").get_ref(), b"test");
91+
assert_eq!(AnyBody::from("").size(), BodySize::Sized(0));
92+
assert_eq!(AnyBody::from("test").size(), BodySize::Sized(4));
93+
assert_eq!(AnyBody::from("test").get_ref(), b"test");
9394

9495
assert_eq!("test".size(), BodySize::Sized(4));
9596
assert_eq!(
@@ -103,13 +104,16 @@ mod tests {
103104

104105
#[actix_rt::test]
105106
async fn test_static_bytes() {
106-
assert_eq!(Body::from(b"test".as_ref()).size(), BodySize::Sized(4));
107-
assert_eq!(Body::from(b"test".as_ref()).get_ref(), b"test");
107+
assert_eq!(AnyBody::from(b"test".as_ref()).size(), BodySize::Sized(4));
108+
assert_eq!(AnyBody::from(b"test".as_ref()).get_ref(), b"test");
108109
assert_eq!(
109-
Body::copy_from_slice(b"test".as_ref()).size(),
110+
AnyBody::copy_from_slice(b"test".as_ref()).size(),
110111
BodySize::Sized(4)
111112
);
112-
assert_eq!(Body::copy_from_slice(b"test".as_ref()).get_ref(), b"test");
113+
assert_eq!(
114+
AnyBody::copy_from_slice(b"test".as_ref()).get_ref(),
115+
b"test"
116+
);
113117
let sb = Bytes::from(&b"test"[..]);
114118
pin!(sb);
115119

@@ -122,8 +126,8 @@ mod tests {
122126

123127
#[actix_rt::test]
124128
async fn test_vec() {
125-
assert_eq!(Body::from(Vec::from("test")).size(), BodySize::Sized(4));
126-
assert_eq!(Body::from(Vec::from("test")).get_ref(), b"test");
129+
assert_eq!(AnyBody::from(Vec::from("test")).size(), BodySize::Sized(4));
130+
assert_eq!(AnyBody::from(Vec::from("test")).get_ref(), b"test");
127131
let test_vec = Vec::from("test");
128132
pin!(test_vec);
129133

@@ -140,8 +144,8 @@ mod tests {
140144
#[actix_rt::test]
141145
async fn test_bytes() {
142146
let b = Bytes::from("test");
143-
assert_eq!(Body::from(b.clone()).size(), BodySize::Sized(4));
144-
assert_eq!(Body::from(b.clone()).get_ref(), b"test");
147+
assert_eq!(AnyBody::from(b.clone()).size(), BodySize::Sized(4));
148+
assert_eq!(AnyBody::from(b.clone()).get_ref(), b"test");
145149
pin!(b);
146150

147151
assert_eq!(b.size(), BodySize::Sized(4));
@@ -154,8 +158,8 @@ mod tests {
154158
#[actix_rt::test]
155159
async fn test_bytes_mut() {
156160
let b = BytesMut::from("test");
157-
assert_eq!(Body::from(b.clone()).size(), BodySize::Sized(4));
158-
assert_eq!(Body::from(b.clone()).get_ref(), b"test");
161+
assert_eq!(AnyBody::from(b.clone()).size(), BodySize::Sized(4));
162+
assert_eq!(AnyBody::from(b.clone()).get_ref(), b"test");
159163
pin!(b);
160164

161165
assert_eq!(b.size(), BodySize::Sized(4));
@@ -168,10 +172,10 @@ mod tests {
168172
#[actix_rt::test]
169173
async fn test_string() {
170174
let b = "test".to_owned();
171-
assert_eq!(Body::from(b.clone()).size(), BodySize::Sized(4));
172-
assert_eq!(Body::from(b.clone()).get_ref(), b"test");
173-
assert_eq!(Body::from(&b).size(), BodySize::Sized(4));
174-
assert_eq!(Body::from(&b).get_ref(), b"test");
175+
assert_eq!(AnyBody::from(b.clone()).size(), BodySize::Sized(4));
176+
assert_eq!(AnyBody::from(b.clone()).get_ref(), b"test");
177+
assert_eq!(AnyBody::from(&b).size(), BodySize::Sized(4));
178+
assert_eq!(AnyBody::from(&b).get_ref(), b"test");
175179
pin!(b);
176180

177181
assert_eq!(b.size(), BodySize::Sized(4));
@@ -204,29 +208,33 @@ mod tests {
204208
#[actix_rt::test]
205209
async fn test_body_eq() {
206210
assert!(
207-
Body::Bytes(Bytes::from_static(b"1"))
208-
== Body::Bytes(Bytes::from_static(b"1"))
211+
AnyBody::Bytes(Bytes::from_static(b"1"))
212+
== AnyBody::Bytes(Bytes::from_static(b"1"))
209213
);
210-
assert!(Body::Bytes(Bytes::from_static(b"1")) != Body::None);
214+
assert!(AnyBody::Bytes(Bytes::from_static(b"1")) != AnyBody::None);
211215
}
212216

213217
#[actix_rt::test]
214218
async fn test_body_debug() {
215-
assert!(format!("{:?}", Body::None).contains("Body::None"));
216-
assert!(format!("{:?}", Body::Bytes(Bytes::from_static(b"1"))).contains('1'));
219+
assert!(format!("{:?}", AnyBody::<BoxBody>::None).contains("Body::None"));
220+
assert!(format!("{:?}", AnyBody::from(Bytes::from_static(b"1"))).contains('1'));
217221
}
218222

219223
#[actix_rt::test]
220224
async fn test_serde_json() {
221225
use serde_json::{json, Value};
222226
assert_eq!(
223-
Body::from(serde_json::to_vec(&Value::String("test".to_owned())).unwrap())
224-
.size(),
227+
AnyBody::from(
228+
serde_json::to_vec(&Value::String("test".to_owned())).unwrap()
229+
)
230+
.size(),
225231
BodySize::Sized(6)
226232
);
227233
assert_eq!(
228-
Body::from(serde_json::to_vec(&json!({"test-key":"test-value"})).unwrap())
229-
.size(),
234+
AnyBody::from(
235+
serde_json::to_vec(&json!({"test-key":"test-value"})).unwrap()
236+
)
237+
.size(),
230238
BodySize::Sized(25)
231239
);
232240
}
@@ -250,11 +258,11 @@ mod tests {
250258

251259
#[actix_rt::test]
252260
async fn test_to_bytes() {
253-
let body = Body::empty();
261+
let body = AnyBody::empty();
254262
let bytes = to_bytes(body).await.unwrap();
255263
assert!(bytes.is_empty());
256264

257-
let body = Body::Bytes(Bytes::from_static(b"123"));
265+
let body = AnyBody::copy_from_slice(b"123");
258266
let bytes = to_bytes(body).await.unwrap();
259267
assert_eq!(bytes, b"123"[..]);
260268
}

actix-http/src/error.rs

+10-15
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ use std::{error::Error as StdError, fmt, io, str::Utf8Error, string::FromUtf8Err
55
use derive_more::{Display, Error, From};
66
use http::{uri::InvalidUri, StatusCode};
77

8-
use crate::{
9-
body::{AnyBody, Body},
10-
ws, Response,
11-
};
8+
use crate::{body::AnyBody, ws, Response};
129

1310
pub use http::Error as HttpError;
1411

@@ -29,6 +26,11 @@ impl Error {
2926
}
3027
}
3128

29+
pub(crate) fn with_cause(mut self, cause: impl Into<Box<dyn StdError>>) -> Self {
30+
self.inner.cause = Some(cause.into());
31+
self
32+
}
33+
3234
pub(crate) fn new_http() -> Self {
3335
Self::new(Kind::Http)
3436
}
@@ -49,26 +51,19 @@ impl Error {
4951
Self::new(Kind::SendResponse)
5052
}
5153

52-
// TODO: remove allow
53-
#[allow(dead_code)]
54+
#[allow(unused)] // reserved for future use (TODO: remove allow when being used)
5455
pub(crate) fn new_io() -> Self {
5556
Self::new(Kind::Io)
5657
}
5758

58-
// used in encoder behind feature flag so ignore unused warning
59-
#[allow(unused)]
59+
#[allow(unused)] // used in encoder behind feature flag so ignore unused warning
6060
pub(crate) fn new_encoder() -> Self {
6161
Self::new(Kind::Encoder)
6262
}
6363

6464
pub(crate) fn new_ws() -> Self {
6565
Self::new(Kind::Ws)
6666
}
67-
68-
pub(crate) fn with_cause(mut self, cause: impl Into<Box<dyn StdError>>) -> Self {
69-
self.inner.cause = Some(cause.into());
70-
self
71-
}
7267
}
7368

7469
impl From<Error> for Response<AnyBody> {
@@ -78,12 +73,12 @@ impl From<Error> for Response<AnyBody> {
7873
_ => StatusCode::INTERNAL_SERVER_ERROR,
7974
};
8075

81-
Response::new(status_code).set_body(Body::from(err.to_string()))
76+
Response::new(status_code).set_body(AnyBody::from(err.to_string()))
8277
}
8378
}
8479

8580
#[derive(Debug, Clone, Copy, PartialEq, Eq, Display)]
86-
pub enum Kind {
81+
pub(crate) enum Kind {
8782
#[display(fmt = "error processing HTTP")]
8883
Http,
8984

actix-http/src/header/shared/quality_item.rs

+1
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ mod tests {
195195
use super::*;
196196

197197
// copy of encoding from actix-web headers
198+
#[allow(clippy::enum_variant_names)] // allow Encoding prefix on EncodingExt
198199
#[derive(Clone, PartialEq, Debug)]
199200
pub enum Encoding {
200201
Chunked,

actix-http/src/response_builder.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ impl fmt::Debug for ResponseBuilder {
357357
#[cfg(test)]
358358
mod tests {
359359
use super::*;
360-
use crate::body::Body;
360+
use crate::body::AnyBody;
361361
use crate::http::header::{HeaderName, HeaderValue, CONTENT_TYPE};
362362

363363
#[test]
@@ -390,13 +390,13 @@ mod tests {
390390
fn test_content_type() {
391391
let resp = Response::build(StatusCode::OK)
392392
.content_type("text/plain")
393-
.body(Body::empty());
393+
.body(AnyBody::empty());
394394
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "text/plain")
395395
}
396396

397397
#[test]
398398
fn test_into_builder() {
399-
let mut resp: Response<Body> = "test".into();
399+
let mut resp: Response<AnyBody> = "test".into();
400400
assert_eq!(resp.status(), StatusCode::OK);
401401

402402
resp.headers_mut().insert(

0 commit comments

Comments
 (0)