MemoryMap

Struct MemoryMap 

Source
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

Source

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 filename does 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.
Source

pub fn filename(&self) -> &Path

Returns the name of the memory mapped file.

Source

pub fn mode(&self) -> MappingMode

Returns the memory mapping mode for the file.

Source

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.

Source

pub fn len(&self) -> usize

Returns the length of the memory-mapped file.

Source

pub fn is_empty(&self) -> bool

Returns true if the file is empty.

Trait Implementations§

Source§

impl AsRef<[u64]> for MemoryMap

Available on non-target_family=wasm only.
Source§

fn as_ref(&self) -> &[u64]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Debug for MemoryMap

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for MemoryMap

Available on non-target_family=wasm only.
Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.