@@ -2,13 +2,13 @@ use std::fmt::Debug;
2
2
use std:: io:: { self , Cursor } ;
3
3
4
4
use bytes:: BytesMut ;
5
- use monoio:: buf:: IoBuf ;
6
5
use monoio:: io:: {
7
- AsyncReadRent , AsyncWriteRent , BufReader , BufWriter , OwnedReadHalf ,
8
- OwnedWriteHalf , Splitable ,
6
+ AsyncReadRent , BufReader , BufWriter , OwnedReadHalf , OwnedWriteHalf ,
7
+ Splitable ,
9
8
} ;
10
9
use monoio:: net:: TcpStream ;
11
10
11
+ use super :: frame:: write:: write_frame;
12
12
use super :: frame:: Frame ;
13
13
14
14
/// Send and receive `Frame` values from a remote peer.
@@ -179,83 +179,7 @@ impl WriteConnection {
179
179
/// *buffered* write stream. The data will be written to the buffer.
180
180
/// Once the buffer is full, it is flushed to the underlying socket.
181
181
pub async fn write_frame ( & mut self , frame : & Frame ) -> io:: Result < ( ) > {
182
- // Arrays are encoded by encoding each entry. All other frame types are
183
- // considered literals. For now, mini-redis is not able to encode
184
- // recursive frame structures. See below for more details.
185
- match frame {
186
- Frame :: Array ( val) => {
187
- // Encode the length of the array.
188
- self . stream_w . write ( & [ b'*' ] ) . await . 0 ?;
189
- self . write_decimal ( val. len ( ) as u64 ) . await ?;
190
-
191
- // Iterate and encode each entry in the array.
192
- for entry in & * * val {
193
- self . write_value ( entry) . await ?;
194
- }
195
- }
196
- // The frame type is a literal. Encode the value directly.
197
- _ => self . write_value ( frame) . await ?,
198
- }
199
-
200
- // Ensure the encoded frame is written to the socket. The calls above
201
- // are to the buffered stream and writes. Calling `flush` writes the
202
- // remaining contents of the buffer to the socket.
203
- self . stream_w . flush ( ) . await
204
- }
205
-
206
- /// Write a frame literal to the stream
207
- async fn write_value ( & mut self , frame : & Frame ) -> io:: Result < ( ) > {
208
- match frame {
209
- Frame :: Simple ( val) => {
210
- self . stream_w . write ( & [ b'+' ] ) . await . 0 ?;
211
- self . stream_w . write ( val. as_bytes ( ) . slice ( ..) ) . await . 0 ?;
212
- self . stream_w . write ( & [ b'\r' , b'\n' ] ) . await . 0 ?;
213
- }
214
- Frame :: Error ( val) => {
215
- self . stream_w . write ( & [ b'-' ] ) . await . 0 ?;
216
- self . stream_w . write ( val. as_bytes ( ) . slice ( ..) ) . await . 0 ?;
217
- self . stream_w . write ( & [ b'\r' , b'\n' ] ) . await . 0 ?;
218
- }
219
- Frame :: Integer ( val) => {
220
- self . stream_w . write ( & [ b':' ] ) . await . 0 ?;
221
- self . write_decimal ( * val) . await ?;
222
- }
223
- Frame :: Null => {
224
- self . stream_w . write ( b"$-1\r \n " ) . await . 0 ?;
225
- }
226
- Frame :: Bulk ( val) => {
227
- let len = val. len ( ) ;
228
-
229
- self . stream_w . write ( [ b'$' ] . as_slice ( ) ) . await . 0 ?;
230
- self . write_decimal ( len as u64 ) . await ?;
231
- self . stream_w . write ( val. slice ( ..) ) . await . 0 ?;
232
- self . stream_w . write ( & [ b'\r' , b'\n' ] ) . await . 0 ?;
233
- }
234
- // Encoding an `Array` from within a value cannot be done using a
235
- // recursive strategy. In general, async fns do not support
236
- // recursion. Mini-redis has not needed to encode nested arrays yet,
237
- // so for now it is skipped.
238
- Frame :: Array ( _val) => unreachable ! ( ) ,
239
- }
240
-
241
- Ok ( ( ) )
242
- }
243
-
244
- /// Write a decimal frame to the stream_w
245
- async fn write_decimal ( & mut self , val : u64 ) -> io:: Result < ( ) > {
246
- use std:: io:: Write ;
247
-
248
- // Convert the value to a string
249
- let buf = vec ! [ 0u8 ; 20 ] ;
250
- let mut buf = Cursor :: new ( buf) ;
251
- write ! ( & mut buf, "{}" , val) ?;
252
-
253
- let pos = buf. position ( ) as usize ;
254
-
255
- self . stream_w . write ( buf. into_inner ( ) . slice ( ..pos) ) . await . 0 ?;
256
- self . stream_w . write ( b"\r \n " ) . await . 0 ?;
257
-
258
- Ok ( ( ) )
182
+ write_frame ( & mut self . stream_w , frame) . await
259
183
}
260
184
261
185
pub fn into_inner ( self ) -> OwnedWriteHalf < TcpStream > {
0 commit comments