]>
git.proxmox.com Git - rustc.git/blob - 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 ).
6 use litemap
::testing
::check_store_full
;
7 use std
::cmp
::Ordering
;
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
>);
13 type MapF
<K
, V
> = fn(&(K
, V
)) -> (&K
, &V
);
16 fn map_f
<K
, V
>(input
: &(K
, V
)) -> (&K
, &V
) {
20 type MapFMut
<K
, V
> = fn(&mut (K
, V
)) -> (&K
, &mut V
);
23 fn map_f_mut
<K
, V
>(input
: &mut (K
, V
)) -> (&K
, &mut V
) {
24 (&input
.0, &mut input
.1)
27 impl<K
, V
> StoreConstEmpty
<K
, V
> for VecWithDefaults
<(K
, V
)> {
28 const EMPTY
: VecWithDefaults
<(K
, V
)> = VecWithDefaults(Vec
::new());
31 impl<K
, V
> Store
<K
, V
> for VecWithDefaults
<(K
, V
)> {
33 fn lm_len(&self) -> usize {
34 self.0.as_slice().len()
37 // leave lm_is_empty as default
40 fn lm_get(&self, index
: usize) -> Option
<(&K
, &V
)> {
41 self.0.as_slice().get(index
).map(map_f
)
44 // leave lm_last as default
47 fn lm_binary_search_by
<F
>(&self, mut cmp
: F
) -> Result
<usize, usize>
49 F
: FnMut(&K
) -> Ordering
,
51 self.0.as_slice().binary_search_by(|(k
, _
)| cmp(k
))
55 impl<K
, V
> StoreMut
<K
, V
> for VecWithDefaults
<(K
, V
)> {
57 fn lm_with_capacity(capacity
: usize) -> Self {
58 Self(Vec
::with_capacity(capacity
))
62 fn lm_reserve(&mut self, additional
: usize) {
63 self.0.reserve(additional
)
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
)
72 fn lm_push(&mut self, key
: K
, value
: V
) {
73 self.0.push((key
, value
))
77 fn lm_insert(&mut self, index
: usize, key
: K
, value
: V
) {
78 self.0.insert
(index
, (key
, value
))
82 fn lm_remove(&mut self, index
: usize) -> (K
, V
) {
86 fn lm_clear(&mut self) {
90 // leave lm_retain as default
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
>>;
97 fn lm_iter(&'a
self) -> Self::KeyValueIter
{
98 self.0.as_slice().iter().map(map_f
)
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
)>;
107 fn lm_iter_mut(&'a
mut self) -> Self::KeyValueIterMut
{
108 self.0.as_mut_slice().iter_mut().map(map_f_mut
)
112 fn lm_into_iter(self) -> Self::KeyValueIntoIter
{
113 IntoIterator
::into_iter(self.0)
116 // leave lm_extend_end as default
118 // leave lm_extend_start as default
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
))
127 impl<K
, V
> StoreFromIterator
<K
, V
> for VecWithDefaults
<(K
, V
)> {}
130 fn test_default_impl() {
131 check_store_full
::<VecWithDefaults
<(u32, u64)>>();