use crate::compat::*;
use super::traits::*;
use super::weak_key_hash_map as base;
pub use super::WeakHashSet;
impl<T: WeakKey> WeakHashSet<T, RandomState> {
pub fn new() -> Self {
WeakHashSet(base::WeakKeyHashMap::new())
}
pub fn with_capacity(capacity: usize) -> Self {
WeakHashSet(base::WeakKeyHashMap::with_capacity(capacity))
}
}
impl<T: WeakKey, S: BuildHasher> WeakHashSet<T, S> {
pub fn with_hasher(hash_builder: S) -> Self {
WeakHashSet(base::WeakKeyHashMap::with_hasher(hash_builder))
}
pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self {
WeakHashSet(base::WeakKeyHashMap::with_capacity_and_hasher(
capacity,
hash_builder,
))
}
pub fn hasher(&self) -> &S {
self.0.hasher()
}
pub fn capacity(&self) -> usize {
self.0.capacity()
}
pub fn remove_expired(&mut self) {
self.0.remove_expired();
}
pub fn reserve(&mut self, additional_capacity: usize) {
self.0.reserve(additional_capacity);
}
pub fn shrink_to_fit(&mut self) {
self.0.shrink_to_fit();
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn load_factor(&self) -> f32 {
self.0.load_factor()
}
pub fn clear(&mut self) {
self.0.clear();
}
pub fn contains<Q>(&self, key: &Q) -> bool
where
Q: ?Sized + Eq + Hash,
T::Key: Borrow<Q>,
{
self.0.contains_key(key)
}
pub fn get<Q>(&self, key: &Q) -> Option<T::Strong>
where
Q: ?Sized + Eq + Hash,
T::Key: Borrow<Q>,
{
self.0.get_key(key)
}
pub fn insert(&mut self, key: T::Strong) -> bool {
self.0.insert(key, ()).is_some()
}
pub fn remove<Q>(&mut self, key: &Q) -> bool
where
Q: ?Sized + Eq + Hash,
T::Key: Borrow<Q>,
{
self.0.remove(key).is_some()
}
pub fn retain<F>(&mut self, mut f: F)
where
F: FnMut(T::Strong) -> bool,
{
self.0.retain(|k, _| f(k));
}
pub fn is_subset<S1>(&self, other: &WeakHashSet<T, S1>) -> bool
where
S1: BuildHasher,
{
self.0.domain_is_subset(&other.0)
}
}
pub struct Iter<'a, T: 'a>(base::Keys<'a, T, ()>);
impl<'a, T: WeakElement> Iterator for Iter<'a, T> {
type Item = T::Strong;
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
}
pub struct IntoIter<T>(base::IntoIter<T, ()>);
impl<T: WeakElement> Iterator for IntoIter<T> {
type Item = T::Strong;
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|pair| pair.0)
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
}
pub struct Drain<'a, T: 'a>(base::Drain<'a, T, ()>);
impl<'a, T: WeakElement> Iterator for Drain<'a, T> {
type Item = T::Strong;
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|pair| pair.0)
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
}
impl<T: WeakKey, S> WeakHashSet<T, S> {
pub fn iter(&self) -> Iter<'_, T> {
Iter(self.0.keys())
}
pub fn drain(&mut self) -> Drain<'_, T> {
Drain(self.0.drain())
}
}
impl<T, S, S1> PartialEq<WeakHashSet<T, S1>> for WeakHashSet<T, S>
where
T: WeakKey,
S: BuildHasher,
S1: BuildHasher,
{
fn eq(&self, other: &WeakHashSet<T, S1>) -> bool {
self.0 == other.0
}
}
impl<T: WeakKey, S: BuildHasher> Eq for WeakHashSet<T, S> where T::Key: Eq {}
impl<T: WeakKey, S: BuildHasher + Default> Default for WeakHashSet<T, S> {
fn default() -> Self {
WeakHashSet(base::WeakKeyHashMap::<T, (), S>::default())
}
}
impl<T, S> FromIterator<T::Strong> for WeakHashSet<T, S>
where
T: WeakKey,
S: BuildHasher + Default,
{
fn from_iter<I: IntoIterator<Item = T::Strong>>(iter: I) -> Self {
WeakHashSet(base::WeakKeyHashMap::<T, (), S>::from_iter(
iter.into_iter().map(|k| (k, ())),
))
}
}
impl<T: WeakKey, S: BuildHasher> Extend<T::Strong> for WeakHashSet<T, S> {
fn extend<I: IntoIterator<Item = T::Strong>>(&mut self, iter: I) {
self.0.extend(iter.into_iter().map(|k| (k, ())));
}
}
impl<T: WeakKey, S> Debug for WeakHashSet<T, S>
where
T::Strong: Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
impl<T: WeakKey, S> IntoIterator for WeakHashSet<T, S> {
type Item = T::Strong;
type IntoIter = IntoIter<T>;
fn into_iter(self) -> Self::IntoIter {
IntoIter(self.0.into_iter())
}
}
impl<'a, T: WeakKey, S> IntoIterator for &'a WeakHashSet<T, S> {
type Item = T::Strong;
type IntoIter = Iter<'a, T>;
fn into_iter(self) -> Self::IntoIter {
Iter(self.0.keys())
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::compat::rc::{Rc, Weak};
#[test]
fn test_retain_regresion() {
for _ in 0..20 {
let mut set: WeakHashSet<Weak<u8>> = WeakHashSet::default();
let mut preserve_vals = Vec::new();
const N: u8 = 50;
for i in 0..N {
let rc = Rc::new(i);
preserve_vals.push(rc.clone());
set.insert(rc);
}
let rc_n = Rc::new(N);
set.insert(rc_n.clone());
drop(preserve_vals);
let mut retain_called_on = Vec::new();
set.retain(|val| {
retain_called_on.push(val);
false
});
assert_eq!(retain_called_on, vec![rc_n]);
}
}
}