From 5cd37dfc93aa9bc44df47571396dff70d773fcb2 Mon Sep 17 00:00:00 2001 From: Aaron Kaiser Date: Fri, 26 Apr 2024 11:58:38 +0200 Subject: [PATCH] feat: put lenght of SharedPtr buffer in type information --- src/sharedptr.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/sharedptr.rs b/src/sharedptr.rs index a069714..d8fdc5e 100644 --- a/src/sharedptr.rs +++ b/src/sharedptr.rs @@ -7,15 +7,15 @@ use std::{ use crate::allocator::ALLOCATOR; #[derive(Debug)] -pub struct SharedPtr<'a>(&'a mut [u8]); +pub struct SharedPtr<'a, const N: usize>(&'a mut [u8]); -impl SharedPtr<'_> { - pub fn new(size: usize) -> Option { +impl SharedPtr<'_, N> { + pub fn new() -> Option { let mut allocator = ALLOCATOR.lock().unwrap(); let buf = unsafe { - let buf = allocator.allocate(size)?; - slice::from_raw_parts_mut(buf, size) + let buf = allocator.allocate(N)?; + slice::from_raw_parts_mut(buf, N) }; Some(SharedPtr(buf)) @@ -30,7 +30,7 @@ impl SharedPtr<'_> { } } -impl<'a> Deref for SharedPtr<'a> { +impl<'a, const N: usize> Deref for SharedPtr<'a, N> { type Target = &'a mut [u8]; fn deref(&self) -> &Self::Target { @@ -38,13 +38,13 @@ impl<'a> Deref for SharedPtr<'a> { } } -impl<'a> DerefMut for SharedPtr<'a> { +impl<'a, const N: usize> DerefMut for SharedPtr<'a, N> { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } -impl Drop for SharedPtr<'_> { +impl Drop for SharedPtr<'_, N> { fn drop(&mut self) { let mut allocator = ALLOCATOR.lock().unwrap(); @@ -60,7 +60,7 @@ mod tests { #[test] fn test() { - let mut x = SharedPtr::new(10).unwrap(); + let mut x = SharedPtr::<10>::new().unwrap(); x[0] = 1; assert_eq!(x[0], 1); drop(x);