From: Maximiliano Sandoval <m.sandoval@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH backup 05/13] use or_default instead of or_insert_with(Default::default)
Date: Mon, 12 Feb 2024 14:17:30 +0100 [thread overview]
Message-ID: <20240212131734.454966-5-m.sandoval@proxmox.com> (raw)
In-Reply-To: <20240212131734.454966-1-m.sandoval@proxmox.com>
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@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
next prev parent reply other threads:[~2024-02-12 13:18 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-12 13:17 [pbs-devel] [PATCH backup 01/13] docs: remove redundant explicit link target Maximiliano Sandoval
2024-02-12 13:17 ` [pbs-devel] [PATCH backup 02/13] remove redundant guards Maximiliano Sandoval
2024-02-12 13:17 ` [pbs-devel] [PATCH backup 03/13] remove needless borrows Maximiliano Sandoval
2024-02-12 13:17 ` [pbs-devel] [PATCH backup 04/13] media_catalog: use stream_position Maximiliano Sandoval
2024-02-12 13:17 ` Maximiliano Sandoval [this message]
2024-02-12 13:17 ` [pbs-devel] [PATCH backup 06/13] access first element with first() rather than get(0) Maximiliano Sandoval
2024-02-12 13:17 ` [pbs-devel] [PATCH backup 07/13] datastore: use is_{err, some} rather than match {Ok, Some}(_) Maximiliano Sandoval
2024-02-12 13:17 ` [pbs-devel] [PATCH backup 08/13] snapshot_reader: use Rc for structs that are not Send+Sync Maximiliano Sandoval
2024-02-13 9:20 ` Fabian Grünbichler
2024-02-12 13:17 ` [pbs-devel] [PATCH backup 09/13] pxar: elide explicit lifetime Maximiliano Sandoval
2024-02-13 9:04 ` [pbs-devel] [PATCH backup 01/13] docs: remove redundant explicit link target Maximiliano Sandoval
2024-02-13 9:28 ` [pbs-devel] partially-applied: " Fabian Grünbichler
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240212131734.454966-5-m.sandoval@proxmox.com \
--to=m.sandoval@proxmox.com \
--cc=pbs-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox