29 lines
685 B
Rust
29 lines
685 B
Rust
use std::{marker::PhantomData, ops::Deref, ptr::NonNull};
|
|
|
|
use crate::trace::GCTrace;
|
|
|
|
#[derive(Clone)]
|
|
pub struct GcRef<T: GCTrace>(pub(crate) NonNull<T>, PhantomData<T>);
|
|
|
|
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()
|
|
}
|
|
}
|