From 13980b8cd2be2f42b9323aff1a85eac553f1e395 Mon Sep 17 00:00:00 2001 From: Avril Date: Sat, 26 Dec 2020 20:54:49 +0000 Subject: [PATCH] start map --- src/main.rs | 1 + src/map.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 src/map.rs diff --git a/src/main.rs b/src/main.rs index efaae78..26f2344 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ mod state; mod pool; +mod map; mod job; fn main() { diff --git a/src/map.rs b/src/map.rs new file mode 100644 index 0000000..ce9b8b0 --- /dev/null +++ b/src/map.rs @@ -0,0 +1,61 @@ +use std::ops::RangeBounds; +use std::slice::SliceIndex; +use std::{fs,io}; +use std::path::Path; + +use memmap::MmapMut; + +#[derive(Debug)] +pub struct MemoryMapMut +{ + file: fs::File, + map: MmapMut, +} + +impl AsRef<[u8]> for MemoryMapMut +{ + fn as_ref(&self) -> &[u8] + { + &self.map[..] + } +} + +impl AsMut<[u8]> for MemoryMapMut +{ + fn as_mut(&mut self) -> &mut [u8] + { + &mut self.map[..] + } +} + + +impl MemoryMapMut +{ + #[inline] pub fn as_slice_mut(&mut self) -> &mut [u8] + { + self.as_mut() + } + #[inline] pub fn as_slice(&self) -> &[u8] + { + self.as_ref() + } + pub fn open(from: impl AsRef) -> io::Result + { + let file = fs::OpenOptions::new() + .create(true) + .truncate(true) + .write(true) + .open(from)?; + + Ok(Self{ + map: unsafe{ MmapMut::map_mut(&file)? }, + file, + }) + } +} + +#[inline] pub unsafe fn slice_mut<'a, 'b, T, R: RangeBounds + SliceIndex<[T], Output=[T]>>(what: &'a mut [T], range: R)-> &'b mut [T] +where 'a: 'b +{ + &mut what[range] +}