rkyv/collections/
hash_set.rs1use crate::collections::hash_map::{ArchivedHashMap, HashMapResolver, Keys};
7#[cfg(feature = "alloc")]
8use crate::{
9 ser::{ScratchSpace, Serializer},
10 Serialize,
11};
12use core::{borrow::Borrow, fmt, hash::Hash};
13
14#[cfg_attr(feature = "validation", derive(bytecheck::CheckBytes))]
17#[repr(transparent)]
18pub struct ArchivedHashSet<K>(ArchivedHashMap<K, ()>);
19
20impl<K> ArchivedHashSet<K> {
21 #[inline]
23 pub const fn len(&self) -> usize {
24 self.0.len()
25 }
26
27 #[inline]
29 pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&K>
30 where
31 K: Borrow<Q>,
32 Q: Hash + Eq,
33 {
34 self.0.get_key_value(k).map(|(k, _)| k)
35 }
36
37 #[inline]
39 pub fn contains<Q: ?Sized>(&self, k: &Q) -> bool
40 where
41 K: Borrow<Q>,
42 Q: Hash + Eq,
43 {
44 self.0.contains_key(k)
45 }
46
47 #[cfg(feature = "alloc")]
49 #[inline]
50 pub fn hasher(&self) -> seahash::SeaHasher {
51 self.0.hasher()
52 }
53
54 #[inline]
56 pub const fn is_empty(&self) -> bool {
57 self.0.is_empty()
58 }
59
60 #[inline]
62 pub fn iter(&self) -> Keys<K, ()> {
63 self.0.keys()
64 }
65
66 #[inline]
74 pub unsafe fn resolve_from_len(
75 len: usize,
76 pos: usize,
77 resolver: HashSetResolver,
78 out: *mut Self,
79 ) {
80 let (fp, fo) = out_field!(out.0);
81 ArchivedHashMap::resolve_from_len(len, pos + fp, resolver.0, fo);
82 }
83
84 #[cfg(feature = "alloc")]
90 #[inline]
91 pub unsafe fn serialize_from_iter<'a, KU, S, I>(
92 iter: I,
93 serializer: &mut S,
94 ) -> Result<HashSetResolver, S::Error>
95 where
96 KU: 'a + Serialize<S, Archived = K> + Hash + Eq,
97 S: Serializer + ScratchSpace + ?Sized,
98 I: ExactSizeIterator<Item = &'a KU>,
99 {
100 Ok(HashSetResolver(ArchivedHashMap::serialize_from_iter(
101 iter.map(|x| (x, &())),
102 serializer,
103 )?))
104 }
105}
106
107impl<K: fmt::Debug> fmt::Debug for ArchivedHashSet<K> {
108 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
109 f.debug_set().entries(self.iter()).finish()
110 }
111}
112
113pub struct HashSetResolver(HashMapResolver);
115
116impl<K: Hash + Eq> PartialEq for ArchivedHashSet<K> {
117 #[inline]
118 fn eq(&self, other: &Self) -> bool {
119 self.0 == other.0
120 }
121}
122
123impl<K: Hash + Eq> Eq for ArchivedHashSet<K> {}