pub struct MappedFile { /* private fields */ }Expand description
Memory-mapped file with zero-copy access.
On native platforms (Linux, macOS, Windows), this uses real OS-level
memory mapping via memmap2. On WASM, it falls back to standard
heap-based file reading.
§Safety
The mapped region is valid as long as:
- The file is not modified externally (single-writer assumption)
- The
MappedFileinstance is not dropped - The underlying storage remains available
§SIGBUS Warning (Unix)
If the underlying file is truncated while mapped, accessing the truncated region will generate a SIGBUS signal. This library does not install signal handlers. See module documentation for details.
§Example
use aprender::bundle::mmap::MappedFile;
let mapped = MappedFile::open("model.apr")?;
let header = mapped.slice(0, 32)?; // Zero-copy access
println!("File size: {} bytes", mapped.len());Implementations§
Source§impl MappedFile
impl MappedFile
Sourcepub fn open(path: impl AsRef<Path>) -> Result<Self>
pub fn open(path: impl AsRef<Path>) -> Result<Self>
Open a file for memory-mapped read access.
§Safety Justification (Jidoka)
The unsafe block is required because memmap2::Mmap::map() has
undefined behavior if:
- The file is modified by another process while mapped
- The file is truncated while mapped (triggers SIGBUS on Unix)
We mitigate these risks by:
- Opening the file read-only (no write handle to leak)
- Documenting the single-writer assumption
- Validating checksums before use (in .apr format)
References:
- Vahalia (1996): SIGBUS from truncated mmap
- memmap2 crate safety documentation
Sourcepub fn slice(&self, start: usize, end: usize) -> Option<&[u8]>
pub fn slice(&self, start: usize, end: usize) -> Option<&[u8]>
Get a subslice of the file (zero-copy).
Returns None if the range is out of bounds.
Sourcepub fn advise_sequential(&self) -> Result<()>
pub fn advise_sequential(&self) -> Result<()>
Advise the kernel about sequential access pattern (Unix only).
This can improve performance for linear scans through the file.
Sourcepub fn advise_random(&self) -> Result<()>
pub fn advise_random(&self) -> Result<()>
Advise the kernel about random access pattern (Unix only).
This can improve performance for random lookups.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for MappedFile
impl RefUnwindSafe for MappedFile
impl Send for MappedFile
impl Sync for MappedFile
impl Unpin for MappedFile
impl UnsafeUnpin for MappedFile
impl UnwindSafe for MappedFile
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
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more