Skip to content

Latest commit

 

History

History
34 lines (26 loc) · 1.22 KB

memmap.zh.md

File metadata and controls

34 lines (26 loc) · 1.22 KB

使用内存映射,随机访问文件

[![memmap-badge]][memmap] [![cat-filesystem-badge]][cat-filesystem]

使用[memmap]创建文件的内存映射,并模拟一些文件的非顺序读取。使用内存映射,意味着您只需要索引到一个切片,而不是处理导航文件的seek

这个Mmap::map函数假定内存映射后面的文件,不会同时被另一个进程修改,或者竞争条件发生。

extern crate memmap;

use memmap::Mmap;
use std::fs::File;
use std::io::{Write, Error};

fn main() -> Result<(), Error> {
#     write!(File::create("content.txt")?, "My hovercraft is full of eels!")?;
#
    let file = File::open("content.txt")?;
    let map = unsafe { Mmap::map(&file)? };

    let random_indexes = [0, 1, 2, 19, 22, 10, 11, 29];
    assert_eq!(&map[3..13], b"hovercraft");
    let random_bytes: Vec<u8> = random_indexes.iter()
        .map(|&idx| map[idx])
        .collect();
    assert_eq!(&random_bytes[..], b"My loaf!");
    Ok(())
}