pub struct Options { /* private fields */ }Expand description
A memory map builder, providing advanced options and flags for specifying memory map file behavior.
Implementations
sourceimpl Options
impl Options
sourcepub fn new() -> Self
Available on crate feature sync only.
pub fn new() -> Self
sync only.Creates a new set of options for configuring and creating a memory map.
sourcepub fn offset(self, offset: u64) -> Self
Available on crate feature sync only.
pub fn offset(self, offset: u64) -> Self
sync only.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.
sourcepub fn len(self, len: usize) -> Self
Available on crate feature sync only.
pub fn len(self, len: usize) -> Self
sync only.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.
sourcepub fn populate(self) -> Self
Available on crate feature sync only.
pub fn populate(self) -> Self
sync only.Populate (prefault) page tables for a mapping. For a file mapping, this causes read-ahead on the file. This will help to reduce blocking on page faults later. This option corresponds to the MAP_POPULATE flag on Linux. It has no effect on Windows
sourcepub fn stack(self) -> Self
Available on crate feature sync only.
pub fn stack(self) -> Self
sync only.Configures the anonymous memory map to be suitable for a process or thread stack. This option corresponds to the MAP_STACK flag on Linux. It has no effect on Windows. This option has no effect on file-backed memory maps
sourcepub fn max_size(self, max_sz: u64) -> Self
Available on crate feature sync only.
pub fn max_size(self, max_sz: u64) -> Self
sync only.Configures the max size of the file.
This option only has effect when mmaping a real file in write mode.
This field is ignored when opening DiskMmapFile, AsyncDiskMmapFile, MmapFile and AsyncMmapFile.
sourcepub fn read(self, val: bool) -> Self
Available on crate feature sync only.
pub fn read(self, val: bool) -> Self
sync only.Sets the option for read access. For details, please see std::fs::OpenOptions::read
sourcepub fn write(self, val: bool) -> Self
Available on crate feature sync only.
pub fn write(self, val: bool) -> Self
sync only.Sets the option for write access. For details, please see std::fs::OpenOptions::write.
This field is ignored when opening DiskMmapFile, AsyncDiskMmapFile, MmapFile and AsyncMmapFile.
sourcepub fn create(self, val: bool) -> Self
Available on crate feature sync only.
pub fn create(self, val: bool) -> Self
sync only.Sets the option to create a new file, or open it if it already exists. For details, please see std::fs::OpenOptions::create.
This field is ignored when opening DiskMmapFile, AsyncDiskMmapFile, MmapFile and AsyncMmapFile.
sourcepub fn create_new(self, val: bool) -> Self
Available on crate feature sync only.
pub fn create_new(self, val: bool) -> Self
sync only.Sets the option to create a new file, failing if it already exists. For details, please see std::fs::OpenOptions::create_new
This field is ignored when opening DiskMmapFile, AsyncDiskMmapFile, MmapFile and AsyncMmapFile.
sourcepub fn append(self, val: bool) -> Self
Available on crate feature sync only.
pub fn append(self, val: bool) -> Self
sync only.Sets the option for the append mode. For details, please see std::fs::OpenOptions::append
This field is ignored when opening DiskMmapFile, AsyncDiskMmapFile, MmapFile and AsyncMmapFile.
sourcepub fn truncate(self, val: bool) -> Self
Available on crate feature sync only.
pub fn truncate(self, val: bool) -> Self
sync only.Sets the option for truncating a previous file. For details, please see std::fs::OpenOptions::truncate
This field is ignored when opening DiskMmapFile, AsyncDiskMmapFile, MmapFile and AsyncMmapFile.
sourceimpl Options
impl Options
sourcepub fn create_mmap_file_mut<P: AsRef<Path>>(
self,
path: P
) -> Result<MmapFileMut, Error>
Available on crate feature sync only.
pub fn create_mmap_file_mut<P: AsRef<Path>>(
self,
path: P
) -> Result<MmapFileMut, Error>
sync only.Create a new file and mmap this file with Options
Examples
use fmmap::{Options, MmapFileMut, MmapFileMutExt, MmapFileExt};
let mut file = Options::new().max_size(100).create_mmap_file_mut("create_with_options_test.txt").unwrap();
assert!(!file.is_empty());
file.write_all("some data...".as_bytes(), 0).unwrap();
file.flush().unwrap();sourcepub fn open_mmap_file<P: AsRef<Path>>(self, path: P) -> Result<MmapFile, Error>
Available on crate feature sync only.
pub fn open_mmap_file<P: AsRef<Path>>(self, path: P) -> Result<MmapFile, Error>
sync only.Open a readable memory map backed by a file with Options
Examples
use fmmap::{Options, MmapFile, MmapFileExt};
// open and mmap the file
let file = 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_mmap_file("open_test_with_options.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());sourcepub fn open_exec_mmap_file<P: AsRef<Path>>(
self,
path: P
) -> Result<MmapFile, Error>
Available on crate feature sync only.
pub fn open_exec_mmap_file<P: AsRef<Path>>(
self,
path: P
) -> Result<MmapFile, Error>
sync only.Open a readable and executable memory map backed by a file with Options.
Examples
use fmmap::{Options, MmapFile, MmapFileExt};
// open and mmap the file
let file = Options::new()
// allow read
.read(true)
// mmap content after the sanity text
.offset("sanity text".as_bytes().len() as u64)
.open_exec_mmap_file("exec_mmap_file.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());sourcepub fn open_mmap_file_mut<P: AsRef<Path>>(
self,
path: P
) -> Result<MmapFileMut, Error>
Available on crate feature sync only.
pub fn open_mmap_file_mut<P: AsRef<Path>>(
self,
path: P
) -> Result<MmapFileMut, Error>
sync only.Open or Create(if not exists) a file and mmap this file with Options.
Examples
File already exists
use fmmap::{MmapFileMut, MmapFileExt, MmapFileMutExt, Options};
use std::fs::File;
use std::io::{Read, Seek, SeekFrom, Write};
// mmap the file with options
let mut file = 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_mmap_file_mut("mmap_file_mut.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());
// modify the file data
file.truncate(("some modified data...".len() + "sanity text".len()) as u64).unwrap();
file.write_all("some modified data...".as_bytes(), 0).unwrap();
file.flush().unwrap();
drop(file);
// reopen to check content
let mut buf = vec![0; "some modified data...".len()];
let mut file = File::open("mmap_file_mut.txt").unwrap();
file.seek(SeekFrom::Start("sanity text".as_bytes().len() as u64)).unwrap();
file.read_exact(buf.as_mut_slice()).unwrap();
assert_eq!(buf.as_slice(), "some modified data...".as_bytes());File does not exists
use fmmap::{MmapFileMut, MmapFileExt, MmapFileMutExt, Options};
use std::fs::File;
use std::io::{Read, Write};
// mmap the file with options
let mut file = Options::new()
// allow read
.read(true)
// allow write
.write(true)
// allow append
.append(true)
// truncate to 100
.max_size(100)
.open_mmap_file_mut("mmap_file_mut.txt")
.unwrap();
file.write_all("some data...".as_bytes(), 0).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());
// modify the file data
file.truncate("some modified data...".len() as u64).unwrap();
file.write_all("some modified data...".as_bytes(), 0).unwrap();
file.flush().unwrap();
drop(file);
// reopen to check content
let mut buf = vec![0; "some modified data...".len()];
let mut file = File::open("mmap_file_mut.txt").unwrap();
file.read_exact(buf.as_mut_slice()).unwrap();
assert_eq!(buf.as_slice(), "some modified data...".as_bytes());sourcepub fn open_exist_mmap_file_mut<P: AsRef<Path>>(
self,
path: P
) -> Result<MmapFileMut, Error>
Available on crate feature sync only.
pub fn open_exist_mmap_file_mut<P: AsRef<Path>>(
self,
path: P
) -> Result<MmapFileMut, Error>
sync only.Open an existing file and mmap this file with Options
Examples
use fmmap::{MmapFileMut, MmapFileExt, MmapFileMutExt, Options};
use std::fs::File;
use std::io::{Read, Seek, SeekFrom, Write};
// create a temp file
let mut file = File::create("exist_mmap_file_mut.txt").unwrap();
file.write_all("sanity text".as_bytes()).unwrap();
file.write_all("some data...".as_bytes()).unwrap();
drop(file);
// mmap the file with options
let mut file = Options::new()
// truncate to 100
.max_size(100)
// mmap content after the sanity text
.offset("sanity text".as_bytes().len() as u64)
.open_exist_mmap_file_mut("exist_mmap_file_mut.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());
// modify the file data
file.truncate(("some modified data...".len() + "sanity text".len()) as u64).unwrap();
file.write_all("some modified data...".as_bytes(), 0).unwrap();
file.flush().unwrap();
drop(file);
// reopen to check content
let mut buf = vec![0; "some modified data...".len()];
let mut file = File::open("exist_mmap_file_mut.txt").unwrap();
file.seek(SeekFrom::Start("sanity text".as_bytes().len() as u64)).unwrap();
file.read_exact(buf.as_mut_slice()).unwrap();
assert_eq!(buf.as_slice(), "some modified data...".as_bytes());sourcepub fn open_cow_mmap_file_mut<P: AsRef<Path>>(
self,
path: P
) -> Result<MmapFileMut, Error>
Available on crate feature sync only.
pub fn open_cow_mmap_file_mut<P: AsRef<Path>>(
self,
path: P
) -> Result<MmapFileMut, Error>
sync only.Open and mmap an existing file in copy-on-write mode(copy-on-write memory map backed by a file) with Options.
Data written to the memory map will not be visible by other processes, and will not be carried through to the underlying file.
Examples
use fmmap::{MmapFileMut, MmapFileExt, MmapFileMutExt, Options};
use std::fs::File;
use std::io::{Read, Seek, Write, SeekFrom};
// create a temp file
let mut file = File::create("cow_mmap_file_mut.txt").unwrap();
file.write_all("sanity text".as_bytes()).unwrap();
file.write_all("some data...".as_bytes()).unwrap();
drop(file);
// mmap the file with options
let mut file = Options::new()
// mmap content after the sanity text
.offset("sanity text".as_bytes().len() as u64)
.open_cow_mmap_file_mut("cow_mmap_file_mut.txt")
.unwrap();
assert!(file.is_cow());
let mut buf = vec![0; "some data...".len()];
file.read_exact(buf.as_mut_slice(), 0).unwrap();
assert_eq!(buf.as_slice(), "some data...".as_bytes());
// modify the file data
file.write_all("some data!!!".as_bytes(), 0).unwrap();
file.flush().unwrap();
// cow, change will only be seen in current caller
assert_eq!(file.as_slice(), "some data!!!".as_bytes());
drop(file);
// reopen to check content, cow will not change the content.
let mut file = File::open("cow_mmap_file_mut.txt").unwrap();
let mut buf = vec![0; "some data...".len()];
// skip the sanity text
file.seek(SeekFrom::Start("sanity text".as_bytes().len() as u64)).unwrap();
file.read_exact(buf.as_mut_slice()).unwrap();
assert_eq!(buf.as_slice(), "some data...".as_bytes());Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for Options
impl Send for Options
impl Sync for Options
impl Unpin for Options
impl UnwindSafe for Options
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more