From 1c94f157fe250e880a40db7f2711a00dc0975368 Mon Sep 17 00:00:00 2001 From: Ding Date: Mon, 17 Nov 2025 09:49:11 +0800 Subject: [PATCH 1/2] impl drop --- lib/src/epub/reader.rs | 14 +++++--------- lib/src/mobi/reader.rs | 18 +++++++++++++++++- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/src/epub/reader.rs b/lib/src/epub/reader.rs index 6d02bf1..09e9ef6 100644 --- a/lib/src/epub/reader.rs +++ b/lib/src/epub/reader.rs @@ -708,25 +708,21 @@ fn has_epub_type(e: &BytesStart, value: &str) -> bool { } #[derive(Debug, Clone)] -struct EpubReader { +struct EpubReader { inner: zip::ZipArchive, } -// impl From> for EpubReader { -// fn from(value: Vec) -> Self { - -// EpubReader{ -// inner: -// } +impl Drop for EpubReader { + fn drop(&mut self) {} +} -// } -// } impl EpubReader { pub fn new(value: T) -> IResult { let r = zip::ZipArchive::new(value)?; Ok(EpubReader { inner: r }) } } + impl EpubReaderTrait for EpubReader { fn read(&mut self, book: &mut EpubBook) -> IResult<()> { let reader = &mut self.inner; diff --git a/lib/src/mobi/reader.rs b/lib/src/mobi/reader.rs index 50cf748..30bc5ff 100644 --- a/lib/src/mobi/reader.rs +++ b/lib/src/mobi/reader.rs @@ -394,7 +394,7 @@ where Ok(true) } -pub struct MobiReader { +pub struct MobiReader { reader: BufReader, pub(crate) pdb_header: PDBHeader, pub(crate) mobi_doc_header: MOBIDOCHeader, @@ -406,6 +406,13 @@ pub struct MobiReader { id: AtomicUsize, } +impl Drop for MobiReader { + fn drop(&mut self) { + self.release_memory(); + self.exth_header = None; + } +} + impl MobiReader { pub fn new(v: T) -> IResult> { // let fs = std::fs::File::open(file)?; @@ -649,6 +656,15 @@ impl MobiReader { Ok(pos) } + + pub fn release_memory(&mut self) { + if let Some(mut cache) = self.text_cache.take() { + // 显式清空 Vec,释放内存 + cache.clear(); + // Vec 会在离开作用域时自动释放,但 clear() 可以立即释放容量 + cache.shrink_to_fit(); + } + } } /// 文本分节 From b291ef89115bc180f249a87bee101adac4f0f145 Mon Sep 17 00:00:00 2001 From: Ding Date: Mon, 17 Nov 2025 10:24:37 +0800 Subject: [PATCH 2/2] Update core.rs --- lib/src/epub/core.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/src/epub/core.rs b/lib/src/epub/core.rs index c625bd3..93a3fab 100644 --- a/lib/src/epub/core.rs +++ b/lib/src/epub/core.rs @@ -605,6 +605,11 @@ impl Display for EpubBook { ) } } +impl Drop for EpubBook { + fn drop(&mut self) { + self.release_memory(); + } +} impl EpubBook { iepub_derive::option_string_method!(info, creator); @@ -744,7 +749,7 @@ impl EpubBook { self.assets.iter_mut() } - pub fn remove_assets(&mut self,index: usize){ + pub fn remove_assets(&mut self, index: usize) { self.assets.remove(index); } @@ -763,7 +768,7 @@ impl EpubBook { self.chapters.iter() } - pub fn remove_chapter(&mut self, index: usize){ + pub fn remove_chapter(&mut self, index: usize) { self.chapters.remove(index); } @@ -860,6 +865,16 @@ impl EpubBook { } } + pub fn release_memory(&mut self) { + self.reader = None; + self.chapters.clear(); + self.assets.clear(); + self.nav.clear(); + self.meta.clear(); + self.cover = None; + self.version = String::new(); + } + #[cfg(feature = "cache")] pub fn cache>(&self, file: T) -> IResult<()> { std::fs::write(file, serde_json::to_string(self).unwrap())?;