pub struct MemoryMap { /* private fields */ }Expand description
A memory-mapped file as an array of u64.
This interface is highly unsafe.
The file remains open until the MemoryMap is dropped.
Memory-mapped structures should implement the MemoryMapped trait.
§Examples
use simple_sds_sbwt::serialize::{MemoryMap, MappingMode, Serialize};
use simple_sds_sbwt::serialize;
use std::fs;
let v: Vec<u64> = vec![123, 456];
let filename = serialize::temp_file_name("memory-map");
serialize::serialize_to(&v, &filename);
let map = MemoryMap::new(&filename, MappingMode::ReadOnly).unwrap();
assert_eq!(map.mode(), MappingMode::ReadOnly);
assert_eq!(map.len(), 3);
unsafe {
let slice: &[u64] = map.as_ref();
assert_eq!(slice[0], 2);
assert_eq!(slice[1], 123);
assert_eq!(slice[2], 456);
}
drop(map);
fs::remove_file(&filename).unwrap();Implementations§
Source§impl MemoryMap
impl MemoryMap
Sourcepub fn new<P: AsRef<Path>>(filename: P, mode: MappingMode) -> Result<MemoryMap>
pub fn new<P: AsRef<Path>>(filename: P, mode: MappingMode) -> Result<MemoryMap>
Returns a memory map for the specified file in the given mode.
§Arguments
filename: Name of the file.mode: Memory mapping mode.
§Errors
The call may fail for a number of reasons, including:
- File
filenamedoes not exist. - The file cannot be opened for writing with mode
MappingMode::Mutable. - The size of the file is not a multiple of 8 bytes.
- Memory mapping the file fails.
Sourcepub fn mode(&self) -> MappingMode
pub fn mode(&self) -> MappingMode
Returns the memory mapping mode for the file.
Sourcepub unsafe fn as_mut_slice(&mut self) -> &mut [u64]
pub unsafe fn as_mut_slice(&mut self) -> &mut [u64]
Returns a mutable slice corresponding to the file.
§Safety
Behavior is undefined if the file was opened with mode MappingMode::ReadOnly.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for MemoryMap
impl RefUnwindSafe for MemoryMap
impl !Send for MemoryMap
impl !Sync for MemoryMap
impl Unpin for MemoryMap
impl UnwindSafe for MemoryMap
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more