File tree Expand file tree Collapse file tree 3 files changed +55
-0
lines changed Expand file tree Collapse file tree 3 files changed +55
-0
lines changed Original file line number Diff line number Diff line change @@ -68,4 +68,13 @@ mod alloc {
6868 self . as_str ( ) . pack ( buf)
6969 }
7070 }
71+
72+ impl Packable for Box < [ u8 ] > {
73+ fn pack < T > ( & self , buf : & mut T ) -> usize
74+ where
75+ T : Extend < u8 > ,
76+ {
77+ pack_binary ( buf, self )
78+ }
79+ }
7180}
Original file line number Diff line number Diff line change @@ -101,3 +101,36 @@ impl Unpackable for String {
101101 Ok ( ( n + len, s) )
102102 }
103103}
104+
105+ impl Unpackable for Box < [ u8 ] > {
106+ type Error = Error ;
107+
108+ fn unpack ( buf : & [ u8 ] ) -> Result < ( usize , Self ) , Self :: Error > {
109+ unpack_binary ( buf) . map ( |( n, b) | ( n, b. to_vec ( ) . into_boxed_slice ( ) ) )
110+ }
111+
112+ fn unpack_iter < I > ( bytes : I ) -> Result < ( usize , Self ) , Self :: Error >
113+ where
114+ I : IntoIterator < Item = u8 > ,
115+ {
116+ let mut bytes = bytes. into_iter ( ) ;
117+ let format = take_byte_iter ( bytes. by_ref ( ) ) ?;
118+ let ( n, len) = match format {
119+ Format :: BIN8 => ( 2 , take_byte_iter ( bytes. by_ref ( ) ) ? as usize ) ,
120+ Format :: BIN16 => (
121+ 3 ,
122+ take_num_iter ( bytes. by_ref ( ) , u16:: from_be_bytes) ? as usize ,
123+ ) ,
124+ Format :: BIN32 => (
125+ 5 ,
126+ take_num_iter ( bytes. by_ref ( ) , u32:: from_be_bytes) ? as usize ,
127+ ) ,
128+ _ => return Err ( Error :: UnexpectedFormatTag ) ,
129+ } ;
130+ let v: Vec < _ > = bytes. take ( len) . collect ( ) ;
131+ if v. len ( ) < len {
132+ return Err ( Error :: BufferTooShort ) ;
133+ }
134+ Ok ( ( n + len, v. into_boxed_slice ( ) ) )
135+ }
136+ }
Original file line number Diff line number Diff line change @@ -30,6 +30,19 @@ fn empty_str() {
3030}
3131
3232proptest ! {
33+ #[ test]
34+ fn slice( value: Box <[ u8 ] >) {
35+ let mut bytes = Vec :: new( ) ;
36+ let n = msgpacker:: pack_binary( & mut bytes, & value) ;
37+ assert_eq!( n, bytes. len( ) ) ;
38+ let ( o, x) : ( usize , & [ u8 ] ) = msgpacker:: unpack_binary( & bytes) . unwrap( ) ;
39+ let ( p, y) : ( usize , Vec <u8 >) = msgpacker:: unpack_binary_iter( bytes. clone( ) ) . unwrap( ) ;
40+ assert_eq!( n, o) ;
41+ assert_eq!( n, p) ;
42+ assert_eq!( value, x. into( ) ) ;
43+ assert_eq!( value, y. into( ) ) ;
44+ }
45+
3346 #[ test]
3447 fn vec( v: Vec <u8 >) {
3548 utils:: case( v) ;
You can’t perform that action at this time.
0 commit comments