| Crates.io | mmarinus |
| lib.rs | mmarinus |
| version | 0.4.0 |
| created_at | 2020-08-31 17:36:23.896218+00 |
| updated_at | 2022-04-27 13:46:45.458001+00 |
| description | A safe mmap implementation |
| homepage | https://github.com/enarx/mmarinus |
| repository | https://github.com/enarx/mmarinus |
| max_upload_size | |
| id | 283163 |
| size | 22,413 |
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