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
impl AsyncMmapFile
Sourcepub async fn open<P: AsRef<Path>>(path: P) -> Result<Self>
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());
Sourcepub async fn open_with_options<P: AsRef<Path>>(
path: P,
opts: AsyncOptions,
) -> Result<Self>
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());
Sourcepub async fn open_exec<P: AsRef<Path>>(path: P) -> Result<Self>
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());
Sourcepub async fn open_exec_with_options<P: AsRef<Path>>(
path: P,
opts: AsyncOptions,
) -> Result<Self>
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
impl AsyncMmapFile
Sourcepub fn memory_from_string<P: AsRef<Path>>(path: P, src: String) -> Self
pub fn memory_from_string<P: AsRef<Path>>(path: P, src: String) -> Self
Sourcepub fn memory_from_slice<P: AsRef<Path>>(path: P, src: &'static [u8]) -> Self
pub fn memory_from_slice<P: AsRef<Path>>(path: P, src: &'static [u8]) -> Self
Sourcepub fn memory_from_str<P: AsRef<Path>>(path: P, src: &'static str) -> Self
pub fn memory_from_str<P: AsRef<Path>>(path: P, src: &'static str) -> Self
Trait Implementations§
Source§impl AsyncMmapFileExt for AsyncMmapFile
impl AsyncMmapFileExt for AsyncMmapFile
Source§fn lock_exclusive(&self) -> Result<()>
fn lock_exclusive(&self) -> Result<()>
Locks the file for exclusive usage, blocking if the file is currently locked. Read more
Locks the file for shared usage, blocking if the file is currently locked exclusively. Read more
Source§fn try_lock_exclusive(&self) -> Result<()>
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
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 slice(&self, offset: usize, sz: usize) -> &[u8] ⓘ
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]>
fn bytes(&self, offset: usize, sz: usize) -> Result<&[u8]>
bytes returns data starting from offset off of size sz. Read more
Source§fn path_lossy(&self) -> Cow<'_, str>
fn path_lossy(&self) -> Cow<'_, str>
Returns the path lossy string of the inner file.
Source§fn path_string(&self) -> String
fn path_string(&self) -> String
Returns the path string of the inner file.
Source§fn copy_range_to_vec(&self, offset: usize, len: usize) -> Vec<u8> ⓘ
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
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
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<'_>>
fn reader(&self, offset: usize) -> Result<AsyncMmapFileReader<'_>>
Returns a
AsyncMmapFileReader
which helps read data from mmap like a normal File. Read moreSource§fn range_reader(
&self,
offset: usize,
len: usize,
) -> Result<AsyncMmapFileReader<'_>>
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 moreSource§fn read(&self, dst: &mut [u8], offset: usize) -> usize
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<()>
fn read_exact(&self, dst: &mut [u8], offset: usize) -> Result<()>
Read the exact number of bytes required to fill buf.
Source§fn read_i16(&self, offset: usize) -> Result<i16>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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_u16(&self, offset: usize) -> Result<u16>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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§impl From<AsyncDiskMmapFile> for AsyncMmapFile
impl From<AsyncDiskMmapFile> for AsyncMmapFile
Source§fn from(file: AsyncDiskMmapFile) -> Self
fn from(file: AsyncDiskMmapFile) -> Self
Converts to this type from the input type.
Source§impl From<AsyncMemoryMmapFile> for AsyncMmapFile
impl From<AsyncMemoryMmapFile> for AsyncMmapFile
Source§fn from(file: AsyncMemoryMmapFile) -> Self
fn from(file: AsyncMemoryMmapFile) -> Self
Converts to this type from the input type.
Auto Trait Implementations§
impl !Freeze for AsyncMmapFile
impl !RefUnwindSafe for AsyncMmapFile
impl Send for AsyncMmapFile
impl Sync for AsyncMmapFile
impl Unpin for AsyncMmapFile
impl !UnwindSafe for AsyncMmapFile
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more