2021-03-25 13:25:04 +00:00
|
|
|
// run-rustfix
|
|
|
|
|
|
|
|
#![allow(unused, clippy::needless_pass_by_value, clippy::collapsible_if)]
|
|
|
|
#![warn(clippy::map_entry)]
|
2021-03-30 00:17:03 +00:00
|
|
|
#![feature(asm)]
|
2021-03-25 13:25:04 +00:00
|
|
|
|
|
|
|
use std::collections::{BTreeMap, HashMap};
|
|
|
|
use std::hash::Hash;
|
|
|
|
|
|
|
|
macro_rules! m {
|
|
|
|
($e:expr) => {{ $e }};
|
|
|
|
}
|
|
|
|
|
2021-03-30 00:17:03 +00:00
|
|
|
macro_rules! insert {
|
|
|
|
($map:expr, $key:expr, $val:expr) => {
|
|
|
|
$map.insert($key, $val)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:25:04 +00:00
|
|
|
fn foo() {}
|
|
|
|
|
2021-03-30 00:17:03 +00:00
|
|
|
fn hash_map<K: Eq + Hash + Copy, V: Copy>(m: &mut HashMap<K, V>, m2: &mut HashMap<K, V>, k: K, k2: K, v: V, v2: V) {
|
|
|
|
// or_insert(v)
|
2021-03-25 13:25:04 +00:00
|
|
|
m.entry(k).or_insert(v);
|
|
|
|
|
2021-03-30 00:17:03 +00:00
|
|
|
// semicolon on insert, use or_insert_with(..)
|
2021-03-25 13:47:56 +00:00
|
|
|
m.entry(k).or_insert_with(|| {
|
2021-03-25 13:25:04 +00:00
|
|
|
if true {
|
2021-03-25 13:47:56 +00:00
|
|
|
v
|
2021-03-25 13:25:04 +00:00
|
|
|
} else {
|
2021-03-25 13:47:56 +00:00
|
|
|
v2
|
2021-03-25 13:25:04 +00:00
|
|
|
}
|
2021-03-25 13:47:56 +00:00
|
|
|
});
|
|
|
|
|
2021-03-30 00:17:03 +00:00
|
|
|
// semicolon on if, use or_insert_with(..)
|
2021-03-25 13:47:56 +00:00
|
|
|
m.entry(k).or_insert_with(|| {
|
|
|
|
if true {
|
|
|
|
v
|
|
|
|
} else {
|
|
|
|
v2
|
|
|
|
}
|
|
|
|
});
|
2021-03-25 13:25:04 +00:00
|
|
|
|
2021-03-30 00:17:03 +00:00
|
|
|
// early return, use if let
|
2021-03-25 13:25:04 +00:00
|
|
|
if let std::collections::hash_map::Entry::Vacant(e) = m.entry(k) {
|
|
|
|
if true {
|
|
|
|
e.insert(v);
|
|
|
|
} else {
|
|
|
|
e.insert(v2);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 00:17:03 +00:00
|
|
|
// use or_insert_with(..)
|
2021-03-25 13:47:56 +00:00
|
|
|
m.entry(k).or_insert_with(|| {
|
2021-03-25 13:25:04 +00:00
|
|
|
foo();
|
2021-03-25 13:47:56 +00:00
|
|
|
v
|
|
|
|
});
|
2021-03-25 13:25:04 +00:00
|
|
|
|
2021-03-30 00:17:03 +00:00
|
|
|
// semicolon on insert and match, use or_insert_with(..)
|
2021-03-25 13:47:56 +00:00
|
|
|
m.entry(k).or_insert_with(|| {
|
2021-03-25 13:25:04 +00:00
|
|
|
match 0 {
|
|
|
|
1 if true => {
|
2021-03-25 13:47:56 +00:00
|
|
|
v
|
2021-03-25 13:25:04 +00:00
|
|
|
},
|
|
|
|
_ => {
|
2021-03-25 13:47:56 +00:00
|
|
|
v2
|
2021-03-25 13:25:04 +00:00
|
|
|
},
|
2021-03-25 13:47:56 +00:00
|
|
|
}
|
|
|
|
});
|
2021-03-25 13:25:04 +00:00
|
|
|
|
2021-03-30 00:17:03 +00:00
|
|
|
// one branch doesn't insert, use if let
|
2021-03-25 13:25:04 +00:00
|
|
|
if let std::collections::hash_map::Entry::Vacant(e) = m.entry(k) {
|
|
|
|
match 0 {
|
2021-03-30 00:17:03 +00:00
|
|
|
0 => foo(),
|
2021-03-25 13:25:04 +00:00
|
|
|
_ => {
|
|
|
|
e.insert(v2);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-03-30 00:17:03 +00:00
|
|
|
// use or_insert_with
|
2021-03-25 13:47:56 +00:00
|
|
|
m.entry(k).or_insert_with(|| {
|
2021-03-25 13:25:04 +00:00
|
|
|
foo();
|
|
|
|
match 0 {
|
|
|
|
0 if false => {
|
2021-03-25 13:47:56 +00:00
|
|
|
v
|
2021-03-25 13:25:04 +00:00
|
|
|
},
|
|
|
|
1 => {
|
|
|
|
foo();
|
2021-03-25 13:47:56 +00:00
|
|
|
v
|
2021-03-25 13:25:04 +00:00
|
|
|
},
|
|
|
|
2 | 3 => {
|
|
|
|
for _ in 0..2 {
|
|
|
|
foo();
|
|
|
|
}
|
|
|
|
if true {
|
2021-03-25 13:47:56 +00:00
|
|
|
v
|
2021-03-25 13:25:04 +00:00
|
|
|
} else {
|
2021-03-25 13:47:56 +00:00
|
|
|
v2
|
|
|
|
}
|
2021-03-25 13:25:04 +00:00
|
|
|
},
|
|
|
|
_ => {
|
2021-03-25 13:47:56 +00:00
|
|
|
v2
|
2021-03-25 13:25:04 +00:00
|
|
|
},
|
|
|
|
}
|
2021-03-25 13:47:56 +00:00
|
|
|
});
|
2021-03-25 13:25:04 +00:00
|
|
|
|
2021-03-30 00:17:03 +00:00
|
|
|
// ok, insert in loop
|
|
|
|
if !m.contains_key(&k) {
|
|
|
|
for _ in 0..2 {
|
|
|
|
m.insert(k, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// macro_expansion test, use or_insert(..)
|
2021-03-25 13:47:56 +00:00
|
|
|
m.entry(m!(k)).or_insert_with(|| m!(v));
|
2021-03-30 00:17:03 +00:00
|
|
|
|
|
|
|
// ok, map used before insertion
|
|
|
|
if !m.contains_key(&k) {
|
|
|
|
let _ = m.len();
|
|
|
|
m.insert(k, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ok, inline asm
|
|
|
|
if !m.contains_key(&k) {
|
|
|
|
unsafe { asm!("nop") }
|
|
|
|
m.insert(k, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ok, different keys.
|
|
|
|
if !m.contains_key(&k) {
|
|
|
|
m.insert(k2, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ok, different maps
|
|
|
|
if !m.contains_key(&k) {
|
|
|
|
m2.insert(k, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ok, insert in macro
|
|
|
|
if !m.contains_key(&k) {
|
|
|
|
insert!(m, k, v);
|
|
|
|
}
|
2021-03-25 13:25:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn btree_map<K: Eq + Ord + Copy, V: Copy>(m: &mut BTreeMap<K, V>, k: K, v: V, v2: V) {
|
2021-03-30 00:17:03 +00:00
|
|
|
// insert then do something, use if let
|
2021-03-25 13:25:04 +00:00
|
|
|
if let std::collections::btree_map::Entry::Vacant(e) = m.entry(k) {
|
|
|
|
e.insert(v);
|
|
|
|
foo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {}
|