memmap

Struct MmapOptions

Source
pub struct MmapOptions { /* private fields */ }
Expand description

A memory map builder, providing advanced options and flags for specifying memory map behavior.

MmapOptions can be used to create an anonymous memory map using MmapOptions::map_anon, or a file-backed memory map using one of MmapOptions::map, MmapOptions::map_mut, MmapOptions::map_exec, or MmapOptions::map_copy.

Implementations§

Source§

impl MmapOptions

Source

pub fn new() -> MmapOptions

Creates a new set of options for configuring and creating a memory map.

§Example
use memmap::{MmapMut, MmapOptions};

// Create a new memory map builder.
let mut mmap_options = MmapOptions::new();

// Configure the memory map builder using option setters, then create
// a memory map using one of `mmap_options.map_anon`, `mmap_options.map`,
// `mmap_options.map_mut`, `mmap_options.map_exec`, or `mmap_options.map_copy`:
let mut mmap: MmapMut = mmap_options.len(36).map_anon()?;

// Use the memory map:
mmap.copy_from_slice(b"...data to copy to the memory map...");
Source

pub fn offset(&mut self, offset: u64) -> &mut Self

Configures the memory map to start at byte offset from the beginning of the file.

This option has no effect on anonymous memory maps.

By default, the offset is 0.

§Example
use memmap::MmapOptions;
use std::fs::File;

let mmap = unsafe {
    MmapOptions::new()
                .offset(10)
                .map(&File::open("README.md")?)?
};
assert_eq!(&b"A Rust library for cross-platform memory mapped IO."[..],
           &mmap[..51]);
Source

pub fn len(&mut self, len: usize) -> &mut Self

Configures the created memory mapped buffer to be len bytes long.

This option is mandatory for anonymous memory maps.

For file-backed memory maps, the length will default to the file length.

§Example
use memmap::MmapOptions;
use std::fs::File;

let mmap = unsafe {
    MmapOptions::new()
                .len(8)
                .map(&File::open("README.md")?)?
};
assert_eq!(&b"# memmap"[..], &mmap[..]);
Source

pub fn stack(&mut self) -> &mut Self

Configures the anonymous memory map to be suitable for a process or thread stack.

This option corresponds to the MAP_STACK flag on Linux.

This option has no effect on file-backed memory maps.

§Example
use memmap::MmapOptions;

let stack = MmapOptions::new().stack().len(4096).map_anon();
Source

pub unsafe fn map(&self, file: &File) -> Result<Mmap>

Creates a read-only memory map backed by a file.

§Errors

This method returns an error when the underlying system call fails, which can happen for a variety of reasons, such as when the file is not open with read permissions.

§Example
use memmap::MmapOptions;
use std::fs::File;
use std::io::Read;

let mut file = File::open("README.md")?;

let mut contents = Vec::new();
file.read_to_end(&mut contents)?;

let mmap = unsafe {
    MmapOptions::new().map(&file)?
};

assert_eq!(&contents[..], &mmap[..]);
Source

pub unsafe fn map_exec(&self, file: &File) -> Result<Mmap>

Creates a readable and executable memory map backed by a file.

§Errors

This method returns an error when the underlying system call fails, which can happen for a variety of reasons, such as when the file is not open with read permissions.

Source

pub unsafe fn map_mut(&self, file: &File) -> Result<MmapMut>

Creates a writeable memory map backed by a file.

§Errors

This method returns an error when the underlying system call fails, which can happen for a variety of reasons, such as when the file is not open with read and write permissions.

§Example
use std::fs::OpenOptions;
use std::path::PathBuf;

use memmap::MmapOptions;
let path: PathBuf = /* path to file */
let file = OpenOptions::new().read(true).write(true).create(true).open(&path)?;
file.set_len(13)?;

let mut mmap = unsafe {
    MmapOptions::new().map_mut(&file)?
};

mmap.copy_from_slice(b"Hello, world!");
Source

pub unsafe fn map_copy(&self, file: &File) -> Result<MmapMut>

Creates a copy-on-write memory map backed by a file.

Data written to the memory map will not be visible by other processes, and will not be carried through to the underlying file.

§Errors

This method returns an error when the underlying system call fails, which can happen for a variety of reasons, such as when the file is not open with writable permissions.

§Example
use memmap::MmapOptions;
use std::fs::File;
use std::io::Write;

let file = File::open("README.md")?;
let mut mmap = unsafe { MmapOptions::new().map_copy(&file)? };
(&mut mmap[..]).write_all(b"Hello, world!")?;
Source

pub fn map_anon(&self) -> Result<MmapMut>

Creates an anonymous memory map.

Note: the memory map length must be configured to be greater than 0 before creating an anonymous memory map using MmapOptions::len().

§Errors

This method returns an error when the underlying system call fails.

Trait Implementations§

Source§

impl Clone for MmapOptions

Source§

fn clone(&self) -> MmapOptions

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MmapOptions

Source§

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

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

impl Default for MmapOptions

Source§

fn default() -> MmapOptions

Returns the “default value” for a 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.