Browse Source

meli/accounts: use Arc<str> for account name

Since it gets cloned around a lot.

Signed-off-by: Manos Pitsidianakis <manos@pitsidianak.is>
Manos Pitsidianakis 7 months ago
parent
commit
5af6e059
5 changed files with 27 additions and 29 deletions
  1. 7 9
      meli/src/accounts.rs
  2. 1 1
      meli/src/accounts/mailbox_ops.rs
  3. 7 5
      meli/src/command/actions.rs
  4. 10 10
      meli/src/sqlite3.rs
  5. 2 4
      meli/src/state.rs

+ 7 - 9
meli/src/accounts.rs

@@ -123,7 +123,7 @@ impl IsOnline {
 
 #[derive(Debug)]
 pub struct Account {
-    pub name: String,
+    pub name: Arc<str>,
     pub hash: AccountHash,
     pub is_online: IsOnline,
     pub mailbox_entries: IndexMap<MailboxHash, MailboxEntry>,
@@ -143,7 +143,7 @@ pub struct Account {
 
 impl Drop for Account {
     fn drop(&mut self) {
-        if let Ok(data_dir) = xdg::BaseDirectories::with_profile("meli", &self.name) {
+        if let Ok(data_dir) = xdg::BaseDirectories::with_profile("meli", self.name.as_ref()) {
             if let Ok(data) = data_dir.place_data_file("contacts") {
                 /* place result in cache directory */
                 let f = match fs::File::create(data) {
@@ -201,6 +201,7 @@ impl Account {
         main_loop_handler: MainLoopHandler,
         event_consumer: BackendEventConsumer,
     ) -> Result<Self> {
+        let name: Arc<str> = name.into();
         let s = settings.clone();
         let backend = map.get(&settings.account().format)(
             settings.account(),
@@ -219,7 +220,7 @@ impl Account {
             event_consumer,
         )?;
 
-        let data_dir = xdg::BaseDirectories::with_profile("meli", &name)?;
+        let data_dir = xdg::BaseDirectories::with_profile("meli", name.as_ref())?;
         let mut contacts = Contacts::with_account(settings.account());
 
         if let Ok(data) = data_dir.place_data_file("contacts") {
@@ -700,7 +701,7 @@ impl Account {
                             body: format!(
                                 "{}\n{} | {}",
                                 from,
-                                self.name,
+                                &self.name,
                                 self.mailbox_entries[&mailbox_hash].name()
                             )
                             .into(),
@@ -1009,10 +1010,7 @@ impl Account {
         flags: Option<Flag>,
     ) -> Result<()> {
         if self.settings.account.read_only {
-            return Err(Error::new(format!(
-                "Account {} is read-only.",
-                self.name.as_str()
-            )));
+            return Err(Error::new(format!("Account {} is read-only.", &self.name)));
         }
         let job = self
             .backend
@@ -1803,7 +1801,7 @@ impl Account {
     }
 
     pub fn signature_file(&self) -> Option<PathBuf> {
-        xdg::BaseDirectories::with_profile("meli", &self.name)
+        xdg::BaseDirectories::with_profile("meli", self.name.as_ref())
             .ok()
             .and_then(|d| {
                 d.place_config_file("signature")

+ 1 - 1
meli/src/accounts/mailbox_ops.rs

@@ -185,7 +185,7 @@ impl Account {
                         if !err.is_recoverable() {
                             self.main_loop_handler.send(ThreadEvent::UIEvent(
                                 UIEvent::Notification {
-                                    title: Some(self.name.clone().into()),
+                                    title: Some(self.name.to_string().into()),
                                     source: Some(err.clone()),
                                     body: err.to_string().into(),
                                     kind: Some(NotificationType::Error(err.kind)),

+ 7 - 5
meli/src/command/actions.rs

@@ -21,7 +21,7 @@
 
 //! User actions that need to be handled by the UI
 
-use std::path::PathBuf;
+use std::{path::PathBuf, sync::Arc};
 
 use melib::{email::mailto::Mailto, Flag, SortField, SortOrder};
 
@@ -168,7 +168,7 @@ type MailboxPath = String;
 type NewMailboxPath = String;
 
 macro_rules! impl_into_action {
-    ($({$t:ty => $var:tt}),*) => {
+    ($({$t:ty => $var:tt}),*$(,)?) => {
         $(
             impl From<$t> for Action {
                 fn from(v: $t) -> Self {
@@ -179,11 +179,11 @@ macro_rules! impl_into_action {
     };
 }
 macro_rules! impl_tuple_into_action {
-    ($({$a:ty,$b:ty => $var:tt}),*) => {
+    ($({$a:ty,$b:ty => $var:tt}),*$(,)?) => {
         $(
             impl From<($a,$b)> for Action {
                 fn from((a, b): ($a,$b)) -> Self {
-                    Self::$var(a, b)
+                    Self::$var(a.to_string(), b)
                 }
             }
         )*
@@ -199,5 +199,7 @@ impl_into_action!(
 );
 impl_tuple_into_action!(
     { AccountName, MailboxOperation => Mailbox },
-    { AccountName, AccountAction => AccountAction }
+    { AccountName, AccountAction => AccountAction },
+    { Arc<str>, MailboxOperation => Mailbox },
+    { Arc<str>, AccountAction => AccountAction },
 );

+ 10 - 10
meli/src/sqlite3.rs

@@ -150,10 +150,10 @@ impl AccountCache {
     pub async fn insert(
         envelope: Envelope,
         backend: Arc<RwLock<Box<dyn MailBackend>>>,
-        acc_name: String,
+        acc_name: Arc<str>,
     ) -> Result<()> {
         let db_desc = DatabaseDescription {
-            identifier: Some(acc_name.clone().into()),
+            identifier: Some(acc_name.to_string().into()),
             ..DB.clone()
         };
 
@@ -243,9 +243,9 @@ impl AccountCache {
         Ok(())
     }
 
-    pub async fn remove(acc_name: String, env_hash: EnvelopeHash) -> Result<()> {
+    pub async fn remove(acc_name: Arc<str>, env_hash: EnvelopeHash) -> Result<()> {
         let db_desc = DatabaseDescription {
-            identifier: Some(acc_name.clone().into()),
+            identifier: Some(acc_name.to_string().into()),
             ..DB.clone()
         };
         let db_path = db_desc.db_path()?;
@@ -277,7 +277,7 @@ impl AccountCache {
     }
 
     pub async fn index(
-        acc_name: Arc<String>,
+        acc_name: Arc<str>,
         collection: melib::Collection,
         backend_mutex: Arc<RwLock<Box<dyn MailBackend>>>,
     ) -> Result<()> {
@@ -303,7 +303,7 @@ impl AccountCache {
                     .transaction_with_behavior(melib::rusqlite::TransactionBehavior::Immediate)?;
                 tx.execute(
                     "INSERT OR REPLACE INTO accounts (name) VALUES (?1)",
-                    params![acc_name.as_str(),],
+                    params![acc_name.as_ref()],
                 )
                 .chain_err_summary(|| "Failed to update index:")?;
                 let account_id = {
@@ -311,7 +311,7 @@ impl AccountCache {
                         .prepare("SELECT id FROM accounts WHERE name = ?")
                         .unwrap();
                     let x = stmt
-                        .query_map(params![acc_name.as_str()], |row| row.get(0))
+                        .query_map(params![acc_name.as_ref()], |row| row.get(0))
                         .unwrap()
                         .next()
                         .unwrap()
@@ -398,12 +398,12 @@ impl AccountCache {
     }
 
     pub async fn search(
-        acc_name: String,
+        acc_name: Arc<str>,
         query: Query,
         (sort_field, sort_order): (SortField, SortOrder),
     ) -> Result<SmallVec<[EnvelopeHash; 512]>> {
         let db_desc = DatabaseDescription {
-            identifier: Some(acc_name.clone().into()),
+            identifier: Some(acc_name.to_string().into()),
             ..DB.clone()
         };
 
@@ -449,7 +449,7 @@ impl AccountCache {
         .await
     }
 
-    pub fn db_path(acc_name: &str) -> Result<Option<PathBuf>> {
+    pub fn db_path(acc_name: &Arc<str>) -> Result<Option<PathBuf>> {
         let db_desc = DatabaseDescription {
             identifier: Some(acc_name.to_string().into()),
             ..DB.clone()

+ 2 - 4
meli/src/state.rs

@@ -811,10 +811,8 @@ impl State {
                     return;
                 }
                 let account = &self.context.accounts[account_index];
-                let (acc_name, backend_mutex): (Arc<String>, Arc<_>) = (
-                    Arc::new(account.name().to_string()),
-                    account.backend.clone(),
-                );
+                let (acc_name, backend_mutex): (Arc<str>, Arc<_>) =
+                    (Arc::clone(&account.name), account.backend.clone());
                 let job = crate::sqlite3::AccountCache::index(
                     acc_name,
                     account.collection.clone(),