Skip to content

Commit

Permalink
♻️ inlinize State
Browse files Browse the repository at this point in the history
  • Loading branch information
ChanTsune committed Aug 26, 2024
1 parent ba62a49 commit 8782a62
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions src/file_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,33 @@ pub(crate) struct UnprocessedEntry {
option: ReadOptions,
}

pub(crate) enum State {
pub(crate) enum Entry {
Loaded(LoadedEntry),
Unprocessed(UnprocessedEntry),
}

Check warning

Code scanning / clippy

large size difference between variants Warning

large size difference between variants

pub(crate) struct Entry(State);

impl Entry {
fn empty() -> Self {
Self(State::Loaded(LoadedEntry {
Self::Loaded(LoadedEntry {
data: Vec::new(),
xattrs: HashMap::new(),
}))
})
}

fn load(&mut self) -> &LoadedEntry {
if let State::Unprocessed(e) = &self.0 {
if let Self::Unprocessed(e) = &self {
let mut xattrs = HashMap::with_capacity(e.entry.xattrs().len());
for xattr in e.entry.xattrs() {
xattrs.insert(xattr.name().into(), xattr.value().into());
}
let mut buf = Vec::new();
let mut reader = e.entry.reader(e.option.clone()).unwrap();
reader.read_to_end(&mut buf).unwrap();
self.0 = State::Loaded(LoadedEntry { data: buf, xattrs });
*self = Self::Loaded(LoadedEntry { data: buf, xattrs });
}
match &self.0 {
State::Loaded(l) => l,
State::Unprocessed(_) => unreachable!(),
match self {
Self::Loaded(l) => l,
Self::Unprocessed(_) => unreachable!(),
}
}

Expand Down Expand Up @@ -150,10 +148,10 @@ impl File {
};
let option = ReadOptions::with_password(password);
let (data, raw_size) = if let Some(raw_size) = metadata.raw_file_size() {
let data = Entry(State::Unprocessed(UnprocessedEntry { entry, option }));
let data = Entry::Unprocessed(UnprocessedEntry { entry, option });
(data, raw_size as usize)
} else {
let mut data = Entry(State::Unprocessed(UnprocessedEntry { entry, option }));
let mut data = Entry::Unprocessed(UnprocessedEntry { entry, option });
let raw_size = data.as_slice().len();
(data, raw_size)
};
Expand Down

0 comments on commit 8782a62

Please sign in to comment.