feat: get rid of lifetime annotations

This commit is contained in:
2024-05-07 10:34:42 +02:00
parent 72b2e58244
commit 7c336c8769

View File

@@ -1,24 +1,20 @@
use core::slice;
use std::{
ops::{Deref, DerefMut},
usize,
};
use std::ops::{Deref, DerefMut};
use crate::allocator::ALLOCATOR;
#[derive(Debug)]
pub struct SharedPtr<'a, const N: usize>(&'a mut [u8; N]);
pub struct SharedPtr<const N: usize>(*mut u8);
impl<const N: usize> SharedPtr<'_, N> {
unsafe impl<const N: usize> Send for SharedPtr<N> {}
impl<const N: usize> SharedPtr<N> {
pub fn new() -> Option<Self> {
let mut allocator = ALLOCATOR.lock().unwrap();
let buf = unsafe {
let buf = allocator.allocate(N)?;
slice::from_raw_parts_mut(buf, N)
};
let buf = unsafe { allocator.allocate(N)? };
Some(SharedPtr(buf.try_into().expect("Should never fail")))
Some(SharedPtr(buf))
}
pub fn get_offset(&self) -> usize {
@@ -28,26 +24,34 @@ impl<const N: usize> SharedPtr<'_, N> {
}
}
impl<'a, const N: usize> Deref for SharedPtr<'a, N> {
impl<const N: usize> Deref for SharedPtr<N> {
type Target = [u8; N];
fn deref(&self) -> &Self::Target {
&self.0
unsafe {
slice::from_raw_parts(self.0, N)
.try_into()
.expect("This should never fail")
}
}
}
impl<'a, const N: usize> DerefMut for SharedPtr<'a, N> {
impl<const N: usize> DerefMut for SharedPtr<N> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
unsafe {
slice::from_raw_parts_mut(self.0, N)
.try_into()
.expect("This should never fail")
}
}
}
impl<const N: usize> Drop for SharedPtr<'_, N> {
impl<const N: usize> Drop for SharedPtr<N> {
fn drop(&mut self) {
let mut allocator = ALLOCATOR.lock().unwrap();
unsafe {
allocator.deallocate(self.0.as_mut_ptr());
allocator.deallocate(self.0);
}
}
}