Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use {
alloc::{Allocator, Layout},
fmt::{self, Debug, Formatter},
mem::{self, MaybeUninit},
ops::{Deref, DerefMut},
ptr,
},
};
Expand All @@ -23,16 +24,22 @@ impl<T, A: Allocator> Alloc<T, A> {
}
}

impl<T, A: Allocator> RawMem for Alloc<T, A> {
type Item = T;
impl<T, A: Allocator> Deref for Alloc<T, A> {
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<T, A: Allocator> DerefMut for Alloc<T, A> {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { self.buf.as_slice_mut() }
}
}

impl<T, A: Allocator> RawMem for Alloc<T, A> {
type Item = T;

unsafe fn grow(
&mut self,
Expand Down
15 changes: 11 additions & 4 deletions src/file_mapped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use {
fs::File,
io,
mem::{self, MaybeUninit},
ops::{Deref, DerefMut},
path::Path,
ptr::{self, NonNull},
},
Expand Down Expand Up @@ -42,16 +43,22 @@ impl<T> FileMapped<T> {
}
}

impl<T> RawMem for FileMapped<T> {
type Item = T;
impl<T> Deref for FileMapped<T> {
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<T> DerefMut for FileMapped<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { self.buf.as_slice_mut() }
}
}

impl<T> RawMem for FileMapped<T> {
type Item = T;

unsafe fn grow(
&mut self,
Expand Down
23 changes: 15 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(())
}
Expand Down
6 changes: 2 additions & 4 deletions src/raw_mem.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
alloc::Layout,
mem::{self, MaybeUninit},
ops::{Deref, DerefMut},
ptr,
};

Expand Down Expand Up @@ -67,12 +68,9 @@ impl<T> Drop for Guard<'_, T> {
}
}

pub trait RawMem {
pub trait RawMem: Deref<Target = [Self::Item]> + 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`]
Expand Down