From 0209e4d7fff70b7d6632d08e30b940ed4b5b29c9 Mon Sep 17 00:00:00 2001 From: uselessgoddess Date: Fri, 28 Apr 2023 21:37:39 +0300 Subject: [PATCH] Implement initiative --- src/alloc.rs | 15 +++++++++++---- src/file_mapped.rs | 15 +++++++++++---- src/lib.rs | 23 +++++++++++++++-------- src/raw_mem.rs | 6 ++---- 4 files changed, 39 insertions(+), 20 deletions(-) diff --git a/src/alloc.rs b/src/alloc.rs index 81ddec4..d12aadb 100644 --- a/src/alloc.rs +++ b/src/alloc.rs @@ -8,6 +8,7 @@ use { alloc::{Allocator, Layout}, fmt::{self, Debug, Formatter}, mem::{self, MaybeUninit}, + ops::{Deref, DerefMut}, ptr, }, }; @@ -23,16 +24,22 @@ impl Alloc { } } -impl RawMem for Alloc { - type Item = T; +impl Deref for Alloc { + type Target = [T]; - fn allocated(&self) -> &[Self::Item] { + fn deref(&self) -> &Self::Target { unsafe { self.buf.as_slice() } } +} - fn allocated_mut(&mut self) -> &mut [Self::Item] { +impl DerefMut for Alloc { + fn deref_mut(&mut self) -> &mut Self::Target { unsafe { self.buf.as_slice_mut() } } +} + +impl RawMem for Alloc { + type Item = T; unsafe fn grow( &mut self, diff --git a/src/file_mapped.rs b/src/file_mapped.rs index 1183836..addecd8 100644 --- a/src/file_mapped.rs +++ b/src/file_mapped.rs @@ -7,6 +7,7 @@ use { fs::File, io, mem::{self, MaybeUninit}, + ops::{Deref, DerefMut}, path::Path, ptr::{self, NonNull}, }, @@ -42,16 +43,22 @@ impl FileMapped { } } -impl RawMem for FileMapped { - type Item = T; +impl Deref for FileMapped { + type Target = [T]; - fn allocated(&self) -> &[Self::Item] { + fn deref(&self) -> &Self::Target { unsafe { self.buf.as_slice() } } +} - fn allocated_mut(&mut self) -> &mut [Self::Item] { +impl DerefMut for FileMapped { + fn deref_mut(&mut self) -> &mut Self::Target { unsafe { self.buf.as_slice_mut() } } +} + +impl RawMem for FileMapped { + type Item = T; unsafe fn grow( &mut self, diff --git a/src/lib.rs b/src/lib.rs index b512559..790fa11 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -45,18 +45,25 @@ macro_rules! delegate_memory { use std::{ mem::MaybeUninit, fmt::{self, Formatter}, + ops::{Deref, DerefMut}, }; - impl<$param> RawMem for $me<$param> { - type Item = $param; + impl<$param> Deref for $me<$param> { + type Target = [$param]; - fn allocated(&self) -> &[Self::Item] { - self.0.allocated() + fn deref(&self) -> &Self::Target { + &*self.0 } + } - fn allocated_mut(&mut self) -> &mut [Self::Item] { - self.0.allocated_mut() + impl<$param> DerefMut for$me<$param> { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut *self.0 } + } + + impl<$param> RawMem for $me<$param> { + type Item = $param; unsafe fn grow( &mut self, @@ -138,12 +145,12 @@ fn miri() { for _ in 0..10 { mem.grow_filled(GROW, val.clone())?; } - assert!(mem.allocated() == vec![val; GROW * 10]); + assert!(mem[..] == vec![val; GROW * 10]); for _ in 0..10 { mem.shrink(GROW)?; } - assert_eq!(mem.allocated().len(), 0); + assert_eq!(mem.len(), 0); Ok(()) } diff --git a/src/raw_mem.rs b/src/raw_mem.rs index 3fbfd2e..acc9927 100644 --- a/src/raw_mem.rs +++ b/src/raw_mem.rs @@ -1,6 +1,7 @@ use std::{ alloc::Layout, mem::{self, MaybeUninit}, + ops::{Deref, DerefMut}, ptr, }; @@ -67,12 +68,9 @@ impl Drop for Guard<'_, T> { } } -pub trait RawMem { +pub trait RawMem: Deref + DerefMut { type Item; - fn allocated(&self) -> &[Self::Item]; - fn allocated_mut(&mut self) -> &mut [Self::Item]; - /// # Safety /// Caller must guarantee that `fill` makes the uninitialized part valid for /// [`MaybeUninit::slice_assume_init_mut`]