This library allows reading files by using mmap()
under the hood.
Mapping a file into memory allows reading it as if it was a simple const char*
array (or &[u8]
in Rust terms).
Documentation is hosted here.
What is mmap()
?
mmap()
is a POSIX-compliant system call, which allows mapping files and devices into (virtual) memory.
When opening files the following operations will be executed:
open()
is called to get a file descriptor.fstat()
is called to get the file size. This is needed to letmmap()
know how much memory to map. This number is also used for bondary checking.- Using
mmap()
the file is mapped to memory.
The MappedFile
structure will keep track of the current seek position.
It also implements the following traits:
nix
- Provides friendly bindings to various *nix platform APIs
- Only the necessary features are enabled.
- This library only works on *nix-based systems (Linux, macOS). It does NOT support Windows.
- There's no write support yet.
- Implementing this is simple, as long as the operation would not modify the file size.
Yes. Unfortunately, there's no way around this.
- System calls are from Rust's perspective "unsafe", since they are external C functions.
- Usage of raw pointers
This heavily depends on how the OS decides to map the file. Such mappings are managed by the kernel automatically, and you don't have any control over it.
Either way, most operating systems are smart enough to not automatically map a huge file directly into RAM. mmap()
will almost always use virtual memory, instead of the physical RAM.
To put it simply, you don't really need to worry.
The documentation of the seek()
function says that it shall allow
seeking beyond the end of the file, however since such an operation would very likely cause a segfault, this library does not permit such operations. If a seek is attempted with an invalid offset, an error is returned.
When a MappedFile
goes out of scope, the memory is automatically unmapped using munmap()
and the file descriptor is closed.