[pbs-devel] [PATCH backup 05/13] use or_default instead of or_insert_with(Default::default)

Maximiliano Sandoval m.sandoval at proxmox.com
Mon Feb 12 14:17:30 CET 2024


We need to annotate some cases to allow the compile to infer the types.

Fixes the clippy lint:

```
warning: use of `or_insert_with` to construct default value
   --> src/api2/tape/restore.rs:750:18
    |
750 |                 .or_insert_with(Vec::new);
    |                  ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `or_default()`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unwrap_or_default
    = note: `#[warn(clippy::unwrap_or_default)]` on by default
```

Signed-off-by: Maximiliano Sandoval <m.sandoval at proxmox.com>
---
 pbs-config/src/acl.rs    | 28 ++++++++--------------------
 src/api2/tape/restore.rs | 14 +++++---------
 2 files changed, 13 insertions(+), 29 deletions(-)

diff --git a/pbs-config/src/acl.rs b/pbs-config/src/acl.rs
index cd9987fa..a0354a05 100644
--- a/pbs-config/src/acl.rs
+++ b/pbs-config/src/acl.rs
@@ -447,8 +447,8 @@ impl AclTree {
     }
 
     fn write_node_config(node: &AclTreeNode, path: &str, w: &mut dyn Write) -> Result<(), Error> {
-        let mut role_ug_map0 = HashMap::new();
-        let mut role_ug_map1 = HashMap::new();
+        let mut role_ug_map0: HashMap<_, BTreeSet<_>> = HashMap::new();
+        let mut role_ug_map1: HashMap<_, BTreeSet<_>> = HashMap::new();
 
         for (auth_id, roles) in &node.users {
             // no need to save, because root is always 'Administrator'
@@ -459,15 +459,9 @@ impl AclTree {
                 let role = role.as_str();
                 let auth_id = auth_id.to_string();
                 if *propagate {
-                    role_ug_map1
-                        .entry(role)
-                        .or_insert_with(BTreeSet::new)
-                        .insert(auth_id);
+                    role_ug_map1.entry(role).or_default().insert(auth_id);
                 } else {
-                    role_ug_map0
-                        .entry(role)
-                        .or_insert_with(BTreeSet::new)
-                        .insert(auth_id);
+                    role_ug_map0.entry(role).or_default().insert(auth_id);
                 }
             }
         }
@@ -476,15 +470,9 @@ impl AclTree {
             for (role, propagate) in roles {
                 let group = format!("@{}", group);
                 if *propagate {
-                    role_ug_map1
-                        .entry(role)
-                        .or_insert_with(BTreeSet::new)
-                        .insert(group);
+                    role_ug_map1.entry(role).or_default().insert(group);
                 } else {
-                    role_ug_map0
-                        .entry(role)
-                        .or_insert_with(BTreeSet::new)
-                        .insert(group);
+                    role_ug_map0.entry(role).or_default().insert(group);
                 }
             }
         }
@@ -492,7 +480,7 @@ impl AclTree {
         fn group_by_property_list(
             item_property_map: &HashMap<&str, BTreeSet<String>>,
         ) -> BTreeMap<String, BTreeSet<String>> {
-            let mut result_map = BTreeMap::new();
+            let mut result_map: BTreeMap<_, BTreeSet<_>> = BTreeMap::new();
             for (item, property_map) in item_property_map {
                 let item_list = property_map.iter().fold(String::new(), |mut acc, v| {
                     if !acc.is_empty() {
@@ -503,7 +491,7 @@ impl AclTree {
                 });
                 result_map
                     .entry(item_list)
-                    .or_insert_with(BTreeSet::new)
+                    .or_default()
                     .insert(item.to_string());
             }
             result_map
diff --git a/src/api2/tape/restore.rs b/src/api2/tape/restore.rs
index 47e0586b..8273c867 100644
--- a/src/api2/tape/restore.rs
+++ b/src/api2/tape/restore.rs
@@ -75,7 +75,7 @@ impl TryFrom<Vec<String>> for NamespaceMap {
             let max_depth = mapping.max_depth.unwrap_or(MAX_NAMESPACE_DEPTH);
 
             let ns_map: &mut HashMap<BackupNamespace, (BackupNamespace, usize)> =
-                map.entry(mapping.store).or_insert_with(HashMap::new);
+                map.entry(mapping.store).or_default();
 
             if ns_map.insert(source, (target, max_depth)).is_some() {
                 bail!("duplicate mapping found");
@@ -747,7 +747,7 @@ fn restore_list_worker(
 
             let file_list = snapshot_file_hash
                 .entry(media_id.label.uuid.clone())
-                .or_insert_with(Vec::new);
+                .or_default();
             file_list.push(file_num);
 
             task_log!(
@@ -808,10 +808,8 @@ fn restore_list_worker(
                 // we only want to restore chunks that we do not have yet
                 if !datastore.cond_touch_chunk(&digest, false)? {
                     if let Some((uuid, nr)) = catalog.lookup_chunk(&source_datastore, &digest) {
-                        let file = media_file_chunk_map
-                            .entry(uuid.clone())
-                            .or_insert_with(BTreeMap::new);
-                        let chunks = file.entry(nr).or_insert_with(HashSet::new);
+                        let file = media_file_chunk_map.entry(uuid.clone()).or_default();
+                        let chunks = file.entry(nr).or_default();
                         chunks.insert(digest);
                     }
                 }
@@ -1089,9 +1087,7 @@ fn restore_snapshots_to_tmpdir(
                 );
                 std::fs::create_dir_all(&tmp_path)?;
 
-                let chunks = chunks_list
-                    .entry(source_datastore)
-                    .or_insert_with(HashSet::new);
+                let chunks = chunks_list.entry(source_datastore).or_default();
                 let manifest =
                     try_restore_snapshot_archive(worker.clone(), &mut decoder, &tmp_path)?;
 
-- 
2.39.2





More information about the pbs-devel mailing list