From f71c5f3f12c047d60d1f39fef4040a89f3ab6884 Mon Sep 17 00:00:00 2001 From: Leni Aniva Date: Wed, 11 Mar 2026 21:12:40 -0700 Subject: [PATCH] feat: Add weak key implementation --- Cargo.toml | 5 +++++ src/lib.rs | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 739885b..6dce5b8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,7 @@ features = ["unstable_docrs"] [features] with_ahash = ["ahash"] unstable_docrs = ["with_ahash"] +weak-table = ["dep:weak-table"] [dependencies] lazy_static = "1.*" @@ -32,6 +33,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 c00789c..15b1222 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -451,6 +451,43 @@ 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.