Skip to content

Commit 6188701

Browse files
authored
Merge pull request #25 from lenianiva/weak-table
feat: Implement `weak-table` traits
2 parents a3dbbd5 + 226b18b commit 6188701

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ features = ["unstable_docrs"]
2525
[features]
2626
with_ahash = ["ahash"]
2727
unstable_docrs = ["with_ahash"]
28+
weak-table = ["dep:weak-table"]
2829

2930
[dependencies]
3031
lazy_static = "1.*"
@@ -33,6 +34,10 @@ lazy_static = "1.*"
3334
version = "^0.8.3"
3435
optional = true
3536

37+
[dependencies.weak-table]
38+
version = "^0.3.0"
39+
optional = true
40+
3641
[dev-dependencies]
3742
crossbeam-utils = "^0.8"
3843
trybuild = "^1.0"

src/lib.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,39 @@ impl<T> Ord for WHConsed<T> {
455455
}
456456
}
457457

458+
#[cfg(feature = "weak-table")]
459+
use weak_table::traits::{WeakElement, WeakKey};
460+
461+
#[cfg(feature = "weak-table")]
462+
impl<T> WeakElement for WHConsed<T> {
463+
type Strong = HConsed<T>;
464+
fn new(x: &Self::Strong) -> Self {
465+
x.to_weak()
466+
}
467+
fn view(&self) -> Option<Self::Strong> {
468+
self.to_hconsed()
469+
}
470+
fn is_expired(&self) -> bool {
471+
self.elm.is_expired()
472+
}
473+
fn clone(x: &Self::Strong) -> Self::Strong
474+
where
475+
Self: Sized,
476+
{
477+
x.clone()
478+
}
479+
}
480+
#[cfg(feature = "weak-table")]
481+
impl<T: std::hash::Hash + Eq> WeakKey for WHConsed<T> {
482+
type Key = T;
483+
fn with_key<F, R>(view: &Self::Strong, f: F) -> R
484+
where
485+
F: FnOnce(&Self::Key) -> R,
486+
{
487+
f(view)
488+
}
489+
}
490+
458491
/// The consign storing the actual hash consed elements as `HConsed`s.
459492
pub struct HConsign<T: Hash + Eq + Clone, S = RandomState> {
460493
/// The actual hash consing table.

0 commit comments

Comments
 (0)