Skip to content

Commit 0d8d04d

Browse files
webmaster128maurolacy
authored andcommitted
Let encode_sections always succeed
1 parent 25eac93 commit 0d8d04d

File tree

3 files changed

+31
-159
lines changed

3 files changed

+31
-159
lines changed

Diff for: packages/std/src/conversion.rs

+15-82
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,18 @@
1-
use std::any::type_name;
2-
use std::convert::TryInto;
3-
4-
use crate::errors::{StdError, StdResult};
5-
6-
pub fn to_u32<T: TryInto<u32> + ToString + Copy>(input: T) -> StdResult<u32> {
7-
input.try_into().map_err(|_| {
8-
StdError::conversion_err(type_name::<T>(), type_name::<u32>(), input.to_string())
9-
})
10-
}
11-
12-
#[cfg(test)]
13-
mod tests {
14-
use super::*;
15-
16-
#[test]
17-
fn to_u32_works_for_usize() {
18-
assert_eq!(to_u32(0usize).unwrap(), 0);
19-
assert_eq!(to_u32(1usize).unwrap(), 1);
20-
assert_eq!(to_u32(2147483647usize).unwrap(), 2147483647);
21-
assert_eq!(to_u32(2147483648usize).unwrap(), 2147483648);
22-
assert_eq!(to_u32(4294967295usize).unwrap(), 4294967295);
23-
24-
match to_u32(4294967296usize) {
25-
Err(StdError::ConversionErr {
26-
from_type,
27-
to_type,
28-
input,
29-
..
30-
}) => {
31-
assert_eq!(from_type, "usize");
32-
assert_eq!(to_type, "u32");
33-
assert_eq!(input, "4294967296");
34-
}
35-
Err(err) => panic!("unexpected error: {:?}", err),
36-
Ok(_) => panic!("must not succeed"),
37-
};
38-
}
39-
40-
#[test]
41-
fn to_u32_works_for_u64() {
42-
assert_eq!(to_u32(0u64).unwrap(), 0);
43-
assert_eq!(to_u32(1u64).unwrap(), 1);
44-
assert_eq!(to_u32(2147483647u64).unwrap(), 2147483647);
45-
assert_eq!(to_u32(2147483648u64).unwrap(), 2147483648);
46-
assert_eq!(to_u32(4294967295u64).unwrap(), 4294967295);
47-
48-
match to_u32(4294967296u64) {
49-
Err(StdError::ConversionErr {
50-
from_type,
51-
to_type,
52-
input,
53-
..
54-
}) => {
55-
assert_eq!(from_type, "u64");
56-
assert_eq!(to_type, "u32");
57-
assert_eq!(input, "4294967296");
58-
}
59-
Err(err) => panic!("unexpected error: {:?}", err),
60-
Ok(_) => panic!("must not succeed"),
61-
};
1+
/// Converts an input of type usize to u32.
2+
///
3+
/// On 32 bit platforms such as wasm32 this is just a safe cast.
4+
/// On other plaftforms the conversion panic for values larger than
5+
/// `u32::MAX`.
6+
#[inline]
7+
pub fn force_to_u32(input: usize) -> u32 {
8+
#[cfg(target_pointer_width = "32")]
9+
{
10+
// usize = u32 on this architecture
11+
input as u32
6212
}
63-
64-
#[test]
65-
fn to_u32_works_for_i32() {
66-
assert_eq!(to_u32(0i32).unwrap(), 0);
67-
assert_eq!(to_u32(1i32).unwrap(), 1);
68-
assert_eq!(to_u32(2147483647i32).unwrap(), 2147483647);
69-
70-
match to_u32(-1i32) {
71-
Err(StdError::ConversionErr {
72-
from_type,
73-
to_type,
74-
input,
75-
..
76-
}) => {
77-
assert_eq!(from_type, "i32");
78-
assert_eq!(to_type, "u32");
79-
assert_eq!(input, "-1");
80-
}
81-
Err(err) => panic!("unexpected error: {:?}", err),
82-
Ok(_) => panic!("must not succeed"),
83-
};
13+
#[cfg(not(target_pointer_width = "32"))]
14+
{
15+
use std::convert::TryInto;
16+
input.try_into().expect("Input exceeds u32 range")
8417
}
8518
}

Diff for: packages/std/src/errors/std_error.rs

-60
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,6 @@ pub enum StdError {
3333
#[cfg(feature = "backtraces")]
3434
backtrace: Backtrace,
3535
},
36-
#[error("Couldn't convert from {} to {}. Input: {}", from_type, to_type, input)]
37-
ConversionErr {
38-
from_type: String,
39-
to_type: String,
40-
input: String,
41-
#[cfg(feature = "backtraces")]
42-
backtrace: Backtrace,
43-
},
4436
/// Whenever there is no specific error type available
4537
#[error("Generic error: {msg}")]
4638
GenericErr {
@@ -116,20 +108,6 @@ impl StdError {
116108
}
117109
}
118110

119-
pub fn conversion_err<S: Into<String>, T: Into<String>, U: Into<String>>(
120-
from_type: S,
121-
to_type: T,
122-
input: U,
123-
) -> Self {
124-
StdError::ConversionErr {
125-
from_type: from_type.into(),
126-
to_type: to_type.into(),
127-
input: input.into(),
128-
#[cfg(feature = "backtraces")]
129-
backtrace: Backtrace::capture(),
130-
}
131-
}
132-
133111
pub fn generic_err<S: Into<String>>(msg: S) -> Self {
134112
StdError::GenericErr {
135113
msg: msg.into(),
@@ -235,26 +213,6 @@ impl PartialEq<StdError> for StdError {
235213
false
236214
}
237215
}
238-
StdError::ConversionErr {
239-
from_type,
240-
to_type,
241-
input,
242-
#[cfg(feature = "backtraces")]
243-
backtrace: _,
244-
} => {
245-
if let StdError::ConversionErr {
246-
from_type: rhs_from_type,
247-
to_type: rhs_to_type,
248-
input: rhs_input,
249-
#[cfg(feature = "backtraces")]
250-
backtrace: _,
251-
} = rhs
252-
{
253-
from_type == rhs_from_type && to_type == rhs_to_type && input == rhs_input
254-
} else {
255-
false
256-
}
257-
}
258216
StdError::GenericErr {
259217
msg,
260218
#[cfg(feature = "backtraces")]
@@ -456,24 +414,6 @@ mod tests {
456414
}
457415
}
458416

459-
#[test]
460-
fn conversion_err_works() {
461-
let error = StdError::conversion_err("i32", "u32", "-9");
462-
match error {
463-
StdError::ConversionErr {
464-
from_type,
465-
to_type,
466-
input,
467-
..
468-
} => {
469-
assert_eq!(from_type, "i32");
470-
assert_eq!(to_type, "u32");
471-
assert_eq!(input, "-9");
472-
}
473-
e => panic!("Unexpected error: {:?}", e),
474-
}
475-
}
476-
477417
#[test]
478418
fn invalid_base64_works_for_strings() {
479419
let error = StdError::invalid_base64("my text");

Diff for: packages/std/src/sections.rs

+16-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
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

Comments
 (0)