From 511a989a52c39815775a4cf7aa228b93ef5e5634 Mon Sep 17 00:00:00 2001 From: Ilia Pozdnyakov Date: Sun, 13 Oct 2024 13:43:09 +0500 Subject: [PATCH] remove unneeded reader and writer references --- src/executable/mod.rs | 4 ++-- src/executable/xbe.rs | 6 +++--- src/executable/xex.rs | 10 +++++----- src/god/hash_list.rs | 2 +- src/iso/directory_table.rs | 12 ++++++------ src/iso/iso_type.rs | 12 ++++++------ src/iso/volume_descriptor.rs | 7 ++++--- 7 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/executable/mod.rs b/src/executable/mod.rs index 7cd2e10..70cfee1 100644 --- a/src/executable/mod.rs +++ b/src/executable/mod.rs @@ -25,7 +25,7 @@ pub struct TitleInfo { } impl TitleExecutionInfo { - pub fn from_xex(reader: &mut R) -> Result { + pub fn from_xex(mut reader: R) -> Result { Ok(TitleExecutionInfo { media_id: reader.read_u32::()?, version: reader.read_u32::()?, @@ -38,7 +38,7 @@ impl TitleExecutionInfo { }) } - pub fn from_xbe(reader: &mut R) -> Result { + pub fn from_xbe(mut reader: R) -> Result { reader.seek(SeekFrom::Current(8))?; let title_id = reader.read_u32::()?; diff --git a/src/executable/xbe.rs b/src/executable/xbe.rs index 92bf13f..8a0ab3f 100644 --- a/src/executable/xbe.rs +++ b/src/executable/xbe.rs @@ -16,8 +16,8 @@ pub struct XbeHeaderFields { } impl XbeHeader { - pub fn read(reader: &mut R) -> Result { - Self::check_magic_bytes(reader)?; + pub fn read(mut reader: R) -> Result { + Self::check_magic_bytes(&mut reader)?; // Offset 0x0104 reader.seek(SeekFrom::Current(256))?; @@ -41,7 +41,7 @@ impl XbeHeader { }) } - fn check_magic_bytes(reader: &mut R) -> Result<(), Error> { + fn check_magic_bytes(mut reader: R) -> Result<(), Error> { let mut magic_bytes = [0u8; 4]; reader.read_exact(&mut magic_bytes)?; diff --git a/src/executable/xex.rs b/src/executable/xex.rs index b5ff76d..23a5e31 100644 --- a/src/executable/xex.rs +++ b/src/executable/xex.rs @@ -76,12 +76,12 @@ enum XexHeaderFieldId { } impl XexHeader { - pub fn read(reader: &mut R) -> Result { - Self::check_magic_bytes(reader)?; + pub fn read(mut reader: R) -> Result { + Self::check_magic_bytes(&mut reader)?; Self::read_checked(reader) } - fn check_magic_bytes(reader: &mut R) -> Result<(), Error> { + fn check_magic_bytes(mut reader: R) -> Result<(), Error> { let mut buf = [0_u8; 4]; reader.read_exact(&mut buf)?; @@ -94,7 +94,7 @@ impl XexHeader { Ok(()) } - fn read_checked(reader: &mut R) -> Result { + fn read_checked(mut reader: R) -> Result { let header_offset = reader.stream_position()?; let _ = reader.read_u32::()?; @@ -122,7 +122,7 @@ impl XexHeader { Some(Key::ExecutionId) => { let offset = reader.stream_position()?; reader.seek(SeekFrom::Start(header_offset + (value as u64)))?; - fields.execution_info = Some(TitleExecutionInfo::from_xex(reader)?); + fields.execution_info = Some(TitleExecutionInfo::from_xex(&mut reader)?); reader.seek(SeekFrom::Start(offset))?; } diff --git a/src/god/hash_list.rs b/src/god/hash_list.rs index e2edcb5..e9c56ce 100644 --- a/src/god/hash_list.rs +++ b/src/god/hash_list.rs @@ -46,7 +46,7 @@ impl HashList { Sha1::digest(&self.buffer).into() } - pub fn write(&self, writer: &mut W) -> Result<(), Error> { + pub fn write(&self, mut writer: W) -> Result<(), Error> { writer.write_all(&self.buffer)?; Ok(()) } diff --git a/src/iso/directory_table.rs b/src/iso/directory_table.rs index a27cde7..7b1a86d 100644 --- a/src/iso/directory_table.rs +++ b/src/iso/directory_table.rs @@ -39,7 +39,7 @@ bitflags! { impl DirectoryTable { pub fn read_root( - reader: &mut R, + reader: R, volume: &VolumeDescriptor, ) -> Result { Self::read( @@ -51,7 +51,7 @@ impl DirectoryTable { } fn read( - reader: &mut R, + mut reader: R, volume: &VolumeDescriptor, sector: u32, size: u32, @@ -64,7 +64,7 @@ impl DirectoryTable { ((sector + sector_index) as u64) * volume.sector_size + volume.root_offset; reader.seek(SeekFrom::Start(sector_position))?; - while let Some(entry) = DirectoryEntry::read(reader, volume)? { + while let Some(entry) = DirectoryEntry::read(&mut reader, volume)? { entries.push(entry); } } @@ -85,7 +85,7 @@ impl DirectoryTable { impl DirectoryEntry { fn read( - reader: &mut R, + mut reader: R, volume: &VolumeDescriptor, ) -> Result, Error> { let subtree_left = reader.read_u16::()?; @@ -103,7 +103,7 @@ impl DirectoryEntry { let name_length = reader.read_u8()?; let mut name = vec![0_u8; name_length as usize]; - reader.take(name_length as u64).read(&mut name)?; + reader.by_ref().take(name_length as u64).read(&mut name)?; let name = String::from_utf8_lossy(&name).into_owned(); let alignment_mismatch = ((4 - reader.stream_position()? % 4) % 4) as i64; @@ -112,7 +112,7 @@ impl DirectoryEntry { let is_directory = attributes.contains(DirectoryEntryAttributes::DIRECTORY); let subdirectory = if is_directory { let reader_position = reader.stream_position()?; - let subdir = DirectoryTable::read(reader, &volume, sector, size)?; + let subdir = DirectoryTable::read(&mut reader, &volume, sector, size)?; reader.seek(SeekFrom::Start(reader_position))?; Some(subdir) } else { diff --git a/src/iso/iso_type.rs b/src/iso/iso_type.rs index ebc3c23..9349641 100644 --- a/src/iso/iso_type.rs +++ b/src/iso/iso_type.rs @@ -22,30 +22,30 @@ impl IsoType { } } - pub fn read(reader: &mut R) -> Result, Error> { - if Self::check(reader, IsoType::Xsf)? { + pub fn read(mut reader: R) -> Result, Error> { + if Self::check(&mut reader, IsoType::Xsf)? { return Ok(Some(IsoType::Xsf)); } - if Self::check(reader, IsoType::Xgd2)? { + if Self::check(&mut reader, IsoType::Xgd2)? { return Ok(Some(IsoType::Xgd2)); } - if Self::check(reader, IsoType::Xgd1)? { + if Self::check(&mut reader, IsoType::Xgd1)? { return Ok(Some(IsoType::Xgd1)); } // original code had no extra check here, simply returning Xgd3 as fallback // https://github.com/eliecharra/iso2god-cli/blob/a3b266a5/Chilano/Xbox360/Iso/GDF.cs#L268 - if Self::check(reader, IsoType::Xgd3)? { + if Self::check(&mut reader, IsoType::Xgd3)? { return Ok(Some(IsoType::Xgd3)); } Ok(None) } - fn check(reader: &mut R, iso_type: IsoType) -> Result { + fn check(mut reader: R, iso_type: IsoType) -> Result { let mut buf = [0_u8; 20]; reader.seek(SeekFrom::Start(0x20 * SECTOR_SIZE + iso_type.root_offset()))?; diff --git a/src/iso/volume_descriptor.rs b/src/iso/volume_descriptor.rs index 580a528..2d43e7a 100644 --- a/src/iso/volume_descriptor.rs +++ b/src/iso/volume_descriptor.rs @@ -20,13 +20,14 @@ pub struct VolumeDescriptor { } impl VolumeDescriptor { - pub fn read(reader: &mut R) -> Result { - let iso_type = IsoType::read(reader)?.ok_or_else(|| format_err!("invalid ISO format"))?; + pub fn read(mut reader: R) -> Result { + let iso_type = + IsoType::read(&mut reader)?.ok_or_else(|| format_err!("invalid ISO format"))?; Self::read_of_type(reader, iso_type) } fn read_of_type( - reader: &mut R, + mut reader: R, iso_type: IsoType, ) -> Result { reader.seek(SeekFrom::Start(0x20 * SECTOR_SIZE + iso_type.root_offset()))?;