Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ jobs:
- name: Cargo check
run: cargo check --all-targets ${{ matrix.features }}
- name: Run tests
run: cargo test ${{ matrix.features }} --tests --examples
- name: Run doctests
if: ${{ ! contains(matrix.features, '--no-default-features') }}
run: cargo test ${{ matrix.features }} --doc
run: cargo test ${{ matrix.features }}

clippy:
name: Clippy
Expand Down
25 changes: 15 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ This is because the map holds the strings as `std::sync::Weak<str>`s.
```rust
use weak_table::WeakKeyHashMap;
use std::sync::{Arc, Weak};
# fn x() {
# type WeakKeyHashMap<T,U> = weak_table::WeakKeyHashMap<T, U, ahash::RandomState>;

let mut table = <WeakKeyHashMap<Weak<str>, u32>>::new();
let mut table = <WeakKeyHashMap<Weak<str>, u32>>::default();
let one = Arc::<str>::from("one");
let two = Arc::<str>::from("two");

Expand All @@ -91,6 +93,8 @@ drop(one);

assert_eq!( table.get("one"), None );
assert_eq!( table.get("two"), Some(&2) );
# }
# x();
```

Here we use a weak hash set to implement a simple string interning facility:
Expand All @@ -99,6 +103,8 @@ Here we use a weak hash set to implement a simple string interning facility:
use weak_table::WeakHashSet;
use std::ops::Deref;
use std::rc::{Rc, Weak};
# fn x() {
# type WeakHashSet<T> = weak_table::WeakHashSet<T, ahash::RandomState>;

#[derive(Clone, Debug)]
pub struct Symbol(Rc<str>);
Expand Down Expand Up @@ -137,17 +143,16 @@ impl SymbolTable {
}
}

fn interning_test() {
let mut tab = SymbolTable::new();
let mut tab = SymbolTable::new();

let a0 = tab.intern("a");
let a1 = tab.intern("a");
let b = tab.intern("b");
let a0 = tab.intern("a");
let a1 = tab.intern("a");
let b = tab.intern("b");

assert_eq!(a0, a1);
assert_ne!(a0, b);
}
# interning_test();
assert_eq!(a0, a1);
assert_ne!(a0, b);
# }
# x();
```

[ahash-issue]: https://github.com/tov/weak-table-rs/issues/23
Expand Down
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,12 @@ pub struct WeakKeyHashMap<K, V, ?S>(inner::Table<inner::WeakK<K>, inner::Owned<V
/// ```
/// use weak_table::PtrWeakKeyHashMap;
/// use std::rc::{Rc, Weak};
/// # fn x() {
/// # type PtrWeakKeyHashMap<T, U> = weak_table::PtrWeakKeyHashMap<T, U, ahash::RandomState>;
///
/// type Table = PtrWeakKeyHashMap<Weak<str>, usize>;
///
/// let mut map = Table::new();
/// let mut map = Table::default();
/// let a = Rc::<str>::from("hello");
/// let b = Rc::<str>::from("hello");
///
Expand All @@ -86,6 +88,8 @@ pub struct WeakKeyHashMap<K, V, ?S>(inner::Table<inner::WeakK<K>, inner::Owned<V
///
/// assert_eq!( map.get(&a), Some(&5) );
/// assert_eq!( map.get(&b), Some(&7) );
/// }
/// x();
/// ```
#[derive(Clone)]
pub struct PtrWeakKeyHashMap<K, V,?S>(WeakKeyHashMap<by_ptr::ByPtr<K>, V, S>);
Expand Down
6 changes: 5 additions & 1 deletion src/weak_hash_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,19 @@ impl<T: WeakKey, S: BuildHasher> WeakHashSet<T, S> {
/// use weak_table::WeakHashSet;
/// use std::rc::{Rc, Weak};
/// use std::ops::Deref;
/// # fn x() {
/// # type WeakHashSet<T> = weak_table::WeakHashSet<T, ahash::RandomState>;
///
/// let mut set: WeakHashSet<Weak<String>> = WeakHashSet::new();
/// let mut set: WeakHashSet<Weak<String>> = WeakHashSet::default();
///
/// let a = Rc::new("a".to_owned());
/// set.insert(a.clone());
///
/// let also_a = set.get("a").unwrap();
///
/// assert!(Rc::ptr_eq( &a, &also_a ));
/// # }
/// # x();
/// ```
///
/// expected *O*(1) time; worst-case *O*(*p*) time
Expand Down