AsyncOptions

Struct AsyncOptions 

Source
pub struct AsyncOptions { /* private fields */ }
Available on crate feature smol only.
Expand description

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

Implementations§

Source§

impl AsyncOptions

Source

pub fn new() -> Self

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

Source

pub fn offset(self, offset: u64) -> 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.

Source

pub fn len(self, len: usize) -> 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.

Source

pub fn populate(self) -> Self

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

Source

pub fn stack(self) -> 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. It has no effect on Windows. This option has no effect on file-backed memory maps

Source

pub fn max_size(self, max_sz: u64) -> Self

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.

Source

pub fn read(self, val: bool) -> Self

Sets the option for read access. For details, please see std::fs::OpenOptions::read

Source

pub fn write(self, val: bool) -> Self

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.

Source

pub fn create(self, val: bool) -> Self

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.

Source

pub fn create_new(self, val: bool) -> Self

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.

Source

pub fn append(self, val: bool) -> Self

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.

Source

pub fn truncate(self, val: bool) -> Self

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.

Source§

impl AsyncOptions

Source

pub async fn create_mmap_file_mut<P: AsRef<Path>>( self, path: P, ) -> Result<AsyncMmapFileMut, Error>

Create a new file and mmap this file with AsyncOptions

§Example
use fmmap::smol::{AsyncOptions, AsyncMmapFileMut, AsyncMmapFileExt, AsyncMmapFileMutExt};

 let mut file = AsyncOptions::new()
     // truncate to 100
     .max_size(100)
.create_mmap_file_mut("smol_async_create_with_options_test.txt").await.unwrap();
 assert!(!file.is_empty());
 file.write_all("some data...".as_bytes(), 0).unwrap();
 file.flush().unwrap();
Source

pub async fn open_mmap_file<P: AsRef<Path>>( self, path: P, ) -> Result<AsyncMmapFile, Error>

Open a readable memory map backed by a file with [Options]

§Example
use fmmap::smol::{AsyncOptions, AsyncMmapFile, AsyncMmapFileExt};


 // mmap the file
 let file = AsyncOptions::new()
     // mmap content after the sanity text
     .offset("sanity text".as_bytes().len() as u64)
.open_mmap_file("smol_async_open_with_options_test.txt").await.unwrap();
 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());
Source

pub async fn open_exec_mmap_file<P: AsRef<Path>>( self, path: P, ) -> Result<AsyncMmapFile, Error>

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

§Examples
use fmmap::smol::{AsyncOptions, AsyncMmapFile, AsyncMmapFileExt};


 // mmap the file
 let file = AsyncOptions::new()
     // mmap content after the sanity text
     .offset("sanity text".as_bytes().len() as u64)
.open_exec_mmap_file("smol_async_open_exec_with_options_test.txt").await.unwrap();
 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());
Source

pub async fn open_mmap_file_mut<P: AsRef<Path>>( self, path: P, ) -> Result<AsyncMmapFileMut, Error>

Open or Create(if not exists) a file and mmap this file with AsyncOptions.

§Examples

File already exists

use fmmap::smol::{AsyncMmapFileMut, AsyncMmapFileExt, AsyncMmapFileMutExt, AsyncOptions};


let mut file = AsyncOptions::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("smol_async_open_with_options_test.txt").await.unwrap();
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.truncate(("some modified data...".len() + "sanity text".len()) as u64).await.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 = AsyncMmapFileMut::open("smol_async_open_with_options_test.txt").await.unwrap();
// skip the sanity text
file.read_exact(buf.as_mut_slice(), "sanity text".as_bytes().len()).unwrap();
assert_eq!(buf.as_slice(), "some modified data...".as_bytes());

File does not exists

use fmmap::smol::{AsyncMmapFileMut, AsyncMmapFileExt, AsyncMmapFileMutExt, AsyncOptions};

// mmap the file with options
let mut file = AsyncOptions::new()
    // allow read
    .read(true)
    // allow write
    .write(true)
    // allow append
    .append(true)
    // truncate to 100
    .max_size(100)
.open_mmap_file_mut("smol_async_open_with_options_test.txt").await.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).unwrap();
assert_eq!(buf.as_slice(), "some data...".as_bytes());

// modify the file data
file.truncate("some modified data...".len() as u64).await.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 = AsyncMmapFileMut::open("smol_async_open_with_options_test.txt").await.unwrap();
file.read_exact(buf.as_mut_slice(), 0).unwrap();
assert_eq!(buf.as_slice(), "some modified data...".as_bytes());
Source

pub async fn open_exist_mmap_file_mut<P: AsRef<Path>>( self, path: P, ) -> Result<AsyncMmapFileMut, Error>

Open an existing file and mmap this file with AsyncOptions

§Example
use fmmap::smol::{AsyncMmapFileMut, AsyncMmapFileExt, AsyncMmapFileMutExt, AsyncOptions};

// create a temp file
let mut file = AsyncMmapFileMut::create("smol_async_open_existing_test_with_options.txt").await.unwrap();
file.truncate(23).await.unwrap();
file.write_all("sanity text".as_bytes(), 0).unwrap();
file.write_all("some data...".as_bytes(), "sanity text".as_bytes().len()).unwrap();
file.flush().unwrap();
drop(file);

// mmap the file
let mut file = AsyncOptions::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("smol_async_open_existing_test_with_options.txt").await.unwrap();

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.truncate(("some modified data...".len() + "sanity text".len()) as u64).await.unwrap();
file.write_all("some modified data...".as_bytes(), 0).unwrap();
file.flush().unwrap();

// reopen to check content, cow will not change the content.
let mut file = AsyncMmapFileMut::open("smol_async_open_existing_test_with_options.txt").await.unwrap();
let mut buf = vec![0; "some modified data...".len()];
// skip the sanity text
file.read_exact(buf.as_mut_slice(), "sanity text".as_bytes().len()).unwrap();
assert_eq!(buf.as_slice(), "some modified data...".as_bytes());
Source

pub async fn open_cow_mmap_file_mut<P: AsRef<Path>>( self, path: P, ) -> Result<AsyncMmapFileMut, Error>

Open and mmap an existing file in copy-on-write mode(copy-on-write memory map backed by a file) with AsyncOptions. 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::smol::{AsyncMmapFileMut, AsyncMmapFileExt, AsyncMmapFileMutExt, AsyncOptions};

 // create a temp file
let mut file = AsyncMmapFileMut::create("smol_async_open_cow_with_options_test.txt").await.unwrap();
 file.truncate(23).await.unwrap();
 file.write_all("sanity text".as_bytes(), 0).unwrap();
 file.write_all("some data...".as_bytes(), "sanity text".as_bytes().len()).unwrap();
 file.flush().unwrap();
 drop(file);

 // mmap the file
 let mut file = AsyncOptions::new()
     // mmap content after the sanity text
     .offset("sanity text".as_bytes().len() as u64)
.open_cow_mmap_file_mut("smol_async_open_cow_with_options_test.txt").await.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 = AsyncMmapFileMut::open("smol_async_open_cow_with_options_test.txt").await.unwrap();
 let mut buf = vec![0; "some data...".len()];
 // skip the sanity text
 file.read_exact(buf.as_mut_slice(), "sanity text".as_bytes().len()).unwrap();
 assert_eq!(buf.as_slice(), "some data...".as_bytes());
Source§

impl AsyncOptions

Source

pub fn mode(self, mode: u32) -> Self

Sets the mode bits that a new file will be created with. Read more

Source

pub fn custom_flags(self, flags: i32) -> Self

Pass custom flags to the flags argument of open. Read more

Trait Implementations§

Source§

impl Clone for AsyncOptions

Source§

fn clone(&self) -> AsyncOptions

Returns a duplicate 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 Default for AsyncOptions

Source§

fn default() -> Self

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, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoResult<T> for T

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.