Skip to main content

MappedFile

Struct MappedFile 

Source
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:

  1. The file is not modified externally (single-writer assumption)
  2. The MappedFile instance is not dropped
  3. 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

Source

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:

  1. Opening the file read-only (no write handle to leak)
  2. Documenting the single-writer assumption
  3. Validating checksums before use (in .apr format)

References:

  • Vahalia (1996): SIGBUS from truncated mmap
  • memmap2 crate safety documentation
Source

pub fn as_slice(&self) -> &[u8]

Get the entire file as a byte slice (zero-copy).

Source

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.

Source

pub fn len(&self) -> usize

File size in bytes.

Source

pub fn is_empty(&self) -> bool

Check if file is empty.

Source

pub fn path(&self) -> &str

Get the file path.

Source

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.

Source

pub fn advise_random(&self) -> Result<()>

Advise the kernel about random access pattern (Unix only).

This can improve performance for random lookups.

Trait Implementations§

Source§

impl Debug for MappedFile

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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, 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V