use std::{marker, ptr};
use crate::alloc::{self, tail};
#[repr(transparent)]
pub struct Resident<'a, H, T, N: alloc::raw::TailExtent = usize> {
pointer: ptr::NonNull<T>,
borrow: marker::PhantomData<&'a tail::Block<H, T, N>>,
}
impl<'a, H, T, N: alloc::raw::TailExtent> Resident<'a, H, T, N> {
pub unsafe fn new(view: tail::View<'a, H, T, N>, index: N::Index) -> Self {
let pointer = unsafe { view.data().add(alloc::raw::TailIndex::get(index)) };
Self {
pointer,
borrow: marker::PhantomData,
}
}
pub fn get(self) -> &'a T {
unsafe { self.pointer.as_ref() }
}
pub unsafe fn view(self, index: N::Index) -> tail::View<'a, H, T, N> {
let pointer = unsafe {
self.pointer
.sub(alloc::raw::TailIndex::get(index))
.cast::<u8>()
.sub(tail::Plan::<H, T, N>::tail_offset())
.cast()
};
unsafe { tail::View::from_raw(pointer) }
}
}
impl<H, T, N: alloc::raw::TailExtent> Clone for Resident<'_, H, T, N> {
fn clone(&self) -> Self {
*self
}
}
impl<H, T, N: alloc::raw::TailExtent> Copy for Resident<'_, H, T, N> {}
const _: () = {
assert!(std::mem::size_of::<Resident<'static, u8, u8>>() == std::mem::size_of::<usize>());
assert!(
std::mem::size_of::<Option<Resident<'static, u8, u8>>>() == std::mem::size_of::<usize>()
);
};