use crate::{
Trace, TraceImmutable, GcVisitor, NullTrace, CollectorId,
GcSafe, GcSystem, GcContext,
};
use std::ptr::NonNull;
pub fn gc<'gc, T: GcSafe + 'static>(ptr: &'static T) -> Gc<'gc, T> {
unsafe {
Gc::from_raw(
DummyCollectorId { _priv: () },
NonNull::from(ptr)
)
}
}
pub fn leaked<'gc, T: GcSafe + 'static>(value: T) -> Gc<'gc, T> {
gc(Box::leak(Box::new(value)))
}
pub type Gc<'gc, T> = crate::Gc<'gc, T, DummyCollectorId>;
pub struct DummyContext {
_priv: ()
}
unsafe impl GcContext for DummyContext {
type System = DummySystem;
type Id = DummyCollectorId;
unsafe fn basic_safepoint<T: Trace>(&mut self, _value: &mut &mut T) {
}
unsafe fn freeze(&mut self) {
unimplemented!()
}
unsafe fn unfreeze(&mut self) {
unimplemented!()
}
unsafe fn recurse_context<T, F, R>(&self, value: &mut &mut T, func: F) -> R
where T: Trace, F: for<'gc> FnOnce(&'gc mut Self, &'gc mut T) -> R {
let mut child = DummyContext { _priv: () };
func(&mut child, &mut *value)
}
}
#[derive(Default)]
pub struct DummySystem {
_priv: ()
}
impl DummySystem {
pub fn new() -> Self {
DummySystem::default()
}
pub fn new_context(&self) -> DummyContext {
DummyContext {
_priv: ()
}
}
}
unsafe impl GcSystem for DummySystem {
type Id = DummyCollectorId;
type Context = DummyContext;
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct DummyCollectorId {
_priv: ()
}
unsafe impl Trace for DummyCollectorId {
const NEEDS_TRACE: bool = false;
fn visit<V: GcVisitor>(&mut self, _visitor: &mut V) -> Result<(), <V as GcVisitor>::Err> {
Ok(())
}
}
unsafe impl TraceImmutable for DummyCollectorId {
fn visit_immutable<V: GcVisitor>(&self, _visitor: &mut V) -> Result<(), V::Err> {
Ok(())
}
}
unsafe impl NullTrace for DummyCollectorId {}
unsafe impl CollectorId for DummyCollectorId {
type System = DummySystem;
unsafe fn gc_write_barrier<'gc, T, V>(
_owner: &Gc<'gc, T>,
_value: &Gc<'gc, V>,
_field_offset: usize
) where T: GcSafe + ?Sized + 'gc, V: GcSafe + ?Sized + 'gc {}
unsafe fn assume_valid_system(&self) -> &Self::System {
unimplemented!()
}
}