Skip to content

Commit

Permalink
small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sokorototo committed May 3, 2024
1 parent 2fd26c8 commit 83d4cf0
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions vach/src/loader/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,28 +248,28 @@ where
}
}

/// Given a data source and a [`RegistryEntry`], gets the adjacent raw data
pub(crate) fn read_entry<T: Seek + Read>(handle: &mut T, entry: &RegistryEntry) -> InternalResult<Vec<u8>> {
let mut buffer = Vec::with_capacity(entry.offset as usize + 64);
handle.seek(SeekFrom::Start(entry.location))?;

let mut take = handle.take(entry.offset);
take.read_to_end(&mut buffer)?;

Ok(buffer)
}

impl<T> Archive<T>
where
T: Read + Seek,
{
/// Given a data source and a [`RegistryEntry`], gets the adjacent raw data
pub(crate) fn read_raw(handle: &mut T, entry: &RegistryEntry) -> InternalResult<Vec<u8>> {
let mut buffer = Vec::with_capacity(entry.offset as usize + 64);
handle.seek(SeekFrom::Start(entry.location))?;

let mut take = handle.take(entry.offset);
take.read_to_end(&mut buffer)?;

Ok(buffer)
}

/// Cheaper alternative to `fetch` that works best for single threaded applications.
/// It does not lock the underlying [Mutex], since it requires a mutable reference.
/// Therefore the borrow checker statically guarantees the operation is safe. Refer to [`Mutex::get_mut`](Mutex).
pub fn fetch_mut(&mut self, id: impl AsRef<str>) -> InternalResult<Resource> {
// The reason for this function's unnecessary complexity is it uses the provided functions independently, thus preventing an unnecessary allocation [MAYBE TOO MUCH?]
if let Some(entry) = self.fetch_entry(&id) {
let raw = read_entry(self.handle.get_mut(), &entry)?;
let raw = Archive::read_raw(self.handle.get_mut(), &entry)?;

// Prepare contextual variables
// Decompress and|or decrypt the data
Expand All @@ -293,7 +293,7 @@ where
if let Some(entry) = self.fetch_entry(&id) {
let raw = {
let mut guard = self.handle.lock();
read_entry(guard.by_ref(), &entry)?
Archive::read_raw(guard.by_ref(), &entry)?
};

// Prepare contextual variables
Expand Down

0 comments on commit 83d4cf0

Please sign in to comment.