@@ -9,10 +9,31 @@ use serde::Serialize;
9
9
/// expect JSON, however, there are some APIs that expect newline-delimited JSON (NDJSON).
10
10
/// The [Body] trait allows modelling different API body implementations.
11
11
pub trait Body {
12
+ /// A ready-made immutable buffer that can be used to avoid writing
13
+ ///
14
+ /// If this method returns `Some`, the bytes must be the same as
15
+ /// what would be written by `write`.
16
+ fn bytes ( & self ) -> Option < Bytes > {
17
+ None
18
+ }
19
+
12
20
/// Write to a buffer that will be written to the request stream
13
21
fn write ( & self , bytes : & mut BytesMut ) -> Result < ( ) , Error > ;
14
22
}
15
23
24
+ impl < ' a , B : ?Sized > Body for & ' a B
25
+ where
26
+ B : Body ,
27
+ {
28
+ fn bytes ( & self ) -> Option < Bytes > {
29
+ ( * * self ) . bytes ( )
30
+ }
31
+
32
+ fn write ( & self , bytes : & mut BytesMut ) -> Result < ( ) , Error > {
33
+ ( * * self ) . write ( bytes)
34
+ }
35
+ }
36
+
16
37
/// A JSON body of an API call.
17
38
pub struct JsonBody < T > ( pub ( crate ) T ) ;
18
39
@@ -78,42 +99,38 @@ where
78
99
}
79
100
80
101
impl Body for Bytes {
102
+ fn bytes ( & self ) -> Option < Bytes > {
103
+ Some ( self . clone ( ) )
104
+ }
105
+
81
106
fn write ( & self , bytes : & mut BytesMut ) -> Result < ( ) , Error > {
82
- bytes. resize ( self . len ( ) , 0 ) ;
83
- bytes. copy_from_slice ( & self [ ..] ) ;
84
- Ok ( ( ) )
107
+ self . as_ref ( ) . write ( bytes)
85
108
}
86
109
}
87
110
88
111
impl Body for Vec < u8 > {
89
112
fn write ( & self , bytes : & mut BytesMut ) -> Result < ( ) , Error > {
90
- bytes. resize ( self . len ( ) , 0 ) ;
91
- bytes. copy_from_slice ( & self [ ..] ) ;
92
- Ok ( ( ) )
113
+ self . as_slice ( ) . write ( bytes)
93
114
}
94
115
}
95
116
96
- impl Body for & ' static [ u8 ] {
117
+ impl < ' a > Body for & ' a [ u8 ] {
97
118
fn write ( & self , bytes : & mut BytesMut ) -> Result < ( ) , Error > {
98
- bytes. resize ( self . len ( ) , 0 ) ;
99
- bytes. copy_from_slice ( & self [ .. ] ) ;
119
+ bytes. reserve ( self . len ( ) ) ;
120
+ bytes. put_slice ( * self ) ;
100
121
Ok ( ( ) )
101
122
}
102
123
}
103
124
104
125
impl Body for String {
105
126
fn write ( & self , bytes : & mut BytesMut ) -> Result < ( ) , Error > {
106
- bytes. resize ( self . len ( ) , 0 ) ;
107
- bytes. copy_from_slice ( self . as_bytes ( ) ) ;
108
- Ok ( ( ) )
127
+ self . as_bytes ( ) . write ( bytes)
109
128
}
110
129
}
111
130
112
- impl Body for & ' static str {
131
+ impl < ' a > Body for & ' a str {
113
132
fn write ( & self , bytes : & mut BytesMut ) -> Result < ( ) , Error > {
114
- bytes. resize ( self . len ( ) , 0 ) ;
115
- bytes. copy_from_slice ( self . as_bytes ( ) ) ;
116
- Ok ( ( ) )
133
+ self . as_bytes ( ) . write ( bytes)
117
134
}
118
135
}
119
136
@@ -131,7 +148,7 @@ mod tests {
131
148
132
149
#[ test]
133
150
fn serialize_into_jsonbody_writes_to_bytes ( ) -> Result < ( ) , failure:: Error > {
134
- let mut bytes = BytesMut :: with_capacity ( 21 ) ;
151
+ let mut bytes = BytesMut :: new ( ) ;
135
152
let body: JsonBody < _ > = json ! ( { "foo" : "bar" , "baz" : 1 } ) . into ( ) ;
136
153
let _ = body. write ( & mut bytes) ?;
137
154
// NOTE: serde_json writes properties lexicographically
@@ -142,7 +159,7 @@ mod tests {
142
159
143
160
#[ test]
144
161
fn bodies_into_ndbody_writes_to_bytes ( ) -> Result < ( ) , failure:: Error > {
145
- let mut bytes = BytesMut :: with_capacity ( 22 ) ;
162
+ let mut bytes = BytesMut :: new ( ) ;
146
163
let mut bodies: Vec < JsonBody < _ > > = Vec :: with_capacity ( 2 ) ;
147
164
bodies. push ( json ! ( { "item" : 1 } ) . into ( ) ) ;
148
165
bodies. push ( json ! ( { "item" : 2 } ) . into ( ) ) ;
@@ -164,6 +181,19 @@ mod tests {
164
181
Ok ( ( ) )
165
182
}
166
183
184
+ #[ test]
185
+ fn bytes_body_returns_usable_buf ( ) -> Result < ( ) , failure:: Error > {
186
+ let mut bytes_mut = BytesMut :: with_capacity ( 21 ) ;
187
+ let buf = bytes:: Bytes :: from ( & b"{\" foo\" :\" bar\" ,\" baz\" :1}" [ ..] ) ;
188
+
189
+ let bytes = buf. bytes ( ) . expect ( "bytes always returns Some" ) ;
190
+ let _ = buf. write ( & mut bytes_mut) ?;
191
+ assert_eq ! ( & buf[ ..] , & bytes_mut[ ..] ) ;
192
+ assert_eq ! ( & bytes[ ..] , & bytes_mut[ ..] ) ;
193
+
194
+ Ok ( ( ) )
195
+ }
196
+
167
197
#[ test]
168
198
fn vec_body_writes_to_bytes_mut ( ) -> Result < ( ) , failure:: Error > {
169
199
let mut bytes_mut = BytesMut :: with_capacity ( 21 ) ;
0 commit comments