1- use crate :: conversion:: to_u32;
2- use crate :: errors:: StdResult ;
1+ use crate :: conversion:: force_to_u32;
32
43/// A sections decoder for the special case of two elements
54#[ allow( dead_code) ] // used in Wasm and tests only
@@ -22,18 +21,18 @@ pub fn decode_sections2(data: Vec<u8>) -> (Vec<u8>, Vec<u8>) {
2221/// section1 || section1_len || section2 || section2_len || section3 || section3_len || …
2322/// ```
2423#[ allow( dead_code) ] // used in Wasm and tests only
25- pub fn encode_sections ( sections : & [ Vec < u8 > ] ) -> StdResult < Vec < u8 > > {
24+ pub fn encode_sections ( sections : & [ Vec < u8 > ] ) -> Vec < u8 > {
2625 let mut out_len: usize = sections. iter ( ) . map ( |section| section. len ( ) ) . sum ( ) ;
2726 out_len += 4 * sections. len ( ) ;
2827 let mut out_data = Vec :: with_capacity ( out_len) ;
2928 for section in sections {
30- let section_len = to_u32 ( section. len ( ) ) ? . to_be_bytes ( ) ;
29+ let section_len = force_to_u32 ( section. len ( ) ) . to_be_bytes ( ) ;
3130 out_data. extend ( section) ;
3231 out_data. extend_from_slice ( & section_len) ;
3332 }
3433 debug_assert_eq ! ( out_data. len( ) , out_len) ;
3534 debug_assert_eq ! ( out_data. capacity( ) , out_len) ;
36- Ok ( out_data)
35+ out_data
3736}
3837
3938/// Splits data into the last section ("tail") and the rest.
@@ -109,37 +108,37 @@ mod tests {
109108
110109 #[ test]
111110 fn encode_sections_works_for_empty_sections ( ) {
112- let enc = encode_sections ( & [ ] ) . unwrap ( ) ;
111+ let enc = encode_sections ( & [ ] ) ;
113112 assert_eq ! ( enc, b"" as & [ u8 ] ) ;
114- let enc = encode_sections ( & [ vec ! [ ] ] ) . unwrap ( ) ;
113+ let enc = encode_sections ( & [ vec ! [ ] ] ) ;
115114 assert_eq ! ( enc, b"\0 \0 \0 \0 " as & [ u8 ] ) ;
116- let enc = encode_sections ( & [ vec ! [ ] , vec ! [ ] ] ) . unwrap ( ) ;
115+ let enc = encode_sections ( & [ vec ! [ ] , vec ! [ ] ] ) ;
117116 assert_eq ! ( enc, b"\0 \0 \0 \0 \0 \0 \0 \0 " as & [ u8 ] ) ;
118- let enc = encode_sections ( & [ vec ! [ ] , vec ! [ ] , vec ! [ ] ] ) . unwrap ( ) ;
117+ let enc = encode_sections ( & [ vec ! [ ] , vec ! [ ] , vec ! [ ] ] ) ;
119118 assert_eq ! ( enc, b"\0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 " as & [ u8 ] ) ;
120119 }
121120
122121 #[ test]
123122 fn encode_sections_works_for_one_element ( ) {
124- let enc = encode_sections ( & [ ] ) . unwrap ( ) ;
123+ let enc = encode_sections ( & [ ] ) ;
125124 assert_eq ! ( enc, b"" as & [ u8 ] ) ;
126- let enc = encode_sections ( & [ vec ! [ 0xAA ] ] ) . unwrap ( ) ;
125+ let enc = encode_sections ( & [ vec ! [ 0xAA ] ] ) ;
127126 assert_eq ! ( enc, b"\xAA \0 \0 \0 \x01 " as & [ u8 ] ) ;
128- let enc = encode_sections ( & [ vec ! [ 0xAA , 0xBB ] ] ) . unwrap ( ) ;
127+ let enc = encode_sections ( & [ vec ! [ 0xAA , 0xBB ] ] ) ;
129128 assert_eq ! ( enc, b"\xAA \xBB \0 \0 \0 \x02 " as & [ u8 ] ) ;
130- let enc = encode_sections ( & [ vec ! [ 0x9D ; 277 ] ] ) . unwrap ( ) ;
129+ let enc = encode_sections ( & [ vec ! [ 0x9D ; 277 ] ] ) ;
131130 assert_eq ! ( enc, b"\x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \0 \0 \x01 \x15 " as & [ u8 ] ) ;
132131 }
133132
134133 #[ test]
135134 fn encode_sections_works_for_multiple_elements ( ) {
136- let enc = encode_sections ( & [ vec ! [ 0xAA ] ] ) . unwrap ( ) ;
135+ let enc = encode_sections ( & [ vec ! [ 0xAA ] ] ) ;
137136 assert_eq ! ( enc, b"\xAA \0 \0 \0 \x01 " as & [ u8 ] ) ;
138- let enc = encode_sections ( & [ vec ! [ 0xAA ] , vec ! [ 0xDE , 0xDE ] ] ) . unwrap ( ) ;
137+ let enc = encode_sections ( & [ vec ! [ 0xAA ] , vec ! [ 0xDE , 0xDE ] ] ) ;
139138 assert_eq ! ( enc, b"\xAA \0 \0 \0 \x01 \xDE \xDE \0 \0 \0 \x02 " as & [ u8 ] ) ;
140- let enc = encode_sections ( & [ vec ! [ 0xAA ] , vec ! [ 0xDE , 0xDE ] , vec ! [ ] ] ) . unwrap ( ) ;
139+ let enc = encode_sections ( & [ vec ! [ 0xAA ] , vec ! [ 0xDE , 0xDE ] , vec ! [ ] ] ) ;
141140 assert_eq ! ( enc, b"\xAA \0 \0 \0 \x01 \xDE \xDE \0 \0 \0 \x02 \0 \0 \0 \0 " as & [ u8 ] ) ;
142- let enc = encode_sections ( & [ vec ! [ 0xAA ] , vec ! [ 0xDE , 0xDE ] , vec ! [ ] , vec ! [ 0xFF ; 19 ] ] ) . unwrap ( ) ;
141+ let enc = encode_sections ( & [ vec ! [ 0xAA ] , vec ! [ 0xDE , 0xDE ] , vec ! [ ] , vec ! [ 0xFF ; 19 ] ] ) ;
143142 assert_eq ! ( enc, b"\xAA \0 \0 \0 \x01 \xDE \xDE \0 \0 \0 \x02 \0 \0 \0 \0 \xFF \xFF \xFF \xFF \xFF \xFF \xFF \xFF \xFF \xFF \xFF \xFF \xFF \xFF \xFF \xFF \xFF \xFF \xFF \0 \0 \0 \x13 " as & [ u8 ] ) ;
144143 }
145144}
0 commit comments