pub struct MmapDataSource { /* private fields */ }Expand description
A read-only DataSource backed by a memory-mapped file.
The mapping is created once at construction time using memmap2::Mmap.
Random-access reads through DataSource::read_range copy the requested
bytes; zero-copy access is available via MmapDataSource::as_bytes and
MmapDataSource::read_at.
The struct also implements std::io::Read and std::io::Seek so that
it can be passed to any reader that accepts R: Read + Seek. The internal
cursor used by those trait impls is independent of the DataSource API.
§Safety
Internally this calls unsafe { memmap2::Mmap::map(&file) }. The
invariant is: the file must not be modified through any other handle while
the mapping is live. Violating this invariant causes undefined behaviour.
Implementations§
Source§impl MmapDataSource
impl MmapDataSource
Sourcepub fn open(path: impl AsRef<Path>) -> Result<Self>
pub fn open(path: impl AsRef<Path>) -> Result<Self>
Opens path for read-only memory-mapped access.
§Errors
Returns an error if the file cannot be opened, its metadata cannot be
read, or mmap fails.
Sourcepub fn as_bytes(&self) -> &[u8] ⓘ
pub fn as_bytes(&self) -> &[u8] ⓘ
Returns a byte slice of the entire mapping.
For empty files this returns an empty slice.
Trait Implementations§
Source§impl DataSource for MmapDataSource
impl DataSource for MmapDataSource
Source§impl Debug for MmapDataSource
impl Debug for MmapDataSource
Source§impl Read for MmapDataSource
impl Read for MmapDataSource
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
1.36.0 · Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf. Read more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf)Source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf)cursor. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read. Read more1.0.0 · Source§fn chain<R>(self, next: R) -> Chain<Self, R>
fn chain<R>(self, next: R) -> Chain<Self, R>
Source§impl Seek for MmapDataSource
impl Seek for MmapDataSource
Source§fn seek(&mut self, pos: SeekFrom) -> Result<u64>
fn seek(&mut self, pos: SeekFrom) -> Result<u64>
1.55.0 · Source§fn rewind(&mut self) -> Result<(), Error>
fn rewind(&mut self) -> Result<(), Error>
Source§fn stream_len(&mut self) -> Result<u64, Error>
fn stream_len(&mut self) -> Result<u64, Error>
seek_stream_len)