From 7c336c87695955a222e9e6d3d55987e6539c8c50 Mon Sep 17 00:00:00 2001 From: Aaron Kaiser Date: Tue, 7 May 2024 10:34:42 +0200 Subject: [PATCH] feat: get rid of lifetime annotations --- src/sharedptr.rs | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/sharedptr.rs b/src/sharedptr.rs index 5c70540..8750384 100644 --- a/src/sharedptr.rs +++ b/src/sharedptr.rs @@ -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(*mut u8); -impl SharedPtr<'_, N> { +unsafe impl Send for SharedPtr {} + +impl SharedPtr { pub fn new() -> Option { 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 SharedPtr<'_, N> { } } -impl<'a, const N: usize> Deref for SharedPtr<'a, N> { +impl Deref for SharedPtr { 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 DerefMut for SharedPtr { 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 Drop for SharedPtr<'_, N> { +impl Drop for SharedPtr { fn drop(&mut self) { let mut allocator = ALLOCATOR.lock().unwrap(); unsafe { - allocator.deallocate(self.0.as_mut_ptr()); + allocator.deallocate(self.0); } } }