Struct AsyncMmapFile

Source
pub struct AsyncMmapFile { /* private fields */ }
Available on crate feature async-std only.
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 AsyncMmapFile. This state cannot be constructed directly.

Implementations§

Source§

impl AsyncMmapFile

Source

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

Open a readable memory map backed by a file

§Examples
use fmmap::async_std::{AsyncMmapFile, AsyncMmapFileExt};

 // mmap the file
let mut file = AsyncMmapFile::open("async_std_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());
Source

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

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

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


 // mmap the file
 let opts = AsyncOptions::new()
     // mmap content after the sanity text
     .offset("sanity text".as_bytes().len() as u64);
 // mmap the file
let mut file = AsyncMmapFile::open_with_options("async_std_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());
Source

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

Open a readable and executable memory map backed by a file

§Examples
use fmmap::async_std::{AsyncMmapFile, AsyncMmapFileExt};

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

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

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


 // mmap the file
 let opts = AsyncOptions::new()
     // mmap content after the sanity text
     .offset("sanity text".as_bytes().len() as u64);
 // mmap the file
let mut file = AsyncMmapFile::open_exec_with_options("async_std_async_open_exec_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());
Source§

impl AsyncMmapFile

Source

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

Create a in-memory AsyncMmapFile

§Examples
use bytes::{BufMut, BytesMut};
use fmmap::async_std::AsyncMmapFile;

let mut data = BytesMut::with_capacity(100);
data.put_slice("some data...".as_bytes());
AsyncMmapFile::memory("foo.mem", data.freeze());
Source

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

Create a in-memory AsyncMmapFile from Vec

§Examples
use fmmap::async_std::AsyncMmapFile;

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

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

Create a in-memory AsyncMmapFile from String

§Examples
use fmmap::async_std::AsyncMmapFile;

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

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

Create a in-memory AsyncMmapFile from static slice

§Examples
use bytes::Bytes;
use fmmap::async_std::AsyncMmapFile;

let data: &'static [u8] = "some data...".as_bytes();
AsyncMmapFile::memory_from_slice("foo.mem", data);
Source

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

Create a in-memory AsyncMmapFile from static str

§Examples
use bytes::Bytes;
use fmmap::async_std::AsyncMmapFile;

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

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

Create a in-memory AsyncMmapFile by copy from slice

§Examples
use fmmap::async_std::AsyncMmapFile;

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

Trait Implementations§

Source§

impl AsyncMmapFileExt for AsyncMmapFile

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 From<AsyncDiskMmapFile> for AsyncMmapFile

Source§

fn from(file: AsyncDiskMmapFile) -> Self

Converts to this type from the input type.
Source§

impl From<AsyncMemoryMmapFile> for AsyncMmapFile

Source§

fn from(file: AsyncMemoryMmapFile) -> 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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more