File tree Expand file tree Collapse file tree 1 file changed +30
-7
lines changed Expand file tree Collapse file tree 1 file changed +30
-7
lines changed Original file line number Diff line number Diff line change 1
1
//! HTTP body types
2
2
3
- use crate :: io:: { AsyncRead , Cursor } ;
3
+ use crate :: io:: { AsyncRead , Cursor , Empty } ;
4
4
5
5
pub use super :: response:: IncomingBody ;
6
6
@@ -41,12 +41,24 @@ impl IntoBody for String {
41
41
}
42
42
}
43
43
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 ( ) ) )
50
62
}
51
63
}
52
64
@@ -59,3 +71,14 @@ impl<T: AsRef<[u8]>> AsyncRead for BoundedBody<T> {
59
71
self . 0 . read ( buf) . await
60
72
}
61
73
}
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
+ }
You can’t perform that action at this time.
0 commit comments