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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use crate::{
collections::{
hash_index::HashBuilder,
index_map::{ArchivedIndexMap, IndexMapResolver, Keys},
},
out_field,
};
use core::{borrow::Borrow, fmt, hash::Hash};
#[cfg_attr(feature = "validation", derive(bytecheck::CheckBytes))]
#[repr(transparent)]
pub struct ArchivedIndexSet<K> {
inner: ArchivedIndexMap<K, ()>,
}
impl<K> ArchivedIndexSet<K> {
#[inline]
pub fn contains<Q: ?Sized>(&self, k: &Q) -> bool
where
K: Borrow<Q>,
Q: Hash + Eq,
{
self.inner.contains_key(k)
}
#[inline]
pub fn first(&self) -> Option<&K> {
self.inner.first().map(|(k, _)| k)
}
#[inline]
pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&K>
where
K: Borrow<Q>,
Q: Hash + Eq,
{
self.inner.get_full(k).map(|(_, k, _)| k)
}
#[inline]
pub fn get_full<Q: ?Sized>(&self, k: &Q) -> Option<(usize, &K)>
where
K: Borrow<Q>,
Q: Hash + Eq,
{
self.inner.get_full(k).map(|(i, k, _)| (i, k))
}
#[inline]
pub fn get_index(&self, index: usize) -> Option<&K> {
self.inner.get_index(index).map(|(k, _)| k)
}
#[inline]
pub fn get_index_of<Q: ?Sized>(&self, key: &Q) -> Option<usize>
where
K: Borrow<Q>,
Q: Hash + Eq,
{
self.inner.get_index_of(key)
}
#[inline]
pub fn hasher(&self) -> HashBuilder {
self.inner.hasher()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
#[inline]
pub fn iter(&self) -> Keys<K, ()> {
self.inner.keys()
}
#[inline]
pub fn last(&self) -> Option<&K> {
self.inner.last().map(|(k, _)| k)
}
#[inline]
pub fn len(&self) -> usize {
self.inner.len()
}
#[inline]
pub unsafe fn resolve_from_len(
len: usize,
pos: usize,
resolver: IndexSetResolver,
out: *mut Self,
) {
let (fp, fo) = out_field!(out.inner);
ArchivedIndexMap::resolve_from_len(len, pos + fp, resolver.0, fo);
}
}
#[cfg(feature = "alloc")]
const _: () = {
use crate::{
ser::{ScratchSpace, Serializer},
Serialize,
};
impl<K> ArchivedIndexSet<K> {
#[inline]
pub unsafe fn serialize_from_iter_index<'a, UK, I, F, S>(
iter: I,
index: F,
serializer: &mut S,
) -> Result<IndexSetResolver, S::Error>
where
UK: 'a + Hash + Eq + Serialize<S, Archived = K>,
I: Clone + ExactSizeIterator<Item = &'a UK>,
F: Fn(&UK) -> usize,
S: ScratchSpace + Serializer + ?Sized,
{
Ok(IndexSetResolver(
ArchivedIndexMap::serialize_from_iter_index(
iter.map(|k| (k, &())),
index,
serializer,
)?,
))
}
}
};
impl<K: fmt::Debug> fmt::Debug for ArchivedIndexSet<K> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_set().entries(self.iter()).finish()
}
}
impl<K: PartialEq> PartialEq for ArchivedIndexSet<K> {
fn eq(&self, other: &Self) -> bool {
self.iter().eq(other.iter())
}
}
pub struct IndexSetResolver(IndexMapResolver);