Skip to content

Commit 9d07a4b

Browse files
committed
Require 'NullTrace' for CollectorId
The id is essentially a reference to a garbage collector. It's meaningless for a collector to collect itself, so it shouldn't need to trace references to itself. This is useful in case an API is parameterized by a 'CollectorId'.
1 parent ee21126 commit 9d07a4b

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

libs/context/src/collector.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::marker::PhantomData;
77

88
use slog::{Logger, o};
99

10-
use zerogc::{Gc, GcSafe, GcSystem, Trace, GcSimpleAlloc};
10+
use zerogc::{Gc, GcSafe, GcSystem, Trace, GcSimpleAlloc, NullTrace, TraceImmutable, GcVisitor};
1111

1212
use crate::{CollectorContext};
1313
use crate::state::{CollectionManager, RawContext};
@@ -294,6 +294,20 @@ unsafe impl<C: RawCollectorImpl> ::zerogc::CollectorId for CollectorId<C> {
294294
&*(self as *const CollectorId<C> as *const CollectorRef<C>)
295295
}
296296
}
297+
unsafe impl<C: RawCollectorImpl> Trace for CollectorId<C> {
298+
const NEEDS_TRACE: bool = false;
299+
#[inline(always)]
300+
fn visit<V: GcVisitor>(&mut self, _visitor: &mut V) -> Result<(), V::Err> {
301+
Ok(())
302+
}
303+
}
304+
unsafe impl<C: RawCollectorImpl> TraceImmutable for CollectorId<C> {
305+
#[inline(always)]
306+
fn visit_immutable<V: GcVisitor>(&self, _visitor: &mut V) -> Result<(), <V as GcVisitor>::Err> {
307+
Ok(())
308+
}
309+
}
310+
unsafe impl<C: RawCollectorImpl> NullTrace for CollectorId<C> {}
297311

298312
pub struct WeakCollectorRef<C: RawCollectorImpl> {
299313
weak: <C::Ptr as CollectorPtr<C>>::Weak,

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ impl<C: GcContext> FrozenContext<C> {
363363
///
364364
/// It should be safe to assume that a collector exists
365365
/// if any of its pointers still do!
366-
pub unsafe trait CollectorId: Copy + Eq + Debug + 'static {
366+
pub unsafe trait CollectorId: Copy + Eq + Debug + NullTrace + 'static {
367367
/// The type of the garbage collector system
368368
type System: GcSystem<Id=Self>;
369369
/// Perform a write barrier before writing to a garbage collected field

0 commit comments

Comments
 (0)