From f71c5f3f12c047d60d1f39fef4040a89f3ab6884 Mon Sep 17 00:00:00 2001 From: Leni Aniva Date: Wed, 11 Mar 2026 21:12:40 -0700 Subject: [PATCH 1/2] 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. From 226b18b7376f1c215bd7d44ac358d96a7dd5a36b Mon Sep 17 00:00:00 2001 From: Leni Aniva Date: Wed, 1 Apr 2026 00:10:42 -0700 Subject: [PATCH 2/2] chore: Format code --- src/lib.rs | 50 +++++++++++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7897171..be47ea2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -460,38 +460,34 @@ 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() - } + 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) - } + 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.