5 releases (3 breaking)

0.4.0 Apr 27, 2022
0.3.0 Apr 26, 2022
0.2.1 Aug 16, 2021
0.2.0 Aug 31, 2020
0.1.0 Aug 31, 2020

#466 in Unix APIs

Download history 23/week @ 2024-07-20 27/week @ 2024-07-27 40/week @ 2024-08-03 24/week @ 2024-08-10 9/week @ 2024-08-17 12/week @ 2024-08-24 107/week @ 2024-08-31 40/week @ 2024-09-07 38/week @ 2024-09-14 68/week @ 2024-09-21 102/week @ 2024-09-28 74/week @ 2024-10-05 26/week @ 2024-10-12 25/week @ 2024-10-19 20/week @ 2024-10-26 10/week @ 2024-11-02

85 downloads per month
Used in 2 crates

Apache-2.0

23KB
440 lines

Workflow Status Average time to resolve an issue Percentage of issues still open Maintenance

mmarinus

The mmarinus crate wraps the underlying system mmap() call in safe semantics.

For example:

use mmarinus::{Map, perms};

let mut zero = std::fs::File::open("/dev/zero").unwrap();

let map = Map::bytes(32)
    .near(128 * 1024 * 1024)
    .from(&mut zero, 0)
    .with(perms::Read)
    .unwrap();

assert_eq!(&*map, &[0; 32]);

You can also remap an existing mapping:

use mmarinus::{Map, perms};

let mut zero = std::fs::File::open("/dev/zero").unwrap();

let mut map = Map::bytes(32)
    .anywhere()
    .from(&mut zero, 0)
    .with(perms::Read)
    .unwrap();

assert_eq!(&*map, &[0; 32]);

let mut map = map.remap()
    .from(&mut zero, 0)
    .with(perms::ReadWrite)
    .unwrap();

assert_eq!(&*map, &[0; 32]);
for i in map.iter_mut() {
    *i = 255;
}
assert_eq!(&*map, &[255; 32]);

Alternatively, you can just change the permissions:

use mmarinus::{Map, perms};

let mut zero = std::fs::File::open("/dev/zero").unwrap();

let mut map = Map::bytes(32)
    .at(128 * 1024 * 1024)
    .from(&mut zero, 0)
    .with(perms::Read)
    .unwrap();

assert_eq!(&*map, &[0; 32]);

let mut map = map.reprotect(perms::ReadWrite).unwrap();

assert_eq!(&*map, &[0; 32]);
for i in map.iter_mut() {
    *i = 255;
}
assert_eq!(&*map, &[255; 32]);

Mapping a whole file into memory is easy:

use mmarinus::{Map, Private, perms};

let map = Map::load("/etc/os-release", Private, perms::Read).unwrap();

License: Apache-2.0

Dependencies

~44KB