#[repr(transparent)]pub struct MmapFile { /* private fields */ }Expand description
A read-only memory map file.
There is 3 status of this struct:
- Disk: mmap to a real file
- Memory: use
Bytesto 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
MmapFile. This state cannot be constructed directly.
Implementations
sourceimpl MmapFile
impl MmapFile
sourcepub fn open<P: AsRef<Path>>(path: P) -> Result<Self>
Available on crate feature sync only.
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self>
sync only.Open a readable memory map backed by a file
Examples
use fmmap::{MmapFile, MmapFileExt};
use std::fs::{remove_file, File};
use std::io::Write;
// open and mmap the file
let mut file = MmapFile::open("open_test.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_with_options<P: AsRef<Path>>(path: P, opts: Options) -> Result<Self>
Available on crate feature sync only.
pub fn open_with_options<P: AsRef<Path>>(path: P, opts: Options) -> Result<Self>
sync only.Open a readable memory map backed by a file with Options
Examples
use fmmap::{Options, MmapFile, MmapFileExt};
// mmap the file with options
let opts = 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 and mmap the file
let mut file = MmapFile::open_with_options("open_test_with_options.txt", opts).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<P: AsRef<Path>>(path: P) -> Result<Self>
Available on crate feature sync only.
pub fn open_exec<P: AsRef<Path>>(path: P) -> Result<Self>
sync only.Open a readable memory map backed by a file
Examples
use fmmap::{MmapFile, MmapFileExt};
use std::fs::{remove_file, File};
use std::io::Write;
// open and mmap the file
let mut file = MmapFile::open_exec("open_exec_test.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_with_options<P: AsRef<Path>>(
path: P,
opts: Options
) -> Result<Self>
Available on crate feature sync only.
pub fn open_exec_with_options<P: AsRef<Path>>(
path: P,
opts: Options
) -> Result<Self>
sync only.Open a readable and executable memory map backed by a file with Options.
Examples
use fmmap::{Options, MmapFile, MmapFileExt};
// mmap the file with options
let opts = Options::new()
// allow read
.read(true)
// mmap content after the sanity text
.offset("sanity text".as_bytes().len() as u64);
// open and mmap the file
let mut file = MmapFile::open_exec_with_options("open_exec_test_with_options.txt", opts).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());sourceimpl MmapFile
impl MmapFile
sourcepub fn memory<P: AsRef<Path>>(path: P, data: Bytes) -> Self
Available on crate feature sync only.
pub fn memory<P: AsRef<Path>>(path: P, data: Bytes) -> Self
sync only.Create a in-memory MmapFile
Examples
use bytes::{BufMut, BytesMut};
use fmmap::sync::MmapFile;
let mut data = BytesMut::with_capacity(100);
data.put_slice("some data...".as_bytes());
MmapFile::memory("foo.mem", data.freeze());sourcepub fn memory_from_vec<P: AsRef<Path>>(path: P, src: Vec<u8>) -> Self
Available on crate feature sync only.
pub fn memory_from_vec<P: AsRef<Path>>(path: P, src: Vec<u8>) -> Self
sync only.Create a in-memory MmapFile from Vec
Examples
use fmmap::sync::MmapFile;
let data = (0..=255u8).collect::<Vec<_>>();
MmapFile::memory_from_vec("foo.mem", data);sourcepub fn memory_from_string<P: AsRef<Path>>(path: P, src: String) -> Self
Available on crate feature sync only.
pub fn memory_from_string<P: AsRef<Path>>(path: P, src: String) -> Self
sync only.Create a in-memory MmapFile from String
Examples
use fmmap::sync::MmapFile;
let data: &'static str = "some data...";
MmapFile::memory_from_string("foo.mem", data.to_string());sourcepub fn memory_from_slice<P: AsRef<Path>>(path: P, src: &'static [u8]) -> Self
Available on crate feature sync only.
pub fn memory_from_slice<P: AsRef<Path>>(path: P, src: &'static [u8]) -> Self
sync only.Create a in-memory MmapFile from static slice
Examples
use bytes::Bytes;
use fmmap::sync::MmapFile;
let data: &'static [u8] = "some data...".as_bytes();
MmapFile::memory_from_slice("foo.mem", data);sourcepub fn memory_from_str<P: AsRef<Path>>(path: P, src: &'static str) -> Self
Available on crate feature sync only.
pub fn memory_from_str<P: AsRef<Path>>(path: P, src: &'static str) -> Self
sync only.Create a in-memory MmapFile from static str
Examples
use bytes::Bytes;
use fmmap::sync::MmapFile;
let data: &'static str = "some data...";
MmapFile::memory_from_str("foo.mem", data);Trait Implementations
sourceimpl From<DiskMmapFile> for MmapFile
Available on crate feature sync only.
impl From<DiskMmapFile> for MmapFile
sync only.sourcefn from(file: DiskMmapFile) -> Self
fn from(file: DiskMmapFile) -> Self
Converts to this type from the input type.
sourceimpl From<MemoryMmapFile> for MmapFile
Available on crate feature sync only.
impl From<MemoryMmapFile> for MmapFile
sync only.sourcefn from(file: MemoryMmapFile) -> Self
fn from(file: MemoryMmapFile) -> Self
Converts to this type from the input type.
sourceimpl MmapFileExt for MmapFile
Available on crate feature sync only.
impl MmapFileExt for MmapFile
sync only.sourcefn as_slice(&self) -> &[u8]ⓘNotable traits for &[u8]impl<'_> Read for &[u8]impl<'_> Write for &mut [u8]
fn as_slice(&self) -> &[u8]ⓘNotable traits for &[u8]impl<'_> Read for &[u8]impl<'_> Write for &mut [u8]
Returns the underlying slice of the mmap
sourcefn lock_exclusive(&self) -> Result<()>
fn lock_exclusive(&self) -> Result<()>
Locks the file for shared usage, blocking if the file is currently locked exclusively. Read more
Locks the file for exclusive usage, blocking if the file is currently locked. Read more
sourcefn try_lock_exclusive(&self) -> Result<()>
fn try_lock_exclusive(&self) -> Result<()>
Locks the file for shared 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 (see lock_contended_error).Locks the file for shared usage, or returns a an error if the file is currently locked (see lock_contended_error). Read more
sourcefn slice(&self, offset: usize, sz: usize) -> &[u8]ⓘNotable traits for &[u8]impl<'_> Read for &[u8]impl<'_> Write for &mut [u8]
fn slice(&self, offset: usize, sz: usize) -> &[u8]ⓘNotable traits for &[u8]impl<'_> Read for &[u8]impl<'_> Write for &mut [u8]
slice returns data starting from offset off of size sz. Read more
sourcefn 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
sourcefn path_lossy(&self) -> Cow<'_, str>
fn path_lossy(&self) -> Cow<'_, str>
Returns the path lossy string of the inner file.
sourcefn path_string(&self) -> String
fn path_string(&self) -> String
Returns the path string of the inner file.
sourcefn copy_all_to_vec(&self) -> Vec<u8>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
fn copy_all_to_vec(&self) -> Vec<u8>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
A: Allocator,
Copy the content of the mmap file to Vec
sourcefn copy_range_to_vec(&self, offset: usize, len: usize) -> Vec<u8>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
fn copy_range_to_vec(&self, offset: usize, len: usize) -> Vec<u8>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
A: Allocator,
Copy a range of content of the mmap file to Vec
sourcefn write_all_to_new_file<P: AsRef<Path>>(&self, new_file_path: P) -> Result<()>
fn write_all_to_new_file<P: AsRef<Path>>(&self, new_file_path: P) -> Result<()>
Write the content of the mmap file to a new file.
sourcefn write_range_to_new_file<P: AsRef<Path>>(
&self,
new_file_path: P,
offset: usize,
len: usize
) -> Result<()>
fn write_range_to_new_file<P: AsRef<Path>>(
&self,
new_file_path: P,
offset: usize,
len: usize
) -> Result<()>
Write a range of content of the mmap file to new file.
sourcefn reader(&self, offset: usize) -> Result<MmapFileReader<'_>>
fn reader(&self, offset: usize) -> Result<MmapFileReader<'_>>
Returns a MmapFileReader which helps read data from mmap like a normal File. Read more
sourcefn range_reader(&self, offset: usize, len: usize) -> Result<MmapFileReader<'_>>
fn range_reader(&self, offset: usize, len: usize) -> Result<MmapFileReader<'_>>
Returns a MmapFileReader base on the given offset and len, which helps read data from mmap like a normal File. Read more
sourcefn 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.
sourcefn 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.
sourcefn 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.
sourcefn 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.
sourcefn 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.
sourcefn 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.
sourcefn 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.
sourcefn 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.
sourcefn 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.
sourcefn 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.
sourcefn 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.
sourcefn 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.
sourcefn 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.
sourcefn 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.
sourcefn 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.
sourcefn 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.
sourcefn 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.
sourcefn 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.
sourcefn 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.
sourcefn 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.
sourcefn 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.
sourcefn 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.
sourcefn 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. Read more
sourcefn 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. Read more
Auto Trait Implementations
impl RefUnwindSafe for MmapFile
impl Send for MmapFile
impl Sync for MmapFile
impl Unpin for MmapFile
impl UnwindSafe for MmapFile
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