1
- use crate :: conversion:: to_u32;
2
- use crate :: errors:: StdResult ;
1
+ use crate :: conversion:: force_to_u32;
3
2
4
3
/// A sections decoder for the special case of two elements
5
4
#[ allow( dead_code) ] // used in Wasm and tests only
@@ -22,18 +21,18 @@ pub fn decode_sections2(data: Vec<u8>) -> (Vec<u8>, Vec<u8>) {
22
21
/// section1 || section1_len || section2 || section2_len || section3 || section3_len || …
23
22
/// ```
24
23
#[ 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 > {
26
25
let mut out_len: usize = sections. iter ( ) . map ( |section| section. len ( ) ) . sum ( ) ;
27
26
out_len += 4 * sections. len ( ) ;
28
27
let mut out_data = Vec :: with_capacity ( out_len) ;
29
28
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 ( ) ;
31
30
out_data. extend ( section) ;
32
31
out_data. extend_from_slice ( & section_len) ;
33
32
}
34
33
debug_assert_eq ! ( out_data. len( ) , out_len) ;
35
34
debug_assert_eq ! ( out_data. capacity( ) , out_len) ;
36
- Ok ( out_data)
35
+ out_data
37
36
}
38
37
39
38
/// Splits data into the last section ("tail") and the rest.
@@ -109,37 +108,37 @@ mod tests {
109
108
110
109
#[ test]
111
110
fn encode_sections_works_for_empty_sections ( ) {
112
- let enc = encode_sections ( & [ ] ) . unwrap ( ) ;
111
+ let enc = encode_sections ( & [ ] ) ;
113
112
assert_eq ! ( enc, b"" as & [ u8 ] ) ;
114
- let enc = encode_sections ( & [ vec ! [ ] ] ) . unwrap ( ) ;
113
+ let enc = encode_sections ( & [ vec ! [ ] ] ) ;
115
114
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 ! [ ] ] ) ;
117
116
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 ! [ ] ] ) ;
119
118
assert_eq ! ( enc, b"\0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 " as & [ u8 ] ) ;
120
119
}
121
120
122
121
#[ test]
123
122
fn encode_sections_works_for_one_element ( ) {
124
- let enc = encode_sections ( & [ ] ) . unwrap ( ) ;
123
+ let enc = encode_sections ( & [ ] ) ;
125
124
assert_eq ! ( enc, b"" as & [ u8 ] ) ;
126
- let enc = encode_sections ( & [ vec ! [ 0xAA ] ] ) . unwrap ( ) ;
125
+ let enc = encode_sections ( & [ vec ! [ 0xAA ] ] ) ;
127
126
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 ] ] ) ;
129
128
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 ] ] ) ;
131
130
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 ] ) ;
132
131
}
133
132
134
133
#[ test]
135
134
fn encode_sections_works_for_multiple_elements ( ) {
136
- let enc = encode_sections ( & [ vec ! [ 0xAA ] ] ) . unwrap ( ) ;
135
+ let enc = encode_sections ( & [ vec ! [ 0xAA ] ] ) ;
137
136
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 ] ] ) ;
139
138
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 ! [ ] ] ) ;
141
140
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 ] ] ) ;
143
142
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 ] ) ;
144
143
}
145
144
}
0 commit comments