Skip to content

Commit 42a3829

Browse files
committed
body trait: no more blanket impl forall AsyncRead
impl Body for Empty and BoundedBody. Also, impl some obvious IntoBodys.
1 parent 283f11f commit 42a3829

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

src/http/body.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! HTTP body types
22
3-
use crate::io::{AsyncRead, Cursor};
3+
use crate::io::{AsyncRead, Cursor, Empty};
44

55
pub use super::response::IncomingBody;
66

@@ -41,12 +41,24 @@ impl IntoBody for String {
4141
}
4242
}
4343

44-
impl<T> Body for T
45-
where
46-
T: AsyncRead,
47-
{
48-
fn len(&self) -> Option<usize> {
49-
None
44+
impl IntoBody for &str {
45+
type IntoBody = BoundedBody<Vec<u8>>;
46+
fn into_body(self) -> Self::IntoBody {
47+
BoundedBody(Cursor::new(self.to_owned().into_bytes()))
48+
}
49+
}
50+
51+
impl IntoBody for Vec<u8> {
52+
type IntoBody = BoundedBody<Vec<u8>>;
53+
fn into_body(self) -> Self::IntoBody {
54+
BoundedBody(Cursor::new(self))
55+
}
56+
}
57+
58+
impl IntoBody for &[u8] {
59+
type IntoBody = BoundedBody<Vec<u8>>;
60+
fn into_body(self) -> Self::IntoBody {
61+
BoundedBody(Cursor::new(self.to_owned()))
5062
}
5163
}
5264

@@ -59,3 +71,14 @@ impl<T: AsRef<[u8]>> AsyncRead for BoundedBody<T> {
5971
self.0.read(buf).await
6072
}
6173
}
74+
impl<T: AsRef<[u8]>> Body for BoundedBody<T> {
75+
fn len(&self) -> Option<usize> {
76+
Some(self.0.get_ref().as_ref().len())
77+
}
78+
}
79+
80+
impl Body for Empty {
81+
fn len(&self) -> Option<usize> {
82+
Some(0)
83+
}
84+
}

0 commit comments

Comments
 (0)