vmap-rs
A cross-platform library for fast and safe memory-mapped IO in Rust
Take a look at the Documentation for details!
This library defines a convenient API for reading and writing to files
using the hosts virtual memory system. The design of the API strives to
both minimize the frequency of mapping system calls while still retaining
safe access. Critically, it never attempts the own the File object used
for mapping. That is, it never clones it or in any way retains it. While
this has some implications for the API (i.e. .flush()), it cannot cause
bugs outside of this library through File's leaky abstraction when cloned
and then closed.
The Map and MapMut types are primary means for allocating virtual
memory regions, both for a file and anonymously. Generally, the
Map::with_options() and MapMut::with_options() are used to specify
the mapping requirements. See Options for more information.
The MapMut type maintains interior mutability for the mapped memory,
while the Map is read-only. However, it is possible to convert between
these types (.into_map_mut() and .into_map()) assuming the proper
Options are specified.
Additionally, a variety of buffer implementations are provided in the
vmap::io module. The Ring and InfiniteRing use circular memory
address allocations using cross-platform optimizations to minimize excess
resources where possible. The BufReader and BufWriter implement
buffered I/O using a Ring as a backing layer.
Examples
use Map;
use ;
let path = "example";
// Write some test data
write?;
// Map the first 4 bytes
let = with_options.len.open?;
assert_eq!;
// Reuse the file to map a different region
let map = with_options.offset.len.map?;
assert_eq!;
If opened properly, the Map can be moved into a MapMut and modifications
to the underlying file can be performed:
use Map;
use ;
let path = "example";
// Write some test data
write?;
// Open with write permissions so the Map can be converted into a MapMut
let = with_options.write.len.open?;
assert_eq!;
// Move the Map into a MapMut
// ... we could have started with MapMut::with_options()
let mut map = map.into_map_mut?;
map.clone_from_slice;
// Flush the changes to disk synchronously
map.flush?;
// Move the MapMut back into a Map
let map = map.into_map?;
assert_eq!;
Ring Buffer
The vmap library contains a Ring that constructs a circular memory
allocation where values can wrap from around from the end of the buffer back
to the beginning with sequential memory addresses. The InfiniteRing is
similar, however it allows writes to overwrite reads.
use ;
use ;
let mut buf = new.unwrap;
let mut i = 1;
// Fill up the buffer with lines.
while buf.write_len > 20
// No more space is available.
assert!;
let mut line = Stringnew;
// Read the first line written.
let len = buf.read_line?;
assert_eq!;
line.clear;
// Read the second line written.
let len = buf.read_line?;
assert_eq!;
// Now there is enough space to write more.
write!?;