File tree Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -377,6 +377,9 @@ impl<'a, 'de> SeqAccess<'de> for ItemsParser<'a> {
377
377
self . len -= 1 ;
378
378
379
379
let bytes = self . bytes . get ( ) ;
380
+ if bytes. is_empty ( ) {
381
+ return Ok ( None ) ;
382
+ }
380
383
seed. deserialize ( Deserializer { bytes } ) . map ( Some )
381
384
}
382
385
@@ -398,6 +401,9 @@ impl<'a, 'de> MapAccess<'de> for ItemsParser<'a> {
398
401
self . len -= 1 ;
399
402
400
403
let bytes = self . bytes . get ( ) ;
404
+ if bytes. is_empty ( ) {
405
+ return Ok ( None ) ;
406
+ }
401
407
seed. deserialize ( Deserializer { bytes } ) . map ( Some )
402
408
}
403
409
Original file line number Diff line number Diff line change @@ -611,4 +611,24 @@ mod tests {
611
611
let expected = Bytes :: copy_from_slice ( input) ;
612
612
assert_eq ! ( actual, expected) ;
613
613
}
614
+
615
+ #[ test]
616
+ fn dont_fail_on_wrong_lengths ( ) {
617
+ let wrong_list = bolt ( )
618
+ . tiny_list ( 3 ) // says it's three elements
619
+ . tiny_int ( 42 ) // but it's not
620
+ . build ( ) ;
621
+
622
+ assert_eq ! ( from_bytes:: <Vec <u32 >>( wrong_list) . unwrap( ) , vec![ 42 ] ) ;
623
+
624
+ let wrong_map = bolt ( )
625
+ . tiny_map ( 3 ) // says it's three elements
626
+ . tiny_string ( "key" ) // but it's not
627
+ . tiny_int ( 42 )
628
+ . build ( ) ;
629
+
630
+ let data = from_bytes :: < std:: collections:: HashMap < String , u32 > > ( wrong_map) . unwrap ( ) ;
631
+ assert_eq ! ( data. len( ) , 1 ) ;
632
+ assert_eq ! ( data. get( "key" ) . copied( ) , Some ( 42 ) ) ;
633
+ }
614
634
}
You can’t perform that action at this time.
0 commit comments