diff options
Diffstat (limited to 'src/utils.rs')
-rw-r--r-- | src/utils.rs | 68 |
1 files changed, 6 insertions, 62 deletions
diff --git a/src/utils.rs b/src/utils.rs index fcfcb70..4223cac 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,70 +1,14 @@ -use crate::{DeserializeErr, DeserializeResult, Deserialized}; +use crate::{DeserializeErr, DeserializeResult}; use alloc::string::String; -pub fn read_one_byte(data: &[u8]) -> DeserializeResult<u8> { - match data.split_first() { - Some((byte, rest)) => Deserialized::ok(*byte, rest), - None => Err(DeserializeErr::Eof), - } -} - -pub fn take(amount: usize) -> impl for<'b> Fn(&'b [u8]) -> DeserializeResult<'b, &'b [u8]> { - move |data| { - if data.len() < amount { - Err(DeserializeErr::Eof) - } else { - Ok(data.split_at(amount).into()) - } +pub fn take(amount: usize, data: &[u8]) -> DeserializeResult<&[u8]> { + if data.len() < amount { + Err(DeserializeErr::Eof) + } else { + Ok(data.split_at(amount).into()) } } -pub fn read_long(data: &[u8]) -> DeserializeResult<u64> { - Ok(take(8)(data)?.map(move |bytes| { - (bytes[0] as u64) << 56 - | (bytes[1] as u64) << 48 - | (bytes[2] as u64) << 40 - | (bytes[3] as u64) << 32 - | (bytes[4] as u64) << 24 - | (bytes[5] as u64) << 16 - | (bytes[6] as u64) << 8 - | (bytes[7] as u64) - })) -} - -pub fn write_long(v: u64) -> [u8; 8] { - [ - (v >> 56) as u8, - (v >> 48) as u8, - (v >> 40) as u8, - (v >> 32) as u8, - (v >> 24) as u8, - (v >> 16) as u8, - (v >> 8) as u8, - v as u8, - ] -} - -pub fn read_int(data: &[u8]) -> DeserializeResult<u32> { - Ok(take(4)(data)?.map(move |bytes| { - (bytes[0] as u32) << 24 - | (bytes[1] as u32) << 16 - | (bytes[2] as u32) << 8 - | (bytes[3] as u32) - })) -} - -pub fn write_int(v: u32) -> [u8; 4] { - [(v >> 24) as u8, (v >> 16) as u8, (v >> 8) as u8, v as u8] -} - -pub fn read_short(data: &[u8]) -> DeserializeResult<u16> { - Ok(take(2)(data)?.map(move |bytes| (bytes[0] as u16) << 8 | (bytes[1] as u16))) -} - -pub fn write_short(v: u16) -> [u8; 2] { - [(v >> 8) as u8, v as u8] -} - pub fn hex(data: &[u8]) -> String { let mut str = String::with_capacity(data.len() * 2); for byte_ref in data { |