]> git.proxmox.com Git - rustc.git/blob - vendor/litemap/tests/store.rs
New upstream version 1.67.1+dfsg1
[rustc.git] / vendor / litemap / tests / store.rs
1 // This file is part of ICU4X. For terms of use, please see the file
2 // called LICENSE at the top level of the ICU4X source tree
3 // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5 use litemap::store::*;
6 use litemap::testing::check_store_full;
7 use std::cmp::Ordering;
8
9 /// A Vec wrapper that leverages the default function impls from `Store`
10 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
11 struct VecWithDefaults<T>(Vec<T>);
12
13 type MapF<K, V> = fn(&(K, V)) -> (&K, &V);
14
15 #[inline]
16 fn map_f<K, V>(input: &(K, V)) -> (&K, &V) {
17 (&input.0, &input.1)
18 }
19
20 type MapFMut<K, V> = fn(&mut (K, V)) -> (&K, &mut V);
21
22 #[inline]
23 fn map_f_mut<K, V>(input: &mut (K, V)) -> (&K, &mut V) {
24 (&input.0, &mut input.1)
25 }
26
27 impl<K, V> StoreConstEmpty<K, V> for VecWithDefaults<(K, V)> {
28 const EMPTY: VecWithDefaults<(K, V)> = VecWithDefaults(Vec::new());
29 }
30
31 impl<K, V> Store<K, V> for VecWithDefaults<(K, V)> {
32 #[inline]
33 fn lm_len(&self) -> usize {
34 self.0.as_slice().len()
35 }
36
37 // leave lm_is_empty as default
38
39 #[inline]
40 fn lm_get(&self, index: usize) -> Option<(&K, &V)> {
41 self.0.as_slice().get(index).map(map_f)
42 }
43
44 // leave lm_last as default
45
46 #[inline]
47 fn lm_binary_search_by<F>(&self, mut cmp: F) -> Result<usize, usize>
48 where
49 F: FnMut(&K) -> Ordering,
50 {
51 self.0.as_slice().binary_search_by(|(k, _)| cmp(k))
52 }
53 }
54
55 impl<K, V> StoreMut<K, V> for VecWithDefaults<(K, V)> {
56 #[inline]
57 fn lm_with_capacity(capacity: usize) -> Self {
58 Self(Vec::with_capacity(capacity))
59 }
60
61 #[inline]
62 fn lm_reserve(&mut self, additional: usize) {
63 self.0.reserve(additional)
64 }
65
66 #[inline]
67 fn lm_get_mut(&mut self, index: usize) -> Option<(&K, &mut V)> {
68 self.0.as_mut_slice().get_mut(index).map(map_f_mut)
69 }
70
71 #[inline]
72 fn lm_push(&mut self, key: K, value: V) {
73 self.0.push((key, value))
74 }
75
76 #[inline]
77 fn lm_insert(&mut self, index: usize, key: K, value: V) {
78 self.0.insert(index, (key, value))
79 }
80
81 #[inline]
82 fn lm_remove(&mut self, index: usize) -> (K, V) {
83 self.0.remove(index)
84 }
85 #[inline]
86 fn lm_clear(&mut self) {
87 self.0.clear()
88 }
89
90 // leave lm_retain as default
91 }
92
93 impl<'a, K: 'a, V: 'a> StoreIterable<'a, K, V> for VecWithDefaults<(K, V)> {
94 type KeyValueIter = core::iter::Map<core::slice::Iter<'a, (K, V)>, MapF<K, V>>;
95
96 #[inline]
97 fn lm_iter(&'a self) -> Self::KeyValueIter {
98 self.0.as_slice().iter().map(map_f)
99 }
100 }
101
102 impl<'a, K: 'a, V: 'a> StoreIterableMut<'a, K, V> for VecWithDefaults<(K, V)> {
103 type KeyValueIterMut = core::iter::Map<core::slice::IterMut<'a, (K, V)>, MapFMut<K, V>>;
104 type KeyValueIntoIter = std::vec::IntoIter<(K, V)>;
105
106 #[inline]
107 fn lm_iter_mut(&'a mut self) -> Self::KeyValueIterMut {
108 self.0.as_mut_slice().iter_mut().map(map_f_mut)
109 }
110
111 #[inline]
112 fn lm_into_iter(self) -> Self::KeyValueIntoIter {
113 IntoIterator::into_iter(self.0)
114 }
115
116 // leave lm_extend_end as default
117
118 // leave lm_extend_start as default
119 }
120
121 impl<A> std::iter::FromIterator<A> for VecWithDefaults<A> {
122 fn from_iter<I: IntoIterator<Item = A>>(iter: I) -> Self {
123 Self(Vec::from_iter(iter))
124 }
125 }
126
127 impl<K, V> StoreFromIterator<K, V> for VecWithDefaults<(K, V)> {}
128
129 #[test]
130 fn test_default_impl() {
131 check_store_full::<VecWithDefaults<(u32, u64)>>();
132 }