mun_memory/gc/
ptr.rs

1/// A `GcPtr` is what you interact with outside of the allocator. It is a pointer to a piece of
2/// memory that points to the actual data stored in memory.
3///
4/// This creates an indirection that must be followed to get to the actual data of the object. Note
5/// that the `GcPtr` must therefore be pinned in memory whereas the contained memory pointer may
6/// change.
7#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
8#[repr(transparent)]
9pub struct GcPtr(RawGcPtr);
10
11/// A `GcPtr` is thread safe.
12unsafe impl Send for GcPtr {}
13unsafe impl Sync for GcPtr {}
14
15/// A `RawGcPtr` is an unsafe version of a `GcPtr`. It represents the raw internal pointer
16/// semantics used by the runtime.
17pub type RawGcPtr = *const *mut std::ffi::c_void;
18
19pub trait HasIndirectionPtr {
20    /// Returns a pointer to the referenced memory.
21    ///
22    /// # Safety
23    ///
24    /// This is an unsafe method because derefencing could result in an access violation.
25    unsafe fn deref<T: Sized>(&self) -> *const T;
26
27    /// Returns a mutable pointer to the referenced memory.
28    ///
29    /// # Safety
30    ///
31    /// This is an unsafe method because derefencing could result in an access violation.
32    unsafe fn deref_mut<T: Sized>(&mut self) -> *mut T {
33        self.deref::<T>() as *mut _
34    }
35}
36
37impl HasIndirectionPtr for GcPtr {
38    unsafe fn deref<T: Sized>(&self) -> *const T {
39        (*self.0).cast()
40    }
41}
42
43impl From<GcPtr> for RawGcPtr {
44    fn from(ptr: GcPtr) -> Self {
45        ptr.0
46    }
47}
48
49impl From<RawGcPtr> for GcPtr {
50    fn from(ptr: RawGcPtr) -> Self {
51        GcPtr(ptr)
52    }
53}
54
55impl GcPtr {
56    pub(crate) fn as_ptr(self) -> RawGcPtr {
57        self.0
58    }
59}