From f51f2be18f38113b1c22079780d8ee0928e65d8a Mon Sep 17 00:00:00 2001 From: Dmitry Date: Sun, 9 Oct 2022 18:26:00 +0300 Subject: [PATCH] add db::Writer::load --- src/db.rs | 13 +++++++++++++ src/main.rs | 12 ++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/db.rs b/src/db.rs index 17ddf25..a2d92cd 100644 --- a/src/db.rs +++ b/src/db.rs @@ -79,6 +79,7 @@ where _t: PhantomData, }) } + pub fn push(&mut self, item: T) -> Result<(), String> { let pos: LSize = self.data_buf.position() as LSize; @@ -102,6 +103,18 @@ where Ok(()) } + + pub fn load(&mut self, iter: &mut I) -> Result<(), String> + where + I: Iterator, + { + for item in iter { + self.push(item)?; + } + + Ok(()) + } + pub fn finish(mut self) -> Result<(), String> { // finish tab let pos: LSize = self.data_buf.position() as LSize; diff --git a/src/main.rs b/src/main.rs index 5ba9a27..1bab6d5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -269,16 +269,16 @@ fn db_writer2_task(rx: mpsc::Receiver) { db::Writer::new(NEW_DB_FILENAME, writer_opts).expect("new db writer"); let mut num = 1; - rx.into_iter().for_each(|mut q| { + let mut iter = rx.iter().map(|mut q| { q.num = num; - - writer - .push(q) - .unwrap_or_else(|e| panic!("db writer push, num={num}, {e:#?}")); - num += 1; + q }); + writer + .load(&mut iter) + .unwrap_or_else(|e| panic!("db writer push, num={num}, {e:#?}")); + writer.finish().expect("db writer finish"); println!("write done");