use crate::alloc::access::Accessor;
use crate::alloc::{Alloc, AllocMut};
use crate::error::Error;
pub mod alloc;
pub mod error;
pub mod mark;
pub mod trace;
pub trait Heap {
type Handle;
type Allocator;
fn create_allocator(&self) -> Self::Allocator;
fn handle(&self) -> Self::Handle;
}
#[repr(transparent)]
pub struct Gc<T: ?Sized, H: Alloc<T>> {
handle: <H as Alloc<T>>::RawHandle,
}
impl<T: ?Sized, H: Alloc<T>> Gc<T, H> {
#[inline(always)]
pub fn get<'a, A>(&'a self, allocator: &'a A) -> <A as Accessor<T, H>>::Guard<'a>
where
A: Accessor<T, H>,
{
allocator.read(self)
}
#[inline(always)]
pub fn try_get<'a, A>(
&'a self,
allocator: &'a A,
) -> Result<<A as Accessor<T, H>>::Guard<'a>, Error>
where
A: Accessor<T, H>,
{
allocator.try_read(self)
}
pub fn into_raw(self) -> <H as Alloc<T>>::RawHandle {
self.handle
}
pub fn as_raw(&self) -> &<H as Alloc<T>>::RawHandle {
&self.handle
}
pub unsafe fn from_raw(raw: <H as Alloc<T>>::RawHandle) -> Self {
Gc { handle: raw }
}
}
impl<T: ?Sized, H: Alloc<T>> Copy for Gc<T, H> where <H as Alloc<T>>::RawHandle: Copy {}
impl<T: ?Sized, H: Alloc<T>> Clone for Gc<T, H>
where
<H as Alloc<T>>::RawHandle: Clone,
{
fn clone(&self) -> Self {
Gc {
handle: self.handle.clone(),
}
}
}
pub type GcMut<T, H> = Gc<<H as Alloc<T>>::MutTy, H>;