Skip to content

Commit 55e9a00

Browse files
authored
Add parse functions for large integer types. (#11)
This commits adds parse functions for 64 and 128 bit integer types.
1 parent 2f94c7e commit 55e9a00

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/parse.rs

+48
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,54 @@ impl<Ref: AsRef<[u8]>> Parser<Ref> {
287287
self.parse_buf(&mut res)?;
288288
Ok(u32::from_be_bytes(res))
289289
}
290+
291+
/// Takes an `i64` from the beginning of the parser.
292+
///
293+
/// The value is converted from network byte order into the system’s own
294+
/// byte order if necessary. The parser is advanced by eight octets. If
295+
/// there aren’t enough octets left, leaves the parser untouched and
296+
/// returns an error instead.
297+
pub fn parse_i64(&mut self) -> Result<i64, ShortInput> {
298+
let mut res = [0; 8];
299+
self.parse_buf(&mut res)?;
300+
Ok(i64::from_be_bytes(res))
301+
}
302+
303+
/// Takes a `u64` from the beginning of the parser.
304+
///
305+
/// The value is converted from network byte order into the system’s own
306+
/// byte order if necessary. The parser is advanced by four octets. If
307+
/// there aren’t enough octets left, leaves the parser untouched and
308+
/// returns an error instead.
309+
pub fn parse_u64(&mut self) -> Result<u64, ShortInput> {
310+
let mut res = [0; 8];
311+
self.parse_buf(&mut res)?;
312+
Ok(u64::from_be_bytes(res))
313+
}
314+
315+
/// Takes a `i128` from the beginning of the parser.
316+
///
317+
/// The value is converted from network byte order into the system’s own
318+
/// byte order if necessary. The parser is advanced by sixteen octets. If
319+
/// there aren’t enough octets left, leaves the parser untouched and
320+
/// returns an error instead.
321+
pub fn parse_i128(&mut self) -> Result<i128, ShortInput> {
322+
let mut res = [0; 16];
323+
self.parse_buf(&mut res)?;
324+
Ok(i128::from_be_bytes(res))
325+
}
326+
327+
/// Takes a `u128` from the beginning of the parser.
328+
///
329+
/// The value is converted from network byte order into the system’s own
330+
/// byte order if necessary. The parser is advanced by sixteen octets. If
331+
/// there aren’t enough octets left, leaves the parser untouched and
332+
/// returns an error instead.
333+
pub fn parse_u128(&mut self) -> Result<u128, ShortInput> {
334+
let mut res = [0; 16];
335+
self.parse_buf(&mut res)?;
336+
Ok(u128::from_be_bytes(res))
337+
}
290338
}
291339

292340

0 commit comments

Comments
 (0)