weak_table/lib.rs
1#![doc = include_str!("../README.md")]
2#![cfg_attr(not(feature = "std"), no_std)]
3
4use self::compat::*;
5
6pub mod ptr_weak_hash_set;
7pub mod ptr_weak_key_hash_map;
8pub mod ptr_weak_weak_hash_map;
9pub mod traits;
10pub mod weak_hash_set;
11pub mod weak_key_hash_map;
12pub mod weak_value_hash_map;
13pub mod weak_weak_hash_map;
14
15#[cfg(test)]
16mod tests;
17
18mod by_ptr;
19mod compat;
20mod inner;
21mod size_policy;
22mod util;
23
24/// A hash map with weak keys, hashed on key value.
25///
26/// When a weak pointer expires, its mapping is lazily removed.
27#[derive(Clone)]
28pub struct WeakKeyHashMap<K, V, S = RandomState>(inner::Table<inner::WeakK<K>, inner::Owned<V>, S>);
29
30/// A hash map with weak keys, hashed on key pointer.
31///
32/// When a weak pointer expires, its mapping is lazily removed.
33///
34/// # Examples
35///
36/// ```
37/// use weak_table::PtrWeakKeyHashMap;
38/// use std::rc::{Rc, Weak};
39///
40/// type Table = PtrWeakKeyHashMap<Weak<str>, usize>;
41///
42/// let mut map = Table::new();
43/// let a = Rc::<str>::from("hello");
44/// let b = Rc::<str>::from("hello");
45///
46/// map.insert(a.clone(), 5);
47///
48/// assert_eq!( map.get(&a), Some(&5) );
49/// assert_eq!( map.get(&b), None );
50///
51/// map.insert(b.clone(), 7);
52///
53/// assert_eq!( map.get(&a), Some(&5) );
54/// assert_eq!( map.get(&b), Some(&7) );
55/// ```
56#[derive(Clone)]
57pub struct PtrWeakKeyHashMap<K, V, S = RandomState>(WeakKeyHashMap<by_ptr::ByPtr<K>, V, S>);
58
59/// A hash map with weak values.
60///
61/// When a weak pointer expires, its mapping is lazily removed.
62#[derive(Clone)]
63pub struct WeakValueHashMap<K, V, S = RandomState>(
64 inner::Table<inner::Owned<K>, inner::WeakV<V>, S>,
65);
66
67/// A hash map with weak keys and weak values, hashed on key value.
68///
69/// When a weak pointer expires, its mapping is lazily removed.
70#[derive(Clone)]
71pub struct WeakWeakHashMap<K, V, S = RandomState>(
72 inner::Table<inner::WeakK<K>, inner::WeakV<V>, S>,
73);
74
75/// A hash map with weak keys and weak values, hashed on key pointer.
76///
77/// When a weak pointer expires, its mapping is lazily removed.
78#[derive(Clone)]
79pub struct PtrWeakWeakHashMap<K, V, S = RandomState>(WeakWeakHashMap<by_ptr::ByPtr<K>, V, S>);
80
81/// A hash set with weak elements, hashed on element value.
82///
83/// When a weak pointer expires, its mapping is lazily removed.
84#[derive(Clone)]
85pub struct WeakHashSet<T, S = RandomState>(WeakKeyHashMap<T, (), S>);
86
87/// A hash set with weak elements, hashed on element pointer.
88///
89/// When a weak pointer expires, its mapping is lazily removed.
90#[derive(Clone)]
91pub struct PtrWeakHashSet<T, S = RandomState>(PtrWeakKeyHashMap<T, (), S>);