Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/gc-arena-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ fn collect_derive(mut s: synstructure::Structure) -> TokenStream {
let ty = &b.ast().ty;
quote_spanned!(b.ast().span()=>
|| <#ty as gc_arena::Collect>::needs_trace()
).to_tokens(&mut needs_trace_body);
)
.to_tokens(&mut needs_trace_body);
}
}
// Likewise, this will skip any fields that have `#[collect(require_static)]`
Expand Down
7 changes: 3 additions & 4 deletions src/gc-arena/src/collect_impl.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use alloc::boxed::Box;
use alloc::collections::VecDeque;
use alloc::collections::{BTreeMap, BTreeSet};
use alloc::rc::Rc;
use alloc::string::String;
use alloc::sync::Arc;
use alloc::vec::Vec;
use alloc::collections::VecDeque;
use core::cell::{Cell, RefCell};
use core::marker::PhantomData;
#[cfg(feature = "std")]
use core::hash::{BuildHasher, Hash};
use core::marker::PhantomData;
#[cfg(feature = "std")]
use std::collections::{HashMap, HashSet};

Expand Down Expand Up @@ -271,8 +271,7 @@ where
}

// SAFETY: `PhantomData` is a ZST, and therefore doesn't store anything
unsafe impl<T> Collect for PhantomData<T>
{
unsafe impl<T> Collect for PhantomData<T> {
#[inline]
fn needs_trace() -> bool {
false
Expand Down
22 changes: 15 additions & 7 deletions src/gc-arena/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,13 +264,20 @@ impl Context {
);
}

let gc_box = GcBox {
flags: GcFlags::new(),
next: Cell::new(self.all.get()),
value: UnsafeCell::new(t),
};
gc_box.flags.set_needs_trace(T::needs_trace());
let ptr = NonNull::new_unchecked(Box::into_raw(Box::new(gc_box)));
let flags = GcFlags::new();
flags.set_needs_trace(T::needs_trace());

let mut uninitialized = Box::new(mem::MaybeUninit::<GcBox<T>>::uninit());

core::ptr::write(&mut (*uninitialized.as_mut_ptr()).flags, flags);
core::ptr::write(
&mut (*uninitialized.as_mut_ptr()).next,
Cell::new(self.all.get()),
);
core::ptr::write(&mut (*uninitialized.as_mut_ptr()).value, UnsafeCell::new(t));

let ptr = NonNull::new_unchecked(Box::into_raw(uninitialized) as *mut GcBox<T>);

self.all.set(Some(static_gc_box(ptr)));
if self.phase.get() == Phase::Sweep && self.sweep_prev.get().is_none() {
self.sweep_prev.set(self.all.get());
Expand Down Expand Up @@ -317,6 +324,7 @@ enum Phase {
Sleep,
}

#[inline]
unsafe fn static_gc_box<'gc>(
ptr: NonNull<GcBox<dyn Collect + 'gc>>,
) -> NonNull<GcBox<dyn Collect>> {
Expand Down
5 changes: 5 additions & 0 deletions src/gc-arena/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ pub(crate) struct GcBox<T: Collect + ?Sized> {
pub(crate) struct GcFlags(Cell<u8>);

impl GcFlags {
#[inline]
pub(crate) fn new() -> GcFlags {
GcFlags(Cell::new(0))
}

#[inline]
pub(crate) fn color(&self) -> GcColor {
match self.0.get() & 0x3 {
0x0 => GcColor::White,
Expand All @@ -33,6 +35,7 @@ impl GcFlags {
}
}

#[inline]
pub(crate) fn set_color(&self, color: GcColor) {
self.0.set(
(self.0.get() & !0x3)
Expand All @@ -44,10 +47,12 @@ impl GcFlags {
)
}

#[inline]
pub(crate) fn needs_trace(&self) -> bool {
self.0.get() & 0x4 != 0x0
}

#[inline]
pub(crate) fn set_needs_trace(&self, needs_trace: bool) {
self.0
.set((self.0.get() & !0x4) | if needs_trace { 0x4 } else { 0x0 });
Expand Down