Skip to content

Commit d84a1cd

Browse files
committed
replace Guard::belongs_to with Guard::collector and Collector::eq
1 parent a6199ae commit d84a1cd

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
lines changed

src/collector.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,15 @@ impl Collector {
228228
pub unsafe fn reclaim_all(&self) {
229229
unsafe { self.raw.reclaim_all() };
230230
}
231+
}
232+
233+
impl Eq for Collector {}
231234

235+
impl PartialEq for Collector {
236+
/// Checks if both references point to the same collector.
232237
#[inline]
233-
pub(crate) fn id_eq(this: &Collector, other: &Collector) -> bool {
234-
this.id == other.id
238+
fn eq(&self, other: &Self) -> bool {
239+
self.id == other.id
235240
}
236241
}
237242

src/guard.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,8 @@ pub trait Guard {
8787
/// this thread.
8888
fn thread_id(&self) -> usize;
8989

90-
/// Returns `true` if this guard belongs to the given collector.
91-
///
92-
/// This can be used to verify that user-provided guards are valid
93-
/// for the expected collector.
94-
fn belongs_to(&self, collector: &Collector) -> bool;
90+
/// Returns the collector this guard was created from.
91+
fn collector(&self) -> &Collector;
9592
}
9693

9794
/// A guard that keeps the current thread marked as active.
@@ -186,10 +183,9 @@ impl Guard for LocalGuard<'_> {
186183
self.thread.id
187184
}
188185

189-
/// Returns `true` if this guard belongs to the given collector.
190-
#[inline]
191-
fn belongs_to(&self, collector: &Collector) -> bool {
192-
Collector::id_eq(self.collector, collector)
186+
/// Returns the collector this guard was created from.
187+
fn collector(&self) -> &Collector {
188+
self.collector
193189
}
194190
}
195191

@@ -313,10 +309,9 @@ impl Guard for OwnedGuard<'_> {
313309
Thread::current().id
314310
}
315311

316-
/// Returns `true` if this guard belongs to the given collector.
317-
#[inline]
318-
fn belongs_to(&self, collector: &Collector) -> bool {
319-
Collector::id_eq(self.collector, collector)
312+
/// Returns the collector this guard was created from.
313+
fn collector(&self) -> &Collector {
314+
self.collector
320315
}
321316
}
322317

tests/lib.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -415,14 +415,19 @@ fn owned_guard_concurrent() {
415415
}
416416

417417
#[test]
418-
fn belongs_to() {
418+
fn collector_equality() {
419419
let a = Collector::new();
420420
let b = Collector::new();
421421

422-
assert!(a.enter().belongs_to(&a));
423-
assert!(!a.enter().belongs_to(&b));
424-
assert!(b.enter().belongs_to(&b));
425-
assert!(!b.enter().belongs_to(&a));
422+
assert_eq!(a, a);
423+
assert_eq!(b, b);
424+
assert_ne!(a, b);
425+
426+
assert_eq!(*a.enter().collector(), a);
427+
assert_ne!(*a.enter().collector(), b);
428+
429+
assert_eq!(*b.enter().collector(), b);
430+
assert_ne!(*b.enter().collector(), a);
426431
}
427432

428433
#[test]

0 commit comments

Comments
 (0)