From fadb3634e0b48f37ee7fa708268ef8b5db1ae19f Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Mon, 20 Jul 2020 15:25:35 +0300 Subject: [PATCH] melib: take MailboxHash instead of &Mailbox in fetch*() --- melib/src/backends.rs | 4 ++-- melib/src/backends/imap.rs | 5 ++--- melib/src/backends/jmap.rs | 3 +-- melib/src/backends/maildir/backend.rs | 22 ++++++---------------- melib/src/backends/mbox.rs | 3 +-- melib/src/backends/notmuch.rs | 3 +-- src/conf/accounts.rs | 24 ++++++++++++------------ src/plugins/backend.rs | 3 +-- 8 files changed, 26 insertions(+), 41 deletions(-) diff --git a/melib/src/backends.rs b/melib/src/backends.rs index 88c4dfb9..554b38f0 100644 --- a/melib/src/backends.rs +++ b/melib/src/backends.rs @@ -294,10 +294,10 @@ pub trait MailBackend: ::std::fmt::Debug + Send + Sync { fn is_online_async(&self) -> ResultFuture<()> { Err(MeliError::new("Unimplemented.")) } - fn fetch(&mut self, mailbox: &Mailbox) -> Result>>>; + fn fetch(&mut self, mailbox_hash: MailboxHash) -> Result>>>; fn fetch_async( &mut self, - _mailbox: &Mailbox, + _mailbox_hash: MailboxHash, ) -> Result>> + Send + 'static>>> { Err(MeliError::new("Unimplemented.")) } diff --git a/melib/src/backends/imap.rs b/melib/src/backends/imap.rs index 7de9bd4d..e35da39b 100644 --- a/melib/src/backends/imap.rs +++ b/melib/src/backends/imap.rs @@ -190,11 +190,10 @@ impl MailBackend for ImapType { fn fetch_async( &mut self, - mailbox: &Mailbox, + mailbox_hash: MailboxHash, ) -> Result>> + Send + 'static>>> { let uid_store = self.uid_store.clone(); let can_create_flags = self.can_create_flags.clone(); - let mailbox_hash = mailbox.hash(); let connection = self.connection.clone(); let mut max_uid: Option = None; let mut valid_hash_set: HashSet = HashSet::default(); @@ -297,7 +296,7 @@ impl MailBackend for ImapType { })) } - fn fetch(&mut self, _mailbox: &Mailbox) -> Result>>> { + fn fetch(&mut self, _mailbox_hash: MailboxHash) -> Result>>> { Err(MeliError::new("Unimplemented.")) } diff --git a/melib/src/backends/jmap.rs b/melib/src/backends/jmap.rs index 8bd7166c..f065ad19 100644 --- a/melib/src/backends/jmap.rs +++ b/melib/src/backends/jmap.rs @@ -212,13 +212,12 @@ impl MailBackend for JmapType { self.online.lock().unwrap().1.clone() } - fn fetch(&mut self, mailbox: &Mailbox) -> Result>>> { + fn fetch(&mut self, mailbox_hash: MailboxHash) -> Result>>> { let mut w = AsyncBuilder::new(); let mailboxes = self.mailboxes.clone(); let store = self.store.clone(); let tag_index = self.tag_index.clone(); let connection = self.connection.clone(); - let mailbox_hash = mailbox.hash(); let handle = { let tx = w.tx(); let closure = move |_work_context| { diff --git a/melib/src/backends/maildir/backend.rs b/melib/src/backends/maildir/backend.rs index dd8a2546..99973a7a 100644 --- a/melib/src/backends/maildir/backend.rs +++ b/melib/src/backends/maildir/backend.rs @@ -207,17 +207,16 @@ impl MailBackend for MaildirType { Ok(Box::pin(async { res })) } - fn fetch(&mut self, mailbox: &Mailbox) -> Result>>> { - Ok(self.multicore(4, mailbox)) + fn fetch(&mut self, mailbox_hash: MailboxHash) -> Result>>> { + Ok(self.multicore(4, mailbox_hash)) } fn fetch_async( &mut self, - mailbox: &Mailbox, + mailbox_hash: MailboxHash, ) -> Result>> + Send + 'static>>> { - let mailbox: &MaildirMailbox = &self.mailboxes[&self.owned_mailbox_idx(mailbox)]; - let mailbox_hash = mailbox.hash(); + let mailbox: &MaildirMailbox = &self.mailboxes[&mailbox_hash]; let unseen = mailbox.unseen.clone(); let total = mailbox.total.clone(); let path: PathBuf = mailbox.fs_path().into(); @@ -928,23 +927,14 @@ impl MaildirType { path: root_path, })) } - fn owned_mailbox_idx(&self, mailbox: &Mailbox) -> MailboxHash { - *self - .mailboxes - .iter() - .find(|(_, f)| f.hash() == mailbox.hash()) - .unwrap() - .0 - } - pub fn multicore(&mut self, cores: usize, mailbox: &Mailbox) -> Async>> { + pub fn multicore(&mut self, cores: usize, mailbox_hash: MailboxHash) -> Async>> { let mut w = AsyncBuilder::new(); let cache_dir = xdg::BaseDirectories::with_profile("meli", &self.name).unwrap(); let handle = { let tx = w.tx(); - let mailbox: &MaildirMailbox = &self.mailboxes[&self.owned_mailbox_idx(mailbox)]; - let mailbox_hash = mailbox.hash(); + let mailbox: &MaildirMailbox = &self.mailboxes[&mailbox_hash]; let unseen = mailbox.unseen.clone(); let total = mailbox.total.clone(); let tx_final = w.tx(); diff --git a/melib/src/backends/mbox.rs b/melib/src/backends/mbox.rs index 3aba0fdd..cc043e1f 100644 --- a/melib/src/backends/mbox.rs +++ b/melib/src/backends/mbox.rs @@ -718,11 +718,10 @@ impl MailBackend for MboxType { Ok(()) } - fn fetch(&mut self, mailbox: &Mailbox) -> Result>>> { + fn fetch(&mut self, mailbox_hash: MailboxHash) -> Result>>> { let mut w = AsyncBuilder::new(); let handle = { let tx = w.tx(); - let mailbox_hash = mailbox.hash(); let mailbox_index = self.mailbox_index.clone(); let mailboxes = self.mailboxes.clone(); let mailbox_path = mailboxes.lock().unwrap()[&mailbox_hash].fs_path.clone(); diff --git a/melib/src/backends/notmuch.rs b/melib/src/backends/notmuch.rs index c8481a25..3ad4d24f 100644 --- a/melib/src/backends/notmuch.rs +++ b/melib/src/backends/notmuch.rs @@ -334,9 +334,8 @@ impl MailBackend for NotmuchDb { Ok(()) } - fn fetch(&mut self, mailbox: &Mailbox) -> Result>>> { + fn fetch(&mut self, mailbox_hash: MailboxHash) -> Result>>> { let mut w = AsyncBuilder::new(); - let mailbox_hash = mailbox.hash(); let database = NotmuchDb::new_connection(self.path.as_path(), self.lib.clone(), false); let index = self.index.clone(); let mailbox_index = self.mailbox_index.clone(); diff --git a/src/conf/accounts.rs b/src/conf/accounts.rs index e248cbe3..0984060f 100644 --- a/src/conf/accounts.rs +++ b/src/conf/accounts.rs @@ -531,7 +531,7 @@ impl Account { if entry.conf.mailbox_conf.autoload { entry.status = MailboxStatus::Parsing(0, 0); if self.is_async { - if let Ok(mailbox_job) = self.backend.write().unwrap().fetch_async(&f) { + if let Ok(mailbox_job) = self.backend.write().unwrap().fetch_async(*h) { let mailbox_job = mailbox_job.into_future(); let (rcvr, handle, job_id) = self.job_executor.spawn_specialized(mailbox_job); @@ -545,7 +545,7 @@ impl Account { } } else { entry.worker = match Account::new_worker( - f.clone(), + &f, &mut self.backend, &self.work_context, self.notify_fn.clone(), @@ -571,15 +571,15 @@ impl Account { } fn new_worker( - mailbox: Mailbox, + mailbox: &Mailbox, backend: &Arc>>, work_context: &WorkContext, notify_fn: Arc, ) -> Result { - let mut mailbox_handle = backend.write().unwrap().fetch(&mailbox)?; + let mailbox_hash = mailbox.hash(); + let mut mailbox_handle = backend.write().unwrap().fetch(mailbox_hash)?; let mut builder = AsyncBuilder::new(); let our_tx = builder.tx(); - let mailbox_hash = mailbox.hash(); let priority = match mailbox.special_usage() { SpecialUsageMailbox::Inbox => 0, SpecialUsageMailbox::Sent => 1, @@ -601,9 +601,9 @@ impl Account { * workers causing no actual parsing to be done. If we could yield from within the worker * threads' closures this could be avoided, but it requires green threads. */ + let name = format!("Parsing {}", mailbox.path()); builder.set_priority(priority).set_is_static(true); let mut w = builder.build(Box::new(move |work_context| { - let name = format!("Parsing {}", mailbox.path()); let work = mailbox_handle.work().unwrap(); work_context.new_work.send(work).unwrap(); let thread_id = std::thread::current().id(); @@ -821,7 +821,7 @@ impl Account { } RefreshEventKind::Rescan => { let handle = match Account::new_worker( - self.mailbox_entries[&mailbox_hash].ref_mailbox.clone(), + &self.mailbox_entries[&mailbox_hash].ref_mailbox, &mut self.backend, &self.work_context, self.notify_fn.clone(), @@ -1013,9 +1013,9 @@ impl Account { MailboxStatus::None => { if self.is_async { if !self.active_jobs.values().any(|j| j.is_fetch(mailbox_hash)) { - match self.backend.write().unwrap().fetch_async( - &&self.mailbox_entries[&mailbox_hash].ref_mailbox, - ) { + let mailbox_job = + self.backend.write().unwrap().fetch_async(mailbox_hash); + match mailbox_job { Ok(mailbox_job) => { let mailbox_job = mailbox_job.into_future(); let (rcvr, handle, job_id) = @@ -1046,7 +1046,7 @@ impl Account { } } else if self.mailbox_entries[&mailbox_hash].worker.is_none() { let handle = match Account::new_worker( - self.mailbox_entries[&mailbox_hash].ref_mailbox.clone(), + &self.mailbox_entries[&mailbox_hash].ref_mailbox, &mut self.backend, &self.work_context, self.notify_fn.clone(), @@ -1396,7 +1396,7 @@ impl Account { }); } let (status, worker) = match Account::new_worker( - mailboxes[&mailbox_hash].clone(), + &mailboxes[&mailbox_hash], &mut self.backend, &self.work_context, self.notify_fn.clone(), diff --git a/src/plugins/backend.rs b/src/plugins/backend.rs index a99346b7..fc6429cf 100644 --- a/src/plugins/backend.rs +++ b/src/plugins/backend.rs @@ -98,9 +98,8 @@ impl MailBackend for PluginBackend { } } - fn fetch(&mut self, mailbox: &Mailbox) -> Result>>> { + fn fetch(&mut self, mailbox_hash: MailboxHash) -> Result>>> { let mut w = AsyncBuilder::new(); - let _mailbox_hash = mailbox.hash(); let channel = self.channel.clone(); let handle = { let tx = w.tx();