blob: fb98e6263bc50fa3cd1c39b468af57c1f8d21cae (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
use memmap2::{Mmap, MmapOptions};
use std::{
fs::File,
io::{stdin, Read},
path::PathBuf,
};
use crate::error::Result;
#[derive(Debug, PartialEq)]
pub(crate) enum Source {
Stdin,
File(PathBuf),
}
impl Source {
pub(crate) fn from_paths(paths: Vec<PathBuf>) -> Vec<Self> {
paths.into_iter().map(Self::File).collect()
}
pub(crate) fn from_stdin() -> Vec<Self> {
vec![Self::Stdin]
}
pub(crate) fn display(&self) -> String {
match self {
Self::Stdin => "STDIN".to_string(),
Self::File(path) => format!("FILE {}", path.display()),
}
}
}
// TODO: memmap2 docs state that users should implement proper
// procedures to avoid problems the `unsafe` keyword indicate.
// This would be in a later PR.
pub(crate) unsafe fn make_mmap(path: &PathBuf) -> Result<Mmap> {
Ok(Mmap::map(&File::open(path)?)?)
}
pub(crate) fn make_mmap_stdin() -> Result<Mmap> {
let mut handle = stdin().lock();
let mut buf = Vec::new();
handle.read_to_end(&mut buf)?;
let mut mmap = MmapOptions::new().len(buf.len()).map_anon()?;
mmap.copy_from_slice(&buf);
let mmap = mmap.make_read_only()?;
Ok(mmap)
}
|