2 use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
3 use std::hash::{Hash, Hasher};
5 use std::sync::atomic::{AtomicUsize, Ordering::Relaxed};
8 struct Key(AtomicUsize);
11 fn clone(&self) -> Self {
12 Key(AtomicUsize::new(self.0.load(Relaxed)))
16 impl PartialEq for Key {
17 fn eq(&self, other: &Self) -> bool {
18 self.0.load(Relaxed) == other.0.load(Relaxed)
25 fn hash<H: Hasher>(&self, h: &mut H) {
26 self.0.load(Relaxed).hash(h);
30 fn should_not_take_this_arg(m: &mut HashMap<Key, usize>, _n: usize) -> HashSet<Key> {
31 let _other: HashMap<Key, bool> = HashMap::new();
32 m.keys().cloned().collect()
35 fn this_is_ok(_m: &mut HashMap<usize, Key>) {}
37 // Raw pointers are hashed by the address they point to, so it doesn't matter if they point to a
38 // type with interior mutability. See:
39 // - clippy issue: https://github.com/rust-lang/rust-clippy/issues/6745
40 // - std lib: https://github.com/rust-lang/rust/blob/1.54.0/library/core/src/hash/mod.rs#L717-L736
42 fn raw_ptr_is_ok(_m: &mut HashMap<*const Key, ()>) {}
43 fn raw_mut_ptr_is_ok(_m: &mut HashMap<*mut Key, ()>) {}
49 fn trait_fn(&self, set: HashSet<Self::AssociatedType>);
52 fn generics_are_ok_too<K>(_m: &mut HashSet<K>) {
53 // nothing to see here, move along
56 fn tuples<U>(_m: &mut HashMap<((), U), ()>) {}
58 fn tuples_bad<U>(_m: &mut HashMap<(Key, U), bool>) {}
61 let _ = should_not_take_this_arg(&mut HashMap::new(), 1);
62 this_is_ok(&mut HashMap::new());
63 tuples::<Key>(&mut HashMap::new());
64 tuples::<()>(&mut HashMap::new());
65 tuples_bad::<()>(&mut HashMap::new());
67 raw_ptr_is_ok(&mut HashMap::new());
68 raw_mut_ptr_is_ok(&mut HashMap::new());
70 let _map = HashMap::<Cell<usize>, usize>::new();
71 let _map = HashMap::<&mut Cell<usize>, usize>::new();
72 let _map = HashMap::<&mut usize, usize>::new();
73 // Collection types from `std` who's impl of `Hash` or `Ord` delegate their type parameters
74 let _map = HashMap::<Vec<Cell<usize>>, usize>::new();
75 let _map = HashMap::<BTreeMap<Cell<usize>, ()>, usize>::new();
76 let _map = HashMap::<BTreeMap<(), Cell<usize>>, usize>::new();
77 let _map = HashMap::<BTreeSet<Cell<usize>>, usize>::new();
78 let _map = HashMap::<Option<Cell<usize>>, usize>::new();
79 let _map = HashMap::<Option<Vec<Cell<usize>>>, usize>::new();
80 let _map = HashMap::<Result<&mut usize, ()>, usize>::new();
81 // Smart pointers from `std` who's impl of `Hash` or `Ord` delegate their type parameters
82 let _map = HashMap::<Box<Cell<usize>>, usize>::new();
83 let _map = HashMap::<Rc<Cell<usize>>, usize>::new();
84 let _map = HashMap::<Arc<Cell<usize>>, usize>::new();