blob: 667c51cb4a3f348ad202c5111e1609e2565ead55 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
// compile-flags: --crate-name mut_key
#![warn(clippy::mutable_key_type)]
use std::cmp::{Eq, PartialEq};
use std::collections::{HashMap, HashSet};
use std::hash::{Hash, Hasher};
use std::ops::Deref;
use std::sync::atomic::{AtomicUsize, Ordering};
struct Counted<T> {
count: AtomicUsize,
val: T,
}
impl<T: Clone> Clone for Counted<T> {
fn clone(&self) -> Self {
Self {
count: AtomicUsize::new(0),
val: self.val.clone(),
}
}
}
impl<T: PartialEq> PartialEq for Counted<T> {
fn eq(&self, other: &Self) -> bool {
self.val == other.val
}
}
impl<T: PartialEq + Eq> Eq for Counted<T> {}
impl<T: Hash> Hash for Counted<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.val.hash(state);
}
}
impl<T> Deref for Counted<T> {
type Target = T;
fn deref(&self) -> &T {
self.count.fetch_add(1, Ordering::AcqRel);
&self.val
}
}
// This is not linted because `"mut_key::Counted"` is in
// `arc_like_types` in `clippy.toml`
fn should_not_take_this_arg(_v: HashSet<Counted<String>>) {}
fn main() {
should_not_take_this_arg(HashSet::new());
}
|