File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ use core:: mem;
2+
13use no_std_io:: io:: { Read , Seek , Write } ;
24
35#[ cfg( feature = "alloc" ) ]
4042 Ctx : Copy ,
4143 Predicate : FnMut ( usize , & T ) -> bool ,
4244{
45+ // ZST detected, return empty vec
46+ if mem:: size_of :: < T > ( ) == 0 {
47+ return Ok ( Vec :: new ( ) ) ;
48+ }
49+
4350 let mut res = capacity. map_or_else ( Vec :: new, Vec :: with_capacity) ;
4451
4552 let start_read = reader. bits_read ;
6774 T : DekuReader < ' a , Ctx > ,
6875 Ctx : Copy ,
6976{
77+ // ZST detected, return empty vec
78+ if mem:: size_of :: < T > ( ) == 0 {
79+ return Ok ( Vec :: new ( ) ) ;
80+ }
81+
7082 let mut res = capacity. map_or_else ( Vec :: new, Vec :: with_capacity) ;
7183 loop {
7284 if reader. end ( ) {
Original file line number Diff line number Diff line change @@ -230,3 +230,36 @@ fn test_units() {
230230 let new_bytes = a. to_bytes ( ) . unwrap ( ) ;
231231 assert_eq ! ( bytes, & * new_bytes) ;
232232}
233+
234+ /// Issue 513
235+ #[ test]
236+ fn test_zst_vec_1 ( ) {
237+ #[ derive( Debug , PartialEq , DekuRead ) ]
238+ struct EmptyThing { }
239+
240+ #[ derive( Debug , PartialEq , DekuRead ) ]
241+ struct ListOfThings {
242+ #[ deku( read_all) ]
243+ things : Vec < EmptyThing > ,
244+ }
245+
246+ let bytes = vec ! [ ] ;
247+ let ( y, x) = ListOfThings :: from_bytes ( ( & bytes, 0 ) ) . unwrap ( ) ;
248+ assert_eq ! ( x. things. len( ) , 0 ) ;
249+ }
250+ /// Issue 513
251+ #[ test]
252+ fn test_zst_vec_2 ( ) {
253+ #[ derive( Debug , PartialEq , DekuRead ) ]
254+ struct EmptyThing { }
255+
256+ #[ derive( Debug , PartialEq , DekuRead ) ]
257+ struct ListOfThings {
258+ #[ deku( bytes_read = "1" ) ]
259+ things : Vec < EmptyThing > ,
260+ }
261+
262+ let bytes = vec ! [ ] ;
263+ let ( y, x) = ListOfThings :: from_bytes ( ( & bytes, 0 ) ) . unwrap ( ) ;
264+ assert_eq ! ( x. things. len( ) , 0 ) ;
265+ }
You can’t perform that action at this time.
0 commit comments