Skip to content

Commit 60efdde

Browse files
committed
error fix #1
1 parent f279e44 commit 60efdde

1 file changed

Lines changed: 34 additions & 102 deletions

File tree

src/lib.rs

Lines changed: 34 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,8 @@ pub mod binary {
6969

7070
#[inline]
7171
pub fn get_bool(&mut self) -> bool {
72-
match self.get(1) {
73-
Ok(bytes) => bytes[0] != 0,
74-
Err(err) => {
75-
panic!("Error get_bool(): Not enough bytes, Error: {:?}", err);
76-
}
77-
}
72+
let bytes = self.get(1);
73+
bytes[0] != 0
7874
}
7975

8076
#[inline]
@@ -84,12 +80,8 @@ pub mod binary {
8480

8581
#[inline]
8682
pub fn get_byte(&mut self) -> u8 {
87-
match self.get(1) {
88-
Ok(bytes) => bytes[0],
89-
Err(err) => {
90-
panic!("Error get_byte(): Not enough bytes, Error: {:?}", err);
91-
}
92-
}
83+
let bytes = self.get(1);
84+
bytes[0]
9385
}
9486

9587
#[inline]
@@ -100,22 +92,14 @@ pub mod binary {
10092
// Big-endian (network byte order)
10193
#[inline]
10294
pub fn get_be_unsigned_short(&mut self) -> u16 {
103-
match self.get(2) {
104-
Ok(bytes) => u16::from_be_bytes([bytes[0], bytes[1]]),
105-
Err(err) => {
106-
panic!("Error get_short(): Not enough bytes, Error: {:?}", err);
107-
}
108-
}
95+
let bytes = self.get(2);
96+
u16::from_be_bytes([bytes[0], bytes[1]])
10997
}
11098

11199
#[inline]
112100
pub fn get_be_signed_short(&mut self) -> i16 {
113-
match self.get(2) {
114-
Ok(bytes) => i16::from_be_bytes([bytes[0], bytes[1]]),
115-
Err(err) => {
116-
panic!("Error get_signed_short(): Not enough bytes, Error: {:?}", err);
117-
}
118-
}
101+
let bytes = self.get(2);
102+
i16::from_be_bytes([bytes[0], bytes[1]])
119103
}
120104

121105
#[inline]
@@ -131,22 +115,14 @@ pub mod binary {
131115
// Little-endian
132116
#[inline]
133117
pub fn get_le_unsigned_short(&mut self) -> u16 {
134-
match self.get(2) {
135-
Ok(bytes) => u16::from_le_bytes([bytes[0], bytes[1]]),
136-
Err(err) => {
137-
panic!("Error get_l_short(): Not enough bytes, Error: {:?}", err);
138-
}
139-
}
118+
let bytes = self.get(2);
119+
u16::from_le_bytes([bytes[0], bytes[1]])
140120
}
141121

142122
#[inline]
143123
pub fn get_le_signed_short(&mut self) -> i16 {
144-
match self.get(2) {
145-
Ok(bytes) => i16::from_le_bytes([bytes[0], bytes[1]]),
146-
Err(err) => {
147-
panic!("Error get_signed_l_short(): Not enough bytes, Error: {:?}", err);
148-
}
149-
}
124+
let bytes = self.get(2);
125+
i16::from_le_bytes([bytes[0], bytes[1]])
150126
}
151127

152128
#[inline]
@@ -162,12 +138,8 @@ pub mod binary {
162138
// 24-bit integers (Triad) - Big-endian
163139
#[inline]
164140
pub fn get_be_triad(&mut self) -> i32 {
165-
match self.get(3) {
166-
Ok(bytes) => i32::from_be_bytes([0, bytes[0], bytes[1], bytes[2]]),
167-
Err(err) => {
168-
panic!("Error get_triad(): Not enough bytes, Error: {:?}", err);
169-
}
170-
}
141+
let bytes = self.get(3);
142+
i32::from_be_bytes([0, bytes[0], bytes[1], bytes[2]])
171143
}
172144

173145
#[inline]
@@ -179,12 +151,8 @@ pub mod binary {
179151
// 24-bit integers (Triad) - Little-endian
180152
#[inline]
181153
pub fn get_le_triad(&mut self) -> i32 {
182-
match self.get(3) {
183-
Ok(bytes) => i32::from_le_bytes([bytes[0], bytes[1], bytes[2], 0]),
184-
Err(err) => {
185-
panic!("Error get_l_triad(): Not enough bytes, Error: {:?}", err);
186-
}
187-
}
154+
let bytes = self.get(3);
155+
i32::from_le_bytes([bytes[0], bytes[1], bytes[2], 0])
188156
}
189157

190158
#[inline]
@@ -196,12 +164,8 @@ pub mod binary {
196164
// 32-bit integers - Big-endian
197165
#[inline]
198166
pub fn get_be_unsigned_int(&mut self) -> u32 {
199-
match self.get(4) {
200-
Ok(bytes) => u32::from_be_bytes(bytes.try_into().unwrap()),
201-
Err(err) => {
202-
panic!("Error get_int(): Not enough bytes, Error: {:?}", err);
203-
}
204-
}
167+
let bytes = self.get(4);
168+
u32::from_be_bytes(bytes.try_into().unwrap())
205169
}
206170

207171
#[inline]
@@ -212,12 +176,8 @@ pub mod binary {
212176
// 32-bit integers - Little-endian
213177
#[inline]
214178
pub fn get_le_unsigned_int(&mut self) -> u32 {
215-
match self.get(4) {
216-
Ok(bytes) => u32::from_le_bytes(bytes.try_into().unwrap()),
217-
Err(err) => {
218-
panic!("Error get_l_int(): Not enough bytes, Error: {:?}", err);
219-
}
220-
}
179+
let bytes = self.get(4);
180+
u32::from_le_bytes(bytes.try_into().unwrap())
221181
}
222182

223183
#[inline]
@@ -228,12 +188,8 @@ pub mod binary {
228188
// 32-bit floats - Big-endian
229189
#[inline]
230190
pub fn get_be_float(&mut self) -> f32 {
231-
match self.get(4) {
232-
Ok(bytes) => f32::from_be_bytes(bytes.try_into().unwrap()),
233-
Err(err) => {
234-
panic!("Error get_float(): Not enough bytes, Error: {:?}", err);
235-
}
236-
}
191+
let bytes = self.get(4);
192+
f32::from_be_bytes(bytes.try_into().unwrap())
237193
}
238194

239195
#[inline]
@@ -244,12 +200,8 @@ pub mod binary {
244200
// 32-bit floats - Little-endian
245201
#[inline]
246202
pub fn get_le_float(&mut self) -> f32 {
247-
match self.get(4) {
248-
Ok(bytes) => f32::from_le_bytes(bytes.try_into().unwrap()),
249-
Err(err) => {
250-
panic!("Error get_l_float(): Not enough bytes, Error: {:?}", err);
251-
}
252-
}
203+
let bytes = self.get(4);
204+
f32::from_le_bytes(bytes.try_into().unwrap())
253205
}
254206

255207
#[inline]
@@ -260,12 +212,8 @@ pub mod binary {
260212
// 64-bit floats - Big-endian
261213
#[inline]
262214
pub fn get_be_double(&mut self) -> f64 {
263-
match self.get(8) {
264-
Ok(bytes) => f64::from_be_bytes(bytes.try_into().unwrap()),
265-
Err(err) => {
266-
panic!("Error get_double(): Not enough bytes, Error: {:?}", err);
267-
}
268-
}
215+
let bytes = self.get(8);
216+
f64::from_be_bytes(bytes.try_into().unwrap())
269217
}
270218

271219
#[inline]
@@ -276,12 +224,8 @@ pub mod binary {
276224
// 64-bit floats - Little-endian
277225
#[inline]
278226
pub fn get_le_double(&mut self) -> f64 {
279-
match self.get(8) {
280-
Ok(bytes) => f64::from_le_bytes(bytes.try_into().unwrap()),
281-
Err(err) => {
282-
panic!("Error get_l_double(): Not enough bytes, Error: {:?}", err);
283-
}
284-
}
227+
let bytes = self.get(8);
228+
f64::from_le_bytes(bytes.try_into().unwrap())
285229
}
286230

287231
#[inline]
@@ -292,12 +236,8 @@ pub mod binary {
292236
// 64-bit integers - Big-endian
293237
#[inline]
294238
pub fn get_be_signed_long(&mut self) -> i64 {
295-
match self.get(8) {
296-
Ok(bytes) => i64::from_be_bytes(bytes.try_into().unwrap()),
297-
Err(err) => {
298-
panic!("Error get_long(): Not enough bytes, Error: {:?}", err);
299-
}
300-
}
239+
let bytes = self.get(8);
240+
i64::from_be_bytes(bytes.try_into().unwrap())
301241
}
302242

303243
#[inline]
@@ -308,12 +248,8 @@ pub mod binary {
308248
// 64-bit integers - Little-endian
309249
#[inline]
310250
pub fn get_le_signed_long(&mut self) -> i64 {
311-
match self.get(8) {
312-
Ok(bytes) => i64::from_le_bytes(bytes.try_into().unwrap()),
313-
Err(err) => {
314-
panic!("Error get_l_long(): Not enough bytes, Error: {:?}", err);
315-
}
316-
}
251+
let bytes = self.get(8);
252+
i64::from_le_bytes(bytes.try_into().unwrap())
317253
}
318254

319255
#[inline]
@@ -384,12 +320,8 @@ pub mod binary {
384320

385321
#[inline]
386322
pub fn get_var_long(&mut self) -> i64 {
387-
match self.get_unsigned_var_long() {
388-
Ok(raw) => ((raw >> 1) as i64) ^ (-((raw & 1) as i64)),
389-
Err(err) => {
390-
panic!("Error get_var_long(): Failed to read unsigned var long, Error: {:?}", err);
391-
}
392-
}
323+
let raw = self.get_unsigned_var_long();
324+
((raw >> 1) as i64) ^ (-((raw & 1) as i64))
393325
}
394326

395327
#[inline]

0 commit comments

Comments
 (0)