pub struct FileMmap { /* private fields */ }

Implementations§

§

impl FileMmap

pub fn new<P>(path: P) -> Result<FileMmap, Error>
where P: AsRef<Path>,

Opens the file and creates the mmap.

pub fn len(&self) -> u64

Gets the length of the file.

pub unsafe fn bytes(&self, addr: isize, len: usize) -> &[u8]

Gets a byte slice from the mmap’d region.

§Safety

Make sure addr+len does not exceed the size of the file.

pub fn set_len(&mut self, len: u64) -> Result<(), Error>

Sets the length of the file.

pub fn append(&mut self, bytes: &[u8]) -> Result<u64, Error>

Appends a byte slice to the end of the file.

pub fn write(&mut self, addr: isize, bytes: &[u8]) -> Result<(), Error>

Writes a byte slice to the mmap’d region.

Methods from Deref<Target = MmapRaw>§

pub fn as_ptr(&self) -> *const u8

Returns a raw pointer to the memory mapped file.

Before dereferencing this pointer, you have to make sure that the file has not been truncated since the memory map was created. Avoiding this will not introduce memory safety issues in Rust terms, but will cause SIGBUS (or equivalent) signal.

pub fn as_mut_ptr(&self) -> *mut u8

Returns an unsafe mutable pointer to the memory mapped file.

Before dereferencing this pointer, you have to make sure that the file has not been truncated since the memory map was created. Avoiding this will not introduce memory safety issues in Rust terms, but will cause SIGBUS (or equivalent) signal.

pub fn len(&self) -> usize

Returns the length in bytes of the memory map.

Note that truncating the file can cause the length to change (and render this value unusable).

pub fn flush(&self) -> Result<(), Error>

Flushes outstanding memory map modifications to disk.

When this method returns with a non-error result, all outstanding changes to a file-backed memory map are guaranteed to be durably stored. The file’s metadata (including last modification timestamp) may not be updated.

§Example
use std::fs::OpenOptions;
use std::io::Write;
use std::path::PathBuf;
use std::slice;

use memmap2::MmapRaw;

let tempdir = tempfile::tempdir()?;
let path: PathBuf = /* path to file */
let file = OpenOptions::new().read(true).write(true).create(true).open(&path)?;
file.set_len(128)?;

let mut mmap = unsafe { MmapRaw::map_raw(&file)? };

let mut memory = unsafe { slice::from_raw_parts_mut(mmap.as_mut_ptr(), 128) };
memory.write_all(b"Hello, world!")?;
mmap.flush()?;

pub fn flush_async(&self) -> Result<(), Error>

Asynchronously flushes outstanding memory map modifications to disk.

This method initiates flushing modified pages to durable storage, but it will not wait for the operation to complete before returning. The file’s metadata (including last modification timestamp) may not be updated.

pub fn flush_range(&self, offset: usize, len: usize) -> Result<(), Error>

Flushes outstanding memory map modifications in the range to disk.

The offset and length must be in the bounds of the memory map.

When this method returns with a non-error result, all outstanding changes to a file-backed memory in the range are guaranteed to be durable stored. The file’s metadata (including last modification timestamp) may not be updated. It is not guaranteed the only the changes in the specified range are flushed; other outstanding changes to the memory map may be flushed as well.

pub fn flush_async_range(&self, offset: usize, len: usize) -> Result<(), Error>

Asynchronously flushes outstanding memory map modifications in the range to disk.

The offset and length must be in the bounds of the memory map.

This method initiates flushing modified pages to durable storage, but it will not wait for the operation to complete before returning. The file’s metadata (including last modification timestamp) may not be updated. It is not guaranteed that the only changes flushed are those in the specified range; other outstanding changes to the memory map may be flushed as well.

Trait Implementations§

§

impl Deref for FileMmap

§

type Target = MmapRaw

The resulting type after dereferencing.
§

fn deref(&self) -> &<FileMmap as Deref>::Target

Dereferences the value.
§

impl Drop for FileMmap

§

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>,

§

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>,

§

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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V