diff --git a/Cargo.toml b/Cargo.toml index e294ad3..1a8cb41 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ features = ["unstable_docrs"] [features] with_ahash = ["ahash"] unstable_docrs = ["with_ahash"] +weak-table = ["dep:weak-table"] [dependencies] lazy_static = "1.*" @@ -33,6 +34,10 @@ lazy_static = "1.*" version = "^0.8.3" optional = true +[dependencies.weak-table] +version = "^0.3.0" +optional = true + [dev-dependencies] crossbeam-utils = "^0.8" trybuild = "^1.0" diff --git a/src/lib.rs b/src/lib.rs index 301de31..be47ea2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -455,6 +455,39 @@ impl Ord for WHConsed { } } +#[cfg(feature = "weak-table")] +use weak_table::traits::{WeakElement, WeakKey}; + +#[cfg(feature = "weak-table")] +impl WeakElement for WHConsed { + type Strong = HConsed; + fn new(x: &Self::Strong) -> Self { + x.to_weak() + } + fn view(&self) -> Option { + self.to_hconsed() + } + fn is_expired(&self) -> bool { + self.elm.is_expired() + } + fn clone(x: &Self::Strong) -> Self::Strong + where + Self: Sized, + { + x.clone() + } +} +#[cfg(feature = "weak-table")] +impl WeakKey for WHConsed { + type Key = T; + fn with_key(view: &Self::Strong, f: F) -> R + where + F: FnOnce(&Self::Key) -> R, + { + f(view) + } +} + /// The consign storing the actual hash consed elements as `HConsed`s. pub struct HConsign { /// The actual hash consing table.