use std::{marker, ptr, slice};
use crate::alloc::{self, tail};
#[repr(transparent)]
pub struct View<'a, H, T, N: alloc::raw::TailExtent = usize> {
pub(in crate::alloc::tail) pointer: ptr::NonNull<tail::Prefix<H, T, N>>,
pub(in crate::alloc::tail) borrow: marker::PhantomData<&'a tail::Block<H, T, N>>,
}
impl<'a, H, T, N: alloc::raw::TailExtent> View<'a, H, T, N> {
pub(in crate::alloc) fn data(&self) -> ptr::NonNull<T> {
let offset = tail::Plan::<H, T, N>::tail_offset();
unsafe { self.pointer.cast::<u8>().add(offset).cast() }
}
pub(in crate::alloc) unsafe fn from_raw(pointer: ptr::NonNull<u8>) -> Self {
Self {
pointer: pointer.cast(),
borrow: marker::PhantomData,
}
}
pub fn new(block: &'a tail::Block<H, T, N>) -> Self {
Self {
pointer: block.pointer,
borrow: marker::PhantomData,
}
}
pub fn header(self) -> &'a H {
unsafe { &self.pointer.as_ref().header }
}
pub fn is_empty(self) -> bool {
self.len() == 0
}
pub fn len(self) -> usize {
self.len_proof().len()
}
pub fn as_slice(&self) -> &'a [T] {
let len = self.len_proof();
unsafe { slice::from_raw_parts(self.data().as_ptr(), len.len()) }
}
pub(in crate::alloc::tail) fn len_proof(&self) -> tail::Plan<H, T, N> {
unsafe { self.pointer.as_ref().len }
}
}
impl<H, T, N: alloc::raw::TailExtent> Clone for View<'_, H, T, N> {
fn clone(&self) -> Self {
*self
}
}
impl<H, T, N: alloc::raw::TailExtent> Copy for View<'_, H, T, N> {}