small memory usage improvement (v0.1.3)

This commit is contained in:
Philipp Schuster 2021-10-05 12:01:29 +02:00
parent 40e6da5e97
commit a8c44fb83d
3 changed files with 8 additions and 11 deletions

View File

@ -7,7 +7,7 @@ as GNU Longname. The maximum supported file name length is 100 characters includ
The maximum supported file size is 8GiB. Also, directories are not supported yet but only flat The maximum supported file size is 8GiB. Also, directories are not supported yet but only flat
collections of files. collections of files.
""" """
version = "0.1.2" version = "0.1.3"
edition = "2018" edition = "2018"
keywords = ["tar", "tarball", "archive"] keywords = ["tar", "tarball", "archive"]
categories = ["data-structures", "no-std", "parser-implementations"] categories = ["data-structures", "no-std", "parser-implementations"]

View File

@ -27,7 +27,6 @@ use crate::header::PosixHeader;
use crate::{TypeFlag, BLOCKSIZE}; use crate::{TypeFlag, BLOCKSIZE};
use arrayvec::ArrayString; use arrayvec::ArrayString;
use core::fmt::{Debug, Formatter}; use core::fmt::{Debug, Formatter};
use core::ptr;
use core::str::FromStr; use core::str::FromStr;
/// Describes an entry in an archive. /// Describes an entry in an archive.
@ -116,11 +115,10 @@ impl<'a> ArchiveIterator<'a> {
} }
} }
/// Returns a pointer to the next Header. /// Returns a reference to the next Header.
const fn next_hdr(&self, block_index: usize) -> *const PosixHeader { fn next_hdr(&self, block_index: usize) -> &'a PosixHeader {
let hdr_ptr = &self.archive.data[block_index * BLOCKSIZE]; let hdr_ptr = &self.archive.data[block_index * BLOCKSIZE];
let hdr_ptr = hdr_ptr as *const u8; unsafe { (hdr_ptr as *const u8).cast::<PosixHeader>().as_ref() }.unwrap()
hdr_ptr as *const PosixHeader
} }
} }
@ -134,11 +132,10 @@ impl<'a> Iterator for ArchiveIterator<'a> {
} }
let hdr = self.next_hdr(self.block_index); let hdr = self.next_hdr(self.block_index);
let hdr = unsafe { ptr::read(hdr) };
// check if we found end of archive // check if we found end of archive
if hdr.is_zero_block() { if hdr.is_zero_block() {
let next_hdr = unsafe { ptr::read(self.next_hdr(self.block_index + 1)) }; let next_hdr = self.next_hdr(self.block_index + 1);
if next_hdr.is_zero_block() { if next_hdr.is_zero_block() {
// gracefully terminated Archive // gracefully terminated Archive
log::debug!("End of Tar archive with two zero blocks!"); log::debug!("End of Tar archive with two zero blocks!");

View File

@ -284,9 +284,9 @@ mod tests {
use crate::BLOCKSIZE; use crate::BLOCKSIZE;
use std::mem::size_of; use std::mem::size_of;
fn bytes_to_archive(bytes: &[u8]) -> PosixHeader { /// Casts the bytes to a reference to a PosixhHeader.
let hdr = bytes.as_ptr() as *const PosixHeader; fn bytes_to_archive(bytes: &[u8]) -> &PosixHeader {
unsafe { core::ptr::read(hdr) } unsafe { (bytes.as_ptr() as *const PosixHeader).as_ref() }.unwrap()
} }
#[test] #[test]