AsyncMmapFileMut

Struct AsyncMmapFileMut 

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

A writable memory map file.

There is 3 status of this struct:

  • Disk: mmap to a real file
  • Memory: use BytesMut 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, remove, close the AsyncMmapFileMut. This state cannot be constructed directly.

Implementations§

Source§

impl AsyncMmapFileMut

Source

pub async fn create<P: AsRef<Path>>(path: P) -> Result<Self>

Create a new file and mmap this file

§Notes

The new file is zero size, so, before write, you should truncate first. Or you can use create_with_options and set max_size field for AsyncOptions to enable directly write without truncating.

§Examples
use fmmap::tokio::{AsyncMmapFileMut, AsyncMmapFileMutExt};

let mut file = AsyncMmapFileMut::create("tokio_async_create_test.txt").await.unwrap();
 file.truncate(12).await;
 file.write_all("some data...".as_bytes(), 0).unwrap();
 file.flush().unwrap();
Source

pub async fn create_with_options<P: AsRef<Path>>( path: P, opts: AsyncOptions, ) -> Result<Self>

Create a new file and mmap this file with AsyncOptions

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

 let opts = AsyncOptions::new()
     // truncate to 100
     .max_size(100);
let mut file = AsyncMmapFileMut::create_with_options("tokio_async_create_with_options_test.txt", opts).await.unwrap();
 file.write_all("some data...".as_bytes(), 0).unwrap();
 file.flush().unwrap();
Source

pub async fn open<P: AsRef<Path>>(path: P) -> Result<Self>

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

§Notes

If the file does not exist, then the new file will be open in zero size, so before do write, you should truncate first. Or you can use open_with_options and set max_size field for AsyncOptions to enable directly write without truncating.

§Examples

File already exists

use fmmap::tokio::{AsyncMmapFileMut, AsyncMmapFileExt, AsyncMmapFileMutExt};


 // mmap the file
let mut file = AsyncMmapFileMut::open("tokio_async_open_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() 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("tokio_async_open_test.txt").await.unwrap();
 file.read_exact(buf.as_mut_slice(), 0).unwrap();
 assert_eq!(buf.as_slice(), "some modified data...".as_bytes());

File does not exists

use fmmap::tokio::{AsyncMmapFileMut, AsyncMmapFileExt, AsyncMmapFileMutExt};

 // mmap the file
let mut file = AsyncMmapFileMut::open("tokio_async_open_test.txt").await.unwrap();
 file.truncate(100).await.unwrap();
 file.write_all("some data...".as_bytes(), 0).unwrap();
 file.flush().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("tokio_async_open_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_with_options<P: AsRef<Path>>( path: P, opts: AsyncOptions, ) -> Result<Self>

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

§Examples

File already exists

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


// mmap the file
let opts = 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);
let mut file = AsyncMmapFileMut::open_with_options("tokio_async_open_with_options_test.txt", opts).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("tokio_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::tokio::{AsyncMmapFileMut, AsyncMmapFileExt, AsyncMmapFileMutExt, AsyncOptions};

// mmap the file with options
let opts = AsyncOptions::new()
    // allow read
    .read(true)
    // allow write
    .write(true)
    // allow append
    .append(true)
    // truncate to 100
    .max_size(100);

let mut file = AsyncMmapFileMut::open_with_options("tokio_async_open_with_options_test.txt", opts).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("tokio_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<P: AsRef<Path>>(path: P) -> Result<Self>

Open an existing file and mmap this file

§Examples
use fmmap::tokio::{AsyncMmapFileMut, AsyncMmapFileExt, AsyncMmapFileMutExt};

 // create a temp file
let mut file = AsyncMmapFileMut::create("tokio_async_open_existing_test.txt").await.unwrap();
 file.truncate(12).await.unwrap();
 file.write_all("some data...".as_bytes(), 0).unwrap();
 file.flush().unwrap();
 drop(file);

 // mmap the file
let mut file = AsyncMmapFileMut::open_exist("tokio_async_open_existing_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() 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("tokio_async_open_existing_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_with_options<P: AsRef<Path>>( path: P, opts: AsyncOptions, ) -> Result<Self>

Open an existing file and mmap this file with AsyncOptions

§Examples
use fmmap::tokio::{AsyncMmapFileMut, AsyncMmapFileExt, AsyncMmapFileMutExt, AsyncOptions};

// create a temp file
let mut file = AsyncMmapFileMut::create("tokio_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 opts = AsyncOptions::new()
    // truncate to 100
    .max_size(100)
    // mmap content after the sanity text
    .offset("sanity text".as_bytes().len() as u64);

let mut file = AsyncMmapFileMut::open_exist_with_options("tokio_async_open_existing_test_with_options.txt", opts).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("tokio_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<P: AsRef<Path>>(path: P) -> Result<Self>

Open and mmap an existing file in copy-on-write mode(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.

§Examples
use fmmap::tokio::{AsyncMmapFileMut, AsyncMmapFileExt, AsyncMmapFileMutExt};

 // create a temp file
let mut file = AsyncMmapFileMut::create("tokio_async_open_cow_test.txt").await.unwrap();
 file.truncate(12).await.unwrap();
 file.write_all("some data...".as_bytes(), 0).unwrap();
 file.flush().unwrap();
 drop(file);

 // mmap the file
let mut file = AsyncMmapFileMut::open_cow("tokio_async_open_cow_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.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("tokio_async_open_cow_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_cow_with_options<P: AsRef<Path>>( path: P, opts: AsyncOptions, ) -> Result<Self>

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::tokio::{AsyncMmapFileMut, AsyncMmapFileExt, AsyncMmapFileMutExt, AsyncOptions};
 use std::io::SeekFrom;

 // create a temp file
let mut file = AsyncMmapFileMut::create("tokio_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 opts = AsyncOptions::new()
     // mmap content after the sanity text
     .offset("sanity text".as_bytes().len() as u64);

let mut file = AsyncMmapFileMut::open_cow_with_options("tokio_async_open_cow_with_options_test.txt", opts).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.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("tokio_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

pub fn freeze(self) -> Result<AsyncMmapFile>

Make the mmap file read-only.

§Notes

If remove_on_drop is set to true, then the underlying file will not be removed on drop if this function is invoked. Read more

Returns an immutable version of this memory mapped buffer. If the memory map is file-backed, the file must have been opened with read 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 has not been opened with read permissions.

§Examples
use fmmap::tokio::{AsyncMmapFileMut, AsyncMmapFileMutExt};

let mut file = AsyncMmapFileMut::create("tokio_async_freeze_test.txt").await.unwrap();
 file.truncate(12).await;
 file.write_all("some data...".as_bytes(), 0).unwrap();
 file.flush().unwrap();
 // freeze
 file.freeze().unwrap();
Source

pub fn freeze_exec(self) -> Result<AsyncMmapFile>

Transition the memory map to be readable and executable. If the memory map is file-backed, the file must have been opened with execute permissions.

§Notes

If remove_on_drop is set to true, then the underlying file will not be removed on drop if this function is invoked. Read more

§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 has not been opened with execute permissions

§Examples
use fmmap::tokio::{AsyncMmapFileMut, AsyncMmapFileMutExt};

let mut file = AsyncMmapFileMut::create("tokio_async_freeze_exec_test.txt").await.unwrap();
 file.truncate(12).await;
 file.write_all("some data...".as_bytes(), 0).unwrap();
 file.flush().unwrap();
 // freeze_exec
 file.freeze_exec().unwrap();
Source

pub fn get_remove_on_drop(&self) -> bool

Returns whether remove the underlying file on drop.

Source

pub fn set_remove_on_drop(&mut self, val: bool)

Whether remove the underlying file on drop. Default is false.

§Notes

If invoke AsyncMmapFileMut::freeze, then the file will not be removed even though the field remove_on_drop is true.

Source

pub async fn close(&mut self, max_sz: i64) -> Result<()>

Close the file. It would also truncate the file if max_sz >= 0.

Source

pub async fn remove(&mut self) -> Result<()>

Remove the underlying file without dropping, leaving an [AsyncEmptyMmapFile].

Source§

impl AsyncMmapFileMut

Source

pub fn memory<P: AsRef<Path>>(path: P) -> Self

Create a in-memory AsyncMmapFileMut

§Examples
use fmmap::tokio::AsyncMmapFileMut;

AsyncMmapFileMut::memory("foo.mem");
Source

pub fn memory_with_capacity<P: AsRef<Path>>(path: P, cap: usize) -> Self

Create a in-memory AsyncMmapFileMutwith capacity

§Examples
use fmmap::tokio::AsyncMmapFileMut;

AsyncMmapFileMut::memory_with_capacity("foo.mem", 1000);
Source

pub fn memory_from_vec<P: AsRef<Path>>(path: P, src: Vec<u8>) -> Self

Create a in-memory AsyncMmapFileMut from Vec

§Examples
use fmmap::tokio::AsyncMmapFileMut;

let data = (0..=255u8).collect::<Vec<_>>();
AsyncMmapFileMut::memory_from_vec("foo.mem", data);
Source

pub fn memory_from_string<P: AsRef<Path>>(path: P, src: String) -> Self

Create a in-memory AsyncMmapFileMut from String

§Examples
use fmmap::tokio::AsyncMmapFileMut;

let data: &'static str = "some data...";
AsyncMmapFileMut::memory_from_string("foo.mem", data.to_string());
Source

pub fn memory_from_str<P: AsRef<Path>>(path: P, src: &'static str) -> Self

Create a in-memory AsyncMmapFileMut from static str

§Examples
use bytes::Bytes;
use fmmap::tokio::AsyncMmapFileMut;

let data: &'static str = "some data...";
AsyncMmapFileMut::memory_from_str("foo.mem", data);
Source

pub fn memory_from_slice<P: AsRef<Path>>(path: P, src: &[u8]) -> Self

Create a in-memory AsyncMmapFileMut by from slice

§Examples
use fmmap::tokio::AsyncMmapFileMut;

AsyncMmapFileMut::memory_from_slice("foo.mem", "some data...".as_bytes());

Trait Implementations§

Source§

impl AsyncMmapFileExt for AsyncMmapFileMut

Source§

fn len(&self) -> usize

Returns the current mmap length
Source§

fn as_slice(&self) -> &[u8]

Returns the underlying slice of the mmap
Source§

fn path(&self) -> &Path

Returns the path of the inner file.
Source§

fn is_exec(&self) -> bool

Whether the mmap is executable
Source§

async fn metadata(&self) -> Result<MetaData>

Returns the metadata of file metadata Read more
Source§

fn lock_exclusive(&self) -> Result<()>

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

fn lock_shared(&self) -> Result<()>

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

fn try_lock_exclusive(&self) -> Result<()>

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

fn try_lock_shared(&self) -> Result<()>

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

fn unlock(&self) -> Result<()>

Unlocks the file. Read more
Source§

fn is_empty(&self) -> bool

Returns the mmap is empty of not.
Source§

fn slice(&self, offset: usize, sz: usize) -> &[u8]

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

fn bytes(&self, offset: usize, sz: usize) -> Result<&[u8]>

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

fn path_buf(&self) -> PathBuf

Returns the path buf of the inner file.
Source§

fn path_lossy(&self) -> Cow<'_, str>

Returns the path lossy string of the inner file.
Source§

fn path_string(&self) -> String

Returns the path string of the inner file.
Source§

fn copy_all_to_vec(&self) -> Vec<u8>

Copy the content of the mmap file to Vec
Source§

fn copy_range_to_vec(&self, offset: usize, len: usize) -> Vec<u8>

Copy a range of content of the mmap file to Vec
Source§

fn write_all_to_new_file<P: AsRef<Path> + Send + Sync>( &self, new_file_path: P, ) -> impl Future<Output = Result<()>> + Send

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

fn write_range_to_new_file<P: AsRef<Path> + Send + Sync>( &self, new_file_path: P, offset: usize, len: usize, ) -> impl Future<Output = Result<()>> + Send

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

fn reader(&self, offset: usize) -> Result<AsyncMmapFileReader<'_>>

Returns a AsyncMmapFileReader which helps read data from mmap like a normal File. Read more
Source§

fn range_reader( &self, offset: usize, len: usize, ) -> Result<AsyncMmapFileReader<'_>>

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

fn read(&self, dst: &mut [u8], offset: usize) -> usize

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

fn read_exact(&self, dst: &mut [u8], offset: usize) -> Result<()>

Read the exact number of bytes required to fill buf.
Source§

fn read_i8(&self, offset: usize) -> Result<i8>

Read a signed 8 bit integer from offset.
Source§

fn read_i16(&self, offset: usize) -> Result<i16>

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

fn read_i16_le(&self, offset: usize) -> Result<i16>

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

fn read_isize(&self, offset: usize) -> Result<isize>

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

fn read_isize_le(&self, offset: usize) -> Result<isize>

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

fn read_i32(&self, offset: usize) -> Result<i32>

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

fn read_i32_le(&self, offset: usize) -> Result<i32>

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

fn read_i64(&self, offset: usize) -> Result<i64>

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

fn read_i64_le(&self, offset: usize) -> Result<i64>

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

fn read_i128(&self, offset: usize) -> Result<i128>

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

fn read_i128_le(&self, offset: usize) -> Result<i128>

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

fn read_u8(&self, offset: usize) -> Result<u8>

Read an unsigned 8 bit integer from offset.
Source§

fn read_u16(&self, offset: usize) -> Result<u16>

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

fn read_u16_le(&self, offset: usize) -> Result<u16>

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

fn read_usize(&self, offset: usize) -> Result<usize>

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

fn read_usize_le(&self, offset: usize) -> Result<usize>

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

fn read_u32(&self, offset: usize) -> Result<u32>

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

fn read_u32_le(&self, offset: usize) -> Result<u32>

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

fn read_u64(&self, offset: usize) -> Result<u64>

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

fn read_u64_le(&self, offset: usize) -> Result<u64>

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

fn read_u128(&self, offset: usize) -> Result<u128>

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

fn read_u128_le(&self, offset: usize) -> Result<u128>

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

fn read_f32(&self, offset: usize) -> Result<f32>

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

fn read_f32_le(&self, offset: usize) -> Result<f32>

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

fn read_f64(&self, offset: usize) -> Result<f64>

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

fn read_f64_le(&self, offset: usize) -> Result<f64>

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

impl AsyncMmapFileMutExt for AsyncMmapFileMut

Source§

async fn drop_remove(self) -> Result<()>

Remove the underlying file

§Example
use fmmap::tokio::{AsyncMmapFileMut, AsyncMmapFileMutExt};
use tokio::fs::File;
let mut file = AsyncMmapFileMut::create("tokio_async_remove_test.txt").await.unwrap();

file.truncate(12).await;
file.write_all("some data...".as_bytes(), 0).unwrap();
file.flush().unwrap();

file.drop_remove().await.unwrap();

let err = File::open("tokio_async_remove_test.txt").await;
assert_eq!(err.unwrap_err().kind(), std::io::ErrorKind::NotFound);
Source§

async fn close_with_truncate(self, max_sz: i64) -> Result<()>

Close and truncate the underlying file

§Examples
use fmmap::{MetaDataExt,tokio::{AsyncMmapFileMut, AsyncMmapFileExt, AsyncMmapFileMutExt}};

let mut file = AsyncMmapFileMut::create("tokio_async_close_with_truncate_test.txt").await.unwrap();
file.truncate(100).await;
file.write_all("some data...".as_bytes(), 0).unwrap();
file.flush().unwrap();

file.close_with_truncate(50).await.unwrap();

let file = AsyncMmapFileMut::open("tokio_async_close_with_truncate_test.txt").await.unwrap();
let meta = file.metadata().await.unwrap();
assert_eq!(meta.len(), 50);
Source§

fn as_mut_slice(&mut self) -> &mut [u8]

Returns the mutable underlying slice of the mmap
Source§

fn is_cow(&self) -> bool

Whether mmap is copy on write
Source§

fn flush(&self) -> Result<()>

Flushes outstanding memory map modifications to disk (if the inner is a real file). Read more
Source§

fn flush_async(&self) -> Result<()>

Asynchronously flushes outstanding memory map modifications to disk(if the inner is a real file). Read more
Source§

fn flush_range(&self, offset: usize, len: usize) -> Result<()>

Flushes outstanding memory map modifications in the range to disk(if the inner is a real file). Read more
Source§

fn flush_async_range(&self, offset: usize, len: usize) -> Result<()>

Asynchronously flushes outstanding memory map modifications in the range to disk(if the inner is a real file). Read more
Source§

async fn truncate(&mut self, max_sz: u64) -> Result<()>

Truncates the file to the max_size, which will lead to do re-mmap and sync_dir if the inner is a real file.
Source§

fn slice_mut(&mut self, offset: usize, sz: usize) -> &mut [u8]

slice_mut returns mutable data starting from offset off of size sz. Read more
Source§

fn bytes_mut(&mut self, offset: usize, sz: usize) -> Result<&mut [u8]>

bytes_mut returns mutable data starting from offset off of size sz. Read more
Source§

fn zero_range(&mut self, start: usize, end: usize)

Fill 0 to the specific range
Source§

fn writer(&mut self, offset: usize) -> Result<AsyncMmapFileWriter<'_>>

Returns a AsyncMmapFileWriter base on the given offset, which helps read or write data from mmap like a normal File. Read more
Source§

fn range_writer( &mut self, offset: usize, len: usize, ) -> Result<AsyncMmapFileWriter<'_>>

Returns a AsyncMmapFileWriter base on the given offset and len, which helps read or write data from mmap like a normal File. Read more
Source§

fn write(&mut self, src: &[u8], offset: usize) -> usize

Write bytes to the mmap from the offset.
Source§

fn write_all(&mut self, src: &[u8], offset: usize) -> Result<()>

Write the all of bytes in src to the mmap from the offset.
Source§

fn write_i8(&mut self, val: i8, offset: usize) -> Result<()>

Writes a signed 8 bit integer to mmap from the offset.
Source§

fn write_i16(&mut self, val: i16, offset: usize) -> Result<()>

Writes a signed 16 bit integer to mmap from the offset in the big-endian byte order.
Source§

fn write_i16_le(&mut self, val: i16, offset: usize) -> Result<()>

Writes a signed 16 bit integer to mmap from the offset in the little-endian byte order.
Source§

fn write_isize(&mut self, val: isize, offset: usize) -> Result<()>

Writes a signed integer to mmap from the offset in the big-endian byte order.
Source§

fn write_isize_le(&mut self, val: isize, offset: usize) -> Result<()>

Writes a signed integer to mmap from the offset in the little-endian byte order.
Source§

fn write_i32(&mut self, val: i32, offset: usize) -> Result<()>

Writes a signed 32 bit integer to mmap from the offset in the big-endian byte order.
Source§

fn write_i32_le(&mut self, val: i32, offset: usize) -> Result<()>

Writes a signed 32 bit integer to mmap from the offset in the little-endian byte order.
Source§

fn write_i64(&mut self, val: i64, offset: usize) -> Result<()>

Writes a signed 64 bit integer to mmap from the offset in the big-endian byte order.
Source§

fn write_i64_le(&mut self, val: i64, offset: usize) -> Result<()>

Writes a signed 64 bit integer to mmap from the offset in the little-endian byte order.
Source§

fn write_i128(&mut self, val: i128, offset: usize) -> Result<()>

Writes a signed 128 bit integer to mmap from the offset in the big-endian byte order.
Source§

fn write_i128_le(&mut self, val: i128, offset: usize) -> Result<()>

Writes a signed 128 bit integer to mmap from the offset in the little-endian byte order.
Source§

fn write_u8(&mut self, val: u8, offset: usize) -> Result<()>

Writes an unsigned 8 bit integer to mmap from the offset.
Source§

fn write_u16(&mut self, val: u16, offset: usize) -> Result<()>

Writes an unsigned 16 bit integer to mmap from the offset in the big-endian byte order.
Source§

fn write_u16_le(&mut self, val: u16, offset: usize) -> Result<()>

Writes an unsigned 16 bit integer to mmap from the offset in the little-endian byte order.
Source§

fn write_usize(&mut self, val: usize, offset: usize) -> Result<()>

Writes an unsigned integer to mmap from the offset in the big-endian byte order.
Source§

fn write_usize_le(&mut self, val: usize, offset: usize) -> Result<()>

Writes an unsigned integer to mmap from the offset in the little-endian byte order.
Source§

fn write_u32(&mut self, val: u32, offset: usize) -> Result<()>

Writes an unsigned 32 bit integer to mmap from the offset in the big-endian byte order.
Source§

fn write_u32_le(&mut self, val: u32, offset: usize) -> Result<()>

Writes an unsigned 32 bit integer to mmap from the offset in the little-endian byte order.
Source§

fn write_u64(&mut self, val: u64, offset: usize) -> Result<()>

Writes an unsigned 64 bit integer to mmap from the offset in the big-endian byte order.
Source§

fn write_u64_le(&mut self, val: u64, offset: usize) -> Result<()>

Writes an unsigned 64 bit integer to mmap from the offset in the little-endian byte order.
Source§

fn write_u128(&mut self, val: u128, offset: usize) -> Result<()>

Writes an unsigned 128 bit integer to mmap from the offset in the big-endian byte order.
Source§

fn write_u128_le(&mut self, val: u128, offset: usize) -> Result<()>

Writes an unsigned 128 bit integer to mmap from the offset in the little-endian byte order.
Source§

fn write_f32(&mut self, val: f32, offset: usize) -> Result<()>

Writes an IEEE754 single-precision (4 bytes) floating point number to mmap from the offset in big-endian byte order.
Source§

fn write_f32_le(&mut self, val: f32, offset: usize) -> Result<()>

Writes an IEEE754 single-precision (4 bytes) floating point number to mmap from the offset in little-endian byte order.
Source§

fn write_f64(&mut self, val: f64, offset: usize) -> Result<()>

Writes an IEEE754 single-precision (8 bytes) floating point number to mmap from the offset in big-endian byte order.
Source§

fn write_f64_le(&mut self, val: f64, offset: usize) -> Result<()>

Writes an IEEE754 single-precision (8 bytes) floating point number to mmap from the offset in little-endian byte order.
Source§

impl Drop for AsyncMmapFileMut

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl From<AsyncDiskMmapFileMut> for AsyncMmapFileMut

Source§

fn from(file: AsyncDiskMmapFileMut) -> Self

Converts to this type from the input type.
Source§

impl From<AsyncMemoryMmapFileMut> for AsyncMmapFileMut

Source§

fn from(file: AsyncMemoryMmapFileMut) -> Self

Converts to this type from the input type.

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> 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, 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.