#[repr(transparent)]
pub struct MmapFile { /* private fields */ }
Expand description

A read-only memory map file.

There is 3 status of this struct:

  • Disk: mmap to a real file
  • Memory: use Bytes to mock a mmap, which is useful for test and in-memory storage engine
  • Empty: a state represents null mmap, which is helpful for drop, close the MmapFile. This state cannot be constructed directly.

Implementations

Available on crate feature sync only.

Open a readable memory map backed by a file

Examples
use fmmap::{MmapFile, MmapFileExt};
use std::fs::{remove_file, File};
use std::io::Write;


// open and mmap the file
let mut file = MmapFile::open("open_test.txt").unwrap();
let mut buf = vec![0; "some data...".len()];
file.read_exact(buf.as_mut_slice(), 0);
assert_eq!(buf.as_slice(), "some data...".as_bytes());
Available on crate feature sync only.

Open a readable memory map backed by a file with Options

Examples
use fmmap::{Options, MmapFile, MmapFileExt};


// mmap the file with options
let opts = Options::new()
    // allow read
    .read(true)
    // allow write
    .write(true)
    // allow append
    .append(true)
    // truncate to 100
    .max_size(100)
    // mmap content after the sanity text
    .offset("sanity text".as_bytes().len() as u64);
// open and mmap the file
let mut file = MmapFile::open_with_options("open_test_with_options.txt", opts).unwrap();
let mut buf = vec![0; "some data...".len()];
file.read_exact(buf.as_mut_slice(), 0);
assert_eq!(buf.as_slice(), "some data...".as_bytes());
Available on crate feature sync only.

Open a readable memory map backed by a file

Examples
use fmmap::{MmapFile, MmapFileExt};
use std::fs::{remove_file, File};
use std::io::Write;


// open and mmap the file
let mut file = MmapFile::open_exec("open_exec_test.txt").unwrap();
let mut buf = vec![0; "some data...".len()];
file.read_exact(buf.as_mut_slice(), 0);
assert_eq!(buf.as_slice(), "some data...".as_bytes());
Available on crate feature sync only.

Open a readable and executable memory map backed by a file with Options.

Examples
use fmmap::{Options, MmapFile, MmapFileExt};


// mmap the file with options
let opts = Options::new()
    // allow read
    .read(true)
    // mmap content after the sanity text
    .offset("sanity text".as_bytes().len() as u64);
// open and mmap the file
let mut file = MmapFile::open_exec_with_options("open_exec_test_with_options.txt", opts).unwrap();
let mut buf = vec![0; "some data...".len()];
file.read_exact(buf.as_mut_slice(), 0);
assert_eq!(buf.as_slice(), "some data...".as_bytes());
Available on crate feature sync only.

Create a in-memory MmapFile

Examples
use bytes::{BufMut, BytesMut};
use fmmap::sync::MmapFile;

let mut data = BytesMut::with_capacity(100);
data.put_slice("some data...".as_bytes());
MmapFile::memory("foo.mem", data.freeze());
Available on crate feature sync only.

Create a in-memory MmapFile from Vec

Examples
use fmmap::sync::MmapFile;

let data = (0..=255u8).collect::<Vec<_>>();
MmapFile::memory_from_vec("foo.mem", data);
Available on crate feature sync only.

Create a in-memory MmapFile from String

Examples
use fmmap::sync::MmapFile;

let data: &'static str = "some data...";
MmapFile::memory_from_string("foo.mem", data.to_string());
Available on crate feature sync only.

Create a in-memory MmapFile from static slice

Examples
use bytes::Bytes;
use fmmap::sync::MmapFile;

let data: &'static [u8] = "some data...".as_bytes();
MmapFile::memory_from_slice("foo.mem", data);
Available on crate feature sync only.

Create a in-memory MmapFile from static str

Examples
use bytes::Bytes;
use fmmap::sync::MmapFile;

let data: &'static str = "some data...";
MmapFile::memory_from_str("foo.mem", data);
Available on crate feature sync only.

Create a in-memory MmapFile by copy from slice

Examples
use fmmap::sync::MmapFile;

MmapFile::memory_copy_from_slice("foo.mem", "some data...".as_bytes());

Trait Implementations

Converts to this type from the input type.

Converts to this type from the input type.

Returns the current mmap length

Returns the underlying slice of the mmap

Returns the path of the inner file.

Whether the mmap is executable.

Returns the metadata of file metadata Read more

Locks the file for shared usage, blocking if the file is currently locked exclusively. Read more

Locks the file for exclusive usage, blocking if the file is currently locked. Read more

Locks the file for shared usage, or returns a an error if the file is currently locked (see lock_contended_error). Read more

Locks the file for shared usage, or returns a an error if the file is currently locked (see lock_contended_error).Locks the file for shared usage, or returns a an error if the file is currently locked (see lock_contended_error). Read more

Unlocks the file. Read more

Returns the mmap is empty of not.

slice returns data starting from offset off of size sz. Read more

bytes returns data starting from offset off of size sz. Read more

Returns the path buf of the inner file.

Returns the path lossy string of the inner file.

Returns the path string of the inner file.

Copy the content of the mmap file to Vec

Copy a range of content of the mmap file to Vec

Write the content of the mmap file to a new file.

Write a range of content of the mmap file to new file.

Returns a MmapFileReader which helps read data from mmap like a normal File. Read more

Returns a MmapFileReader base on the given offset and len, which helps read data from mmap like a normal File. Read more

Read bytes to the dst buf from the offset, returns how many bytes read.

Read the exact number of bytes required to fill buf.

Read a signed 8 bit integer from offset.

Read a signed 16 bit integer from offset in big-endian byte order.

Read a signed 16 bit integer from offset in little-endian byte order.

Read a signed integer from offset in big-endian byte order.

Read a signed integer from offset in little-endian byte order.

Read a signed 32 bit integer from offset in big-endian byte order.

Read a signed 32 bit integer from offset in little-endian byte order.

Read a signed 64 bit integer from offset in big-endian byte order.

Read a signed 64 bit integer from offset in little-endian byte order.

Read a signed 128 bit integer from offset in big-endian byte order.

Read a signed 128 bit integer from offset in little-endian byte order.

Read an unsigned 8 bit integer from offset.

Read an unsigned 16 bit integer from offset in big-endian.

Read an unsigned 16 bit integer from offset in little-endian.

Read an unsigned integer from offset in big-endian byte order.

Read an unsigned integer from offset in little-endian byte order.

Read an unsigned 32 bit integer from offset in big-endian.

Read an unsigned 32 bit integer from offset in little-endian.

Read an unsigned 64 bit integer from offset in big-endian.

Read an unsigned 64 bit integer from offset in little-endian.

Read an unsigned 128 bit integer from offset in big-endian.

Read an unsigned 128 bit integer from offset in little-endian.

Read an IEEE754 single-precision (4 bytes) floating point number from offset in big-endian byte order. Read more

Read an IEEE754 single-precision (4 bytes) floating point number from offset in little-endian byte order. Read more

Read an IEEE754 single-precision (8 bytes) floating point number from offset in big-endian byte order. Read more

Read an IEEE754 single-precision (8 bytes) floating point number from offset in little-endian byte order. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.