From 51cf2415d08cc3ff6741a60eac4acdc02270498f Mon Sep 17 00:00:00 2001 From: Adrien Champion Date: Tue, 27 Feb 2024 08:26:28 +0100 Subject: [PATCH 1/2] strong ref accessors, weak count accessor --- src/lib.rs | 29 +++++++++++++++++++++++------ tests/try_build/issue_1.stderr | 2 +- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c00789c..b7054af 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -309,7 +309,25 @@ impl HConsed { pub fn uid(&self) -> u64 { self.uid } - /// Turns a hashconsed thing in a weak hashconsed thing. + + /// Clones the underlying reference. + #[inline] + pub fn to_ref(&self) -> Arc { + self.elm.clone() + } + + /// Exposes the underlying reference. + #[inline] + pub fn as_ref(&self) -> &Arc { + &self.elm + } + + /// Generates a weak version of the underlying reference. + pub fn to_weak_ref(&self) -> Weak { + Arc::downgrade(&self.elm) + } + + /// Creates a weak version of itself. #[inline] pub fn to_weak(&self) -> WHConsed { WHConsed { @@ -318,15 +336,14 @@ impl HConsed { } } - /// Weak reference version. - pub fn to_weak_ref(&self) -> Weak { - Arc::downgrade(&self.elm) - } - /// Number of (strong) references to this term. pub fn arc_count(&self) -> usize { Arc::strong_count(&self.elm) } + /// Number of weak references to this term. + pub fn weak_count(&self) -> usize { + Arc::weak_count(&self.elm) + } } impl fmt::Debug for HConsed { diff --git a/tests/try_build/issue_1.stderr b/tests/try_build/issue_1.stderr index 379a339..32ece33 100644 --- a/tests/try_build/issue_1.stderr +++ b/tests/try_build/issue_1.stderr @@ -33,7 +33,7 @@ note: required because it's used within this closure 37 | s.spawn(move |_| { | ^^^^^^^^ note: required by a bound in `crossbeam_utils::thread::Scope::<'env>::spawn` - --> $CARGO/crossbeam-utils-0.8.16/src/thread.rs + --> $CARGO/crossbeam-utils-0.8.19/src/thread.rs | | pub fn spawn<'scope, F, T>(&'scope self, f: F) -> ScopedJoinHandle<'scope, T> | ----- required by a bound in this associated function From dbeab5000afba0f588907d0d9718280f4df24c6c Mon Sep 17 00:00:00 2001 From: Adrien Champion Date: Tue, 27 Feb 2024 08:31:15 +0100 Subject: [PATCH 2/2] clippy fixes --- src/lib.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b7054af..faba1b5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -312,13 +312,13 @@ impl HConsed { /// Clones the underlying reference. #[inline] - pub fn to_ref(&self) -> Arc { + pub fn to_arc(&self) -> Arc { self.elm.clone() } /// Exposes the underlying reference. #[inline] - pub fn as_ref(&self) -> &Arc { + pub fn as_arc(&self) -> &Arc { &self.elm } @@ -368,18 +368,22 @@ impl PartialEq for HConsed { } } impl Eq for HConsed {} + +#[allow(clippy::non_canonical_partial_ord_impl)] impl PartialOrd for HConsed { #[inline] fn partial_cmp(&self, other: &Self) -> Option { self.uid.partial_cmp(&other.uid) } } + impl Ord for HConsed { #[inline] fn cmp(&self, other: &Self) -> Ordering { self.uid.cmp(&other.uid) } } + impl Hash for HConsed { #[inline] fn hash(&self, state: &mut H) @@ -455,6 +459,8 @@ impl PartialEq for WHConsed { } } impl Eq for WHConsed {} + +#[allow(clippy::non_canonical_partial_ord_impl)] impl PartialOrd for WHConsed { #[inline] fn partial_cmp(&self, other: &Self) -> Option {