1818//! This module provides methods to deserialize an incoming response packet
1919
2020use crate :: terrapipe:: RespCode ;
21- use std:: hint :: unreachable_unchecked ;
21+ use std:: vec ;
2222
2323#[ derive( Debug , PartialEq ) ]
2424/// A response datagroup
@@ -31,8 +31,13 @@ impl DataGroup {
3131 fn size ( & self ) -> usize {
3232 self . 0 . len ( )
3333 }
34- fn __unpack_0 ( mut self ) -> DataType {
35- self . 0 . pop ( ) . unwrap ( )
34+ }
35+
36+ impl IntoIterator for DataGroup {
37+ type Item = DataType ;
38+ type IntoIter = vec:: IntoIter < Self :: Item > ;
39+ fn into_iter ( self ) -> <Self as IntoIterator >:: IntoIter {
40+ self . 0 . into_iter ( )
3641 }
3742}
3843
@@ -70,7 +75,9 @@ pub enum ClientResult {
7075 /// The response was Invalid
7176 InvalidResponse ,
7277 /// The response is a valid response and has been parsed into a vector of datagroups
73- Response ( Vec < DataGroup > , usize ) ,
78+ PipelinedResponse ( Vec < DataGroup > , usize ) ,
79+ /// The response is a valid response and has been parsed into a datagroup
80+ SimpleResponse ( DataGroup , usize ) ,
7481 /// A single element in a datagroup (please note that this is a client abstraction)
7582 ResponseItem ( DataType , usize ) ,
7683 /// The response was empty, which means that the remote end closed the connection
@@ -237,19 +244,13 @@ pub fn parse(buf: &[u8]) -> ClientResult {
237244 if items. len ( ) == 1 {
238245 if items[ 0 ] . size ( ) == 1 {
239246 // Single item returned, so we can return this as ClientResult::ResponseItem
240- ClientResult :: ResponseItem (
241- items
242- . pop ( )
243- . unwrap_or_else ( || unsafe { unreachable_unchecked ( ) } )
244- . __unpack_0 ( ) ,
245- pos,
246- )
247+ ClientResult :: ResponseItem ( items. swap_remove ( 0 ) . 0 . swap_remove ( 0 ) , pos)
247248 } else {
248249 // More than one time returned, so we can return this as ClientResult::Response
249- ClientResult :: Response ( items, pos)
250+ ClientResult :: SimpleResponse ( items. swap_remove ( 0 ) , pos)
250251 }
251252 } else {
252- todo ! ( "Pipelined queries aren't implemented yet!" )
253+ ClientResult :: PipelinedResponse ( items , pos )
253254 }
254255 } else {
255256 // Since the number of items we got is not equal to the action size - not all data was
@@ -317,14 +318,14 @@ fn test_parser() {
317318 . to_owned ( ) ;
318319 assert_eq ! (
319320 parse( & res) ,
320- ClientResult :: Response (
321- vec! [ DataGroup ( vec![
321+ ClientResult :: SimpleResponse (
322+ DataGroup ( vec![
322323 DataType :: RespCode ( RespCode :: NotFound ) ,
323324 DataType :: RespCode ( RespCode :: Okay ) ,
324325 DataType :: Str ( "sayan" . to_owned( ) ) ,
325326 DataType :: Str ( "is" . to_owned( ) ) ,
326327 DataType :: Str ( "busy" . to_owned( ) )
327- ] ) ] ,
328+ ] ) ,
328329 res. len( )
329330 )
330331 ) ;
0 commit comments