use crate::collections::hash_map::{ArchivedHashMap, HashMapResolver, Keys};
#[cfg(feature = "alloc")]
use crate::{
ser::{ScratchSpace, Serializer},
Serialize,
};
use core::{borrow::Borrow, fmt, hash::Hash};
#[cfg_attr(feature = "validation", derive(bytecheck::CheckBytes))]
#[repr(transparent)]
pub struct ArchivedHashSet<K>(ArchivedHashMap<K, ()>);
impl<K> ArchivedHashSet<K> {
#[inline]
pub const fn len(&self) -> usize {
self.0.len()
}
#[inline]
pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&K>
where
K: Borrow<Q>,
Q: Hash + Eq,
{
self.0.get_key_value(k).map(|(k, _)| k)
}
#[inline]
pub fn contains<Q: ?Sized>(&self, k: &Q) -> bool
where
K: Borrow<Q>,
Q: Hash + Eq,
{
self.0.contains_key(k)
}
#[cfg(feature = "alloc")]
#[inline]
pub fn hasher(&self) -> seahash::SeaHasher {
self.0.hasher()
}
#[inline]
pub const fn is_empty(&self) -> bool {
self.0.is_empty()
}
#[inline]
pub fn iter(&self) -> Keys<K, ()> {
self.0.keys()
}
#[inline]
pub unsafe fn resolve_from_len(
len: usize,
pos: usize,
resolver: HashSetResolver,
out: *mut Self,
) {
let (fp, fo) = out_field!(out.0);
ArchivedHashMap::resolve_from_len(len, pos + fp, resolver.0, fo);
}
#[cfg(feature = "alloc")]
#[inline]
pub unsafe fn serialize_from_iter<'a, KU, S, I>(
iter: I,
serializer: &mut S,
) -> Result<HashSetResolver, S::Error>
where
KU: 'a + Serialize<S, Archived = K> + Hash + Eq,
S: Serializer + ScratchSpace + ?Sized,
I: ExactSizeIterator<Item = &'a KU>,
{
Ok(HashSetResolver(ArchivedHashMap::serialize_from_iter(
iter.map(|x| (x, &())),
serializer,
)?))
}
}
impl<K: fmt::Debug> fmt::Debug for ArchivedHashSet<K> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_set().entries(self.iter()).finish()
}
}
pub struct HashSetResolver(HashMapResolver);
impl<K: Hash + Eq> PartialEq for ArchivedHashSet<K> {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl<K: Hash + Eq> Eq for ArchivedHashSet<K> {}