[][src]Struct memmap::Mmap

pub struct Mmap { /* fields omitted */ }

An immutable memory mapped buffer.

A Mmap may be backed by a file, or it can be anonymous map, backed by volatile memory.

Use MmapOptions to configure and create a file-backed memory map. To create an immutable anonymous memory map, first create a mutable anonymous memory map using MmapOptions, and then make it immutable with MmapMut::make_read_only.

Example

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

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

See MmapMut for the mutable version.

Methods

impl Mmap[src]

pub unsafe fn map(file: &File) -> Result<Mmap>[src]

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

This is equivalent to calling MmapOptions::new().map(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 std::fs::File;
use std::io::Read;

use memmap::Mmap;

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

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

let mmap = unsafe { Mmap::map(&file)?  };

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

pub fn make_mut(self) -> Result<MmapMut>[src]

Transition the memory map to be writable.

If the memory map is file-backed, the file must have been opened with write permissions.

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::Mmap;
use std::ops::DerefMut;
use std::io::Write;

let file = /* file opened with write permissions */
let mmap = unsafe { Mmap::map(&file)? };
// ... use the read-only memory map ...
let mut mut_mmap = mmap.make_mut()?;
mut_mmap.deref_mut().write_all(b"hello, world!")?;

Trait Implementations

impl AsRef<[u8]> for Mmap[src]

impl Deref for Mmap[src]

type Target = [u8]

The resulting type after dereferencing.

impl Debug for Mmap[src]

Auto Trait Implementations

impl Unpin for Mmap

impl Sync for Mmap

impl Send for Mmap

impl UnwindSafe for Mmap

impl RefUnwindSafe for Mmap

Blanket Implementations

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]