diff --git a/src/parser.rs b/src/parser.rs index e3dc6ab..34e426f 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,8 +1,6 @@ use std::{ - convert::TryInto, fmt::{self, Debug, Formatter}, io::{Cursor, Error, ErrorKind, Read, Result as IoResult, Seek, SeekFrom}, - mem::size_of, }; #[derive(Debug, Clone, Eq, PartialEq)] @@ -56,7 +54,7 @@ impl std::fmt::Display for Image { } fn parse_header(i: &mut impl Read) -> IoResult<(u32, u32)> { - i.tag(b"Xcur")?; + i.tag(*b"Xcur")?; let header = i.u32_le()?; let _version = i.u32_le()?; let ntoc = i.u32_le()?; @@ -77,10 +75,10 @@ fn parse_toc(i: &mut impl Read) -> IoResult { } fn parse_img(i: &mut impl Read) -> IoResult { - i.tag(&[0x24, 0x00, 0x00, 0x00])?; // Header size - i.tag(&[0x02, 0x00, 0xfd, 0xff])?; // Type + i.tag([0x24, 0x00, 0x00, 0x00])?; // Header size + i.tag([0x02, 0x00, 0xfd, 0xff])?; // Type let size = i.u32_le()?; - i.tag(&[0x01, 0x00, 0x00, 0x00])?; // Image version (1) + i.tag([0x01, 0x00, 0x00, 0x00])?; // Image version (1) let width = i.u32_le()?; let height = i.u32_le()?; let xhot = i.u32_le()?; @@ -164,7 +162,7 @@ pub fn parse_xcursor_stream(input: &mut R) -> IoResult IoResult<()>; + fn tag(&mut self, tag: [u8; 4]) -> IoResult<()>; /// Take a slice of bytes. fn take_bytes(&mut self, len: usize) -> IoResult>; @@ -174,8 +172,8 @@ trait StreamExt { } impl StreamExt for R { - fn tag(&mut self, tag: &[u8]) -> IoResult<()> { - let mut data = vec![0; tag.len()]; + fn tag(&mut self, tag: [u8; 4]) -> IoResult<()> { + let mut data = [0u8; 4]; self.read_exact(&mut data)?; if data != tag { Err(Error::new(ErrorKind::Other, "Tag mismatch")) @@ -191,8 +189,9 @@ impl StreamExt for R { } fn u32_le(&mut self) -> IoResult { - self.take_bytes(size_of::()) - .map(|bytes| u32::from_le_bytes(bytes.as_slice().try_into().unwrap())) + let mut data = [0u8; 4]; + self.read_exact(&mut data)?; + Ok(u32::from_le_bytes(data)) } }