crftng-intrprtrs/gc/src/gc_ref.rs

34 lines
762 B
Rust

use std::{marker::PhantomData, ops::Deref, ptr::NonNull};
use crate::trace::GCTrace;
pub struct GcRef<T: GCTrace>(pub(crate) NonNull<T>, PhantomData<T>);
impl<T: GCTrace> Clone for GcRef<T> {
fn clone(&self) -> Self {
Self(self.0, self.1)
}
}
impl<T: GCTrace> Deref for GcRef<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
unsafe { self.0.as_ref() }
}
}
impl<T: GCTrace> GcRef<T> {
pub(crate) unsafe fn new(ptr: NonNull<T>) -> Self {
Self(ptr, PhantomData)
}
/// # Safety
/// Ensure that this is the only instance of a pointer to the underlying value.
/// You might want to instead use one of various [cell][`std::cell`] types as the allocated
/// type
pub unsafe fn get_mut(this: &mut Self) -> &mut T {
this.0.as_mut()
}
}