use std::marker::PhantomData;
pub struct MkFrameVec<'a, T> {
ptr: *mut T,
len: usize,
capacity: usize,
_marker: PhantomData<&'a T>,
}
impl<'a, T> MkFrameVec<'a, T> {
pub(crate) unsafe fn from_raw_parts(ptr: *mut T, capacity: usize) -> Self {
Self {
ptr,
len: 0,
capacity,
_marker: PhantomData,
}
}
pub fn push(&mut self, value: T) -> Result<(), T> {
if self.len >= self.capacity {
return Err(value);
}
unsafe {
self.ptr.add(self.len).write(value);
}
self.len += 1;
Ok(())
}
pub fn len(&self) -> usize {
self.len
}
pub fn is_empty(&self) -> bool {
self.len == 0
}
pub fn capacity(&self) -> usize {
self.capacity
}
pub fn clear(&mut self) {
for i in 0..self.len {
unsafe {
std::ptr::drop_in_place(self.ptr.add(i));
}
}
self.len = 0;
}
}
impl<'a, T> std::ops::Deref for MkFrameVec<'a, T> {
type Target = [T];
fn deref(&self) -> &Self::Target {
unsafe { std::slice::from_raw_parts(self.ptr, self.len) }
}
}
impl<'a, T> std::ops::DerefMut for MkFrameVec<'a, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { std::slice::from_raw_parts_mut(self.ptr, self.len) }
}
}