/src/gitoxide/gix-attributes/src/search/refmap.rs
Line | Count | Source |
1 | | //! A utility to store objects by identity, which deduplicates them while avoiding lifetimes. |
2 | | //! |
3 | | //! We chose to use hashing/identity over pointers as it's possible that different objects end up in the same memory location, |
4 | | //! which would create obscure bugs. The same could happen with hash collisions, but they these are designed to be less likely. |
5 | | use std::{ |
6 | | collections::{btree_map::Entry, hash_map::DefaultHasher, BTreeMap}, |
7 | | hash::{Hash, Hasher}, |
8 | | }; |
9 | | |
10 | | pub(crate) type RefMapKey = u64; |
11 | | #[derive(Clone)] |
12 | | pub(crate) struct RefMap<T>(BTreeMap<RefMapKey, T>); |
13 | | |
14 | | impl<T> Default for RefMap<T> { |
15 | 20.2k | fn default() -> Self { |
16 | 20.2k | RefMap(Default::default()) |
17 | 20.2k | } Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<gix_attributes::Assignment> as core::default::Default>::default Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<gix_glob::Pattern> as core::default::Default>::default Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<std::path::PathBuf> as core::default::Default>::default Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<gix_attributes::Assignment> as core::default::Default>::default Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<gix_glob::Pattern> as core::default::Default>::default Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<std::path::PathBuf> as core::default::Default>::default Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<gix_attributes::Assignment> as core::default::Default>::default Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<gix_glob::Pattern> as core::default::Default>::default Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<std::path::PathBuf> as core::default::Default>::default Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<_> as core::default::Default>::default <gix_attributes::search::refmap::RefMap<gix_attributes::Assignment> as core::default::Default>::default Line | Count | Source | 15 | 6.76k | fn default() -> Self { | 16 | 6.76k | RefMap(Default::default()) | 17 | 6.76k | } |
<gix_attributes::search::refmap::RefMap<gix_glob::Pattern> as core::default::Default>::default Line | Count | Source | 15 | 6.76k | fn default() -> Self { | 16 | 6.76k | RefMap(Default::default()) | 17 | 6.76k | } |
<gix_attributes::search::refmap::RefMap<std::path::PathBuf> as core::default::Default>::default Line | Count | Source | 15 | 6.76k | fn default() -> Self { | 16 | 6.76k | RefMap(Default::default()) | 17 | 6.76k | } |
Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<_> as core::default::Default>::default Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<gix_attributes::Assignment> as core::default::Default>::default Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<gix_glob::Pattern> as core::default::Default>::default Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<std::path::PathBuf> as core::default::Default>::default Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<_> as core::default::Default>::default |
18 | | } |
19 | | |
20 | | impl<T> RefMap<T> |
21 | | where |
22 | | T: Hash + Clone, |
23 | | { |
24 | 0 | pub(crate) fn len(&self) -> usize { |
25 | 0 | self.0.len() |
26 | 0 | } Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<gix_attributes::Assignment>>::len Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<gix_glob::Pattern>>::len Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<std::path::PathBuf>>::len Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<gix_attributes::Assignment>>::len Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<gix_glob::Pattern>>::len Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<std::path::PathBuf>>::len Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<gix_attributes::Assignment>>::len Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<gix_glob::Pattern>>::len Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<std::path::PathBuf>>::len |
27 | 158k | pub(crate) fn insert(&mut self, value: &T) -> RefMapKey { |
28 | 158k | let mut s = DefaultHasher::new(); |
29 | 158k | value.hash(&mut s); |
30 | 158k | let key = s.finish(); |
31 | 158k | match self.0.entry(key) { |
32 | 6.88k | Entry::Vacant(e) => { |
33 | 6.88k | e.insert(value.clone()); |
34 | 6.88k | key |
35 | | } |
36 | 151k | Entry::Occupied(_) => key, |
37 | | } |
38 | 158k | } Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<gix_glob::Pattern>>::insert Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<std::path::PathBuf>>::insert <gix_attributes::search::refmap::RefMap<gix_glob::Pattern>>::insert Line | Count | Source | 27 | 79.0k | pub(crate) fn insert(&mut self, value: &T) -> RefMapKey { | 28 | 79.0k | let mut s = DefaultHasher::new(); | 29 | 79.0k | value.hash(&mut s); | 30 | 79.0k | let key = s.finish(); | 31 | 79.0k | match self.0.entry(key) { | 32 | 5.53k | Entry::Vacant(e) => { | 33 | 5.53k | e.insert(value.clone()); | 34 | 5.53k | key | 35 | | } | 36 | 73.5k | Entry::Occupied(_) => key, | 37 | | } | 38 | 79.0k | } |
<gix_attributes::search::refmap::RefMap<std::path::PathBuf>>::insert Line | Count | Source | 27 | 79.0k | pub(crate) fn insert(&mut self, value: &T) -> RefMapKey { | 28 | 79.0k | let mut s = DefaultHasher::new(); | 29 | 79.0k | value.hash(&mut s); | 30 | 79.0k | let key = s.finish(); | 31 | 79.0k | match self.0.entry(key) { | 32 | 1.35k | Entry::Vacant(e) => { | 33 | 1.35k | e.insert(value.clone()); | 34 | 1.35k | key | 35 | | } | 36 | 77.7k | Entry::Occupied(_) => key, | 37 | | } | 38 | 79.0k | } |
Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<gix_glob::Pattern>>::insert Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<std::path::PathBuf>>::insert |
39 | | |
40 | 79.0k | pub(crate) fn insert_owned(&mut self, value: T) -> RefMapKey { |
41 | 79.0k | let mut s = DefaultHasher::new(); |
42 | 79.0k | value.hash(&mut s); |
43 | 79.0k | let key = s.finish(); |
44 | 79.0k | match self.0.entry(key) { |
45 | 79.0k | Entry::Vacant(e) => { |
46 | 79.0k | e.insert(value); |
47 | 79.0k | key |
48 | | } |
49 | 0 | Entry::Occupied(_) => key, |
50 | | } |
51 | 79.0k | } Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<gix_attributes::Assignment>>::insert_owned <gix_attributes::search::refmap::RefMap<gix_attributes::Assignment>>::insert_owned Line | Count | Source | 40 | 79.0k | pub(crate) fn insert_owned(&mut self, value: T) -> RefMapKey { | 41 | 79.0k | let mut s = DefaultHasher::new(); | 42 | 79.0k | value.hash(&mut s); | 43 | 79.0k | let key = s.finish(); | 44 | 79.0k | match self.0.entry(key) { | 45 | 79.0k | Entry::Vacant(e) => { | 46 | 79.0k | e.insert(value); | 47 | 79.0k | key | 48 | | } | 49 | 0 | Entry::Occupied(_) => key, | 50 | | } | 51 | 79.0k | } |
Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<gix_attributes::Assignment>>::insert_owned |
52 | | |
53 | 0 | pub(crate) fn resolve(&self, key: RefMapKey) -> Option<&T> { |
54 | 0 | self.0.get(&key) |
55 | 0 | } Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<gix_attributes::Assignment>>::resolve Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<gix_glob::Pattern>>::resolve Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<std::path::PathBuf>>::resolve Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<gix_attributes::Assignment>>::resolve Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<gix_glob::Pattern>>::resolve Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<std::path::PathBuf>>::resolve Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<gix_attributes::Assignment>>::resolve Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<gix_glob::Pattern>>::resolve Unexecuted instantiation: <gix_attributes::search::refmap::RefMap<std::path::PathBuf>>::resolve |
56 | | } |