@@ -42,112 +42,77 @@ pub impl ReaderImpl of ReaderTrait {
42
42
}
43
43
44
44
/// Reads the specified number of bytes (up to 16) as a big endian unsigned integer.
45
- fn read_num_bytes (ref self : Reader , num_bytes : u8 ) -> Result < u128 , Error > {
45
+ fn read_num_bytes (ref self : Reader , num_bytes : u8 ) -> u128 {
46
46
assert! (num_bytes <= 16 , " Reader::read_num_bytes: num_bytes is too large" );
47
47
if num_bytes <= self . num_current_bytes {
48
- let x = self . read_from_current (num_bytes );
49
- return Result :: Ok (x );
48
+ return self . read_from_current (num_bytes );
50
49
}
51
50
let num_low_bytes = num_bytes - self . num_current_bytes;
52
51
let high = self . current;
53
- self . fetch_next ()? ;
54
- let low = self . read_num_bytes (num_low_bytes )? ;
55
- let value = if num_low_bytes == 16 {
52
+ self . fetch_next ();
53
+ let low = self . read_num_bytes (num_low_bytes );
54
+ if num_low_bytes == 16 {
56
55
low
57
56
} else {
58
57
high * one_shift_left_bytes_u128 (num_low_bytes ) + low
59
- };
60
- Result :: Ok (value )
58
+ }
61
59
}
62
60
63
- fn read_u256 (ref self : Reader ) -> Result < u256 , Error > {
64
- let high = self . read_num_bytes (16 )? ;
65
- let low = self . read_num_bytes (16 )? ;
61
+ fn read_u256 (ref self : Reader ) -> u256 {
62
+ let high = self . read_num_bytes (16 );
63
+ let low = self . read_num_bytes (16 );
66
64
let value = u256 { high , low };
67
- Result :: Ok ( value )
65
+ value
68
66
}
69
- fn read_u160 (ref self : Reader ) -> Result <u256 , Error > {
70
- let high = self . read_num_bytes (4 )? ;
71
- let low = self . read_num_bytes (16 )? ;
72
- let value = u256 { high , low };
73
- Result :: Ok (value )
67
+ fn read_u160 (ref self : Reader ) -> u256 {
68
+ let high = self . read_num_bytes (4 );
69
+ let low = self . read_num_bytes (16 );
70
+ u256 { high , low }
74
71
}
75
- fn read_u128 (ref self : Reader ) -> Result < u128 , Error > {
72
+ fn read_u128 (ref self : Reader ) -> u128 {
76
73
self . read_num_bytes (16 )
77
74
}
78
- fn read_u64 (ref self : Reader ) -> Result <u64 , Error > {
79
- let value = self . read_num_bytes (8 )? . try_into (). expect (UNEXPECTED_OVERFLOW );
80
- Result :: Ok (value )
75
+ fn read_u64 (ref self : Reader ) -> u64 {
76
+ self . read_num_bytes (8 ). try_into (). expect (UNEXPECTED_OVERFLOW )
81
77
}
82
- fn read_u32 (ref self : Reader ) -> Result <u32 , Error > {
83
- let value = self . read_num_bytes (4 )? . try_into (). expect (UNEXPECTED_OVERFLOW );
84
- Result :: Ok (value )
78
+ fn read_u32 (ref self : Reader ) -> u32 {
79
+ self . read_num_bytes (4 ). try_into (). expect (UNEXPECTED_OVERFLOW )
85
80
}
86
- fn read_u16 (ref self : Reader ) -> Result <u16 , Error > {
87
- let value = self . read_num_bytes (2 )? . try_into (). expect (UNEXPECTED_OVERFLOW );
88
- Result :: Ok (value )
81
+ fn read_u16 (ref self : Reader ) -> u16 {
82
+ self . read_num_bytes (2 ). try_into (). expect (UNEXPECTED_OVERFLOW )
89
83
}
90
- fn read_u8 (ref self : Reader ) -> Result <u8 , Error > {
91
- let value = self . read_num_bytes (1 )? . try_into (). expect (UNEXPECTED_OVERFLOW );
92
- Result :: Ok (value )
84
+ fn read_u8 (ref self : Reader ) -> u8 {
85
+ self . read_num_bytes (1 ). try_into (). expect (UNEXPECTED_OVERFLOW )
93
86
}
94
87
95
88
// TODO: skip without calculating values
96
- fn skip (ref self : Reader , mut num_bytes : u8 ) -> Result <(), Error > {
97
- let mut result = Result :: Ok (());
89
+ fn skip (ref self : Reader , mut num_bytes : u8 ) {
98
90
while num_bytes > 0 {
99
91
if num_bytes > 16 {
100
- match self . read_num_bytes (16 ) {
101
- Result :: Ok (_ ) => {},
102
- Result :: Err (err ) => {
103
- result = Result :: Err (err );
104
- break ;
105
- }
106
- }
92
+ self . read_num_bytes (16 );
107
93
num_bytes -= 16 ;
108
94
} else {
109
- match self . read_num_bytes (num_bytes ) {
110
- Result :: Ok (_ ) => {},
111
- Result :: Err (err ) => {
112
- result = Result :: Err (err );
113
- break ;
114
- }
115
- }
116
- break ;
95
+ self . read_num_bytes (num_bytes );
117
96
}
118
- };
119
- result
97
+ }
120
98
}
121
99
122
100
/// Reads the specified number of bytes as a new byte array.
123
- fn read_byte_array (ref self : Reader , num_bytes : usize ) -> Result < ByteArray , Error > {
101
+ fn read_byte_array (ref self : Reader , num_bytes : usize ) -> ByteArray {
124
102
let mut array : Array <bytes31 > = array! [];
125
- let mut num_last_bytes = Option :: None ;
103
+ let mut num_last_bytes = 0 ;
126
104
let mut num_remaining_bytes = num_bytes ;
127
105
loop {
128
- let r = self . read_bytes_iteration (num_remaining_bytes , ref array );
129
- match r {
130
- Result :: Ok ((
131
- num_read , eof
132
- )) => {
133
- num_remaining_bytes -= num_read ;
134
- if eof {
135
- num_last_bytes = Option :: Some (Result :: Ok (num_read ));
136
- break ;
137
- }
138
- },
139
- Result :: Err (err ) => {
140
- num_last_bytes = Option :: Some (Result :: Err (err ));
141
- break ;
142
- }
106
+ let (num_read , eof ) = self . read_bytes_iteration (num_remaining_bytes , ref array );
107
+ num_remaining_bytes -= num_read ;
108
+ if eof {
109
+ num_last_bytes = num_read ;
110
+ break ;
143
111
}
144
112
};
145
- // `num_last_bytes` is always set to Some before break.
146
- let num_last_bytes = num_last_bytes . unwrap ()? ;
147
113
// num_last_bytes < 31
148
114
let num_last_bytes = num_last_bytes . try_into (). expect (UNEXPECTED_OVERFLOW );
149
- let array = ByteArrayImpl :: new (array , num_last_bytes );
150
- Result :: Ok (array )
115
+ ByteArrayImpl :: new (array , num_last_bytes )
151
116
}
152
117
153
118
/// Returns number of remaining bytes to read.
@@ -179,15 +144,18 @@ impl ReaderPrivateImpl of ReaderPrivateTrait {
179
144
/// Replenishes `self.current` and `self.num_current_bytes`.
180
145
/// This should only be called when all bytes from `self.current` has been read.
181
146
/// Returns `EOF` error if no more data is available.
182
- fn fetch_next (ref self : Reader ) -> Result <(), Error > {
147
+ fn fetch_next (ref self : Reader ) {
183
148
match self . next {
184
149
Option :: Some (next ) => {
185
150
self . next = Option :: None ;
186
151
self . current = next ;
187
152
self . num_current_bytes = 16 ;
188
153
},
189
154
Option :: None => {
190
- let (value , bytes ) = self . array. pop_front (). ok_or (Error :: UnexpectedEndOfInput )? ;
155
+ let (value , bytes ) = self
156
+ . array
157
+ . pop_front ()
158
+ . expect (Error :: UnexpectedEndOfInput . into ());
191
159
let value : u256 = value . into ();
192
160
if bytes > 16 {
193
161
self . current = value . high;
@@ -199,33 +167,31 @@ impl ReaderPrivateImpl of ReaderPrivateTrait {
199
167
}
200
168
},
201
169
}
202
- Result :: Ok (())
203
170
}
204
171
205
172
// Moved out from `read_bytes` because we cannot use `return` or `?` within a loop.
206
173
fn read_bytes_iteration (
207
174
ref self : Reader , num_bytes : usize , ref array : Array <bytes31 >
208
- ) -> Result < (usize , bool ), Error > {
175
+ ) -> (usize , bool ) {
209
176
if num_bytes >= 31 {
210
- let high = self . read_num_bytes (15 )? ;
211
- let low = self . read_num_bytes (16 )? ;
177
+ let high = self . read_num_bytes (15 );
178
+ let low = self . read_num_bytes (16 );
212
179
let value : felt252 = u256 { high , low }. try_into (). expect (UNEXPECTED_OVERFLOW );
213
180
array . append (value . try_into (). expect (UNEXPECTED_OVERFLOW ));
214
- Result :: Ok (( 31 , false ) )
181
+ ( 31 , false )
215
182
} else if num_bytes > 16 {
216
183
// num_bytes < 31
217
- let high = self
218
- . read_num_bytes ((num_bytes - 16 ). try_into (). expect (UNEXPECTED_OVERFLOW ))? ;
219
- let low = self . read_num_bytes (16 )? ;
184
+ let high = self . read_num_bytes ((num_bytes - 16 ). try_into (). expect (UNEXPECTED_OVERFLOW ));
185
+ let low = self . read_num_bytes (16 );
220
186
let value : felt252 = u256 { high , low }. try_into (). expect (UNEXPECTED_OVERFLOW );
221
187
array . append (value . try_into (). expect (UNEXPECTED_OVERFLOW ));
222
- Result :: Ok (( num_bytes , true ) )
188
+ ( num_bytes , true )
223
189
} else {
224
190
// bytes < 16
225
- let low = self . read_num_bytes (num_bytes . try_into (). expect (UNEXPECTED_OVERFLOW ))? ;
191
+ let low = self . read_num_bytes (num_bytes . try_into (). expect (UNEXPECTED_OVERFLOW ));
226
192
let value : felt252 = low . try_into (). expect (UNEXPECTED_OVERFLOW );
227
193
array . append (value . try_into (). expect (UNEXPECTED_OVERFLOW ));
228
- Result :: Ok (( num_bytes , true ) )
194
+ ( num_bytes , true )
229
195
}
230
196
}
231
197
}
0 commit comments