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
use std::fs::{OpenOptions, copy};
use std::io::Read;
use memmap::MmapMut;
use std::path::PathBuf;
pub struct Loader;
impl Loader {
pub fn copy_file(from: &str, to: &str) -> std::io::Result<()>{
copy(from, to)?;
Ok(())
}
pub fn map_file_mut(name: &str) -> std::io::Result<memmap::MmapMut> {
let path : PathBuf = PathBuf::from(name);
let file = OpenOptions::new()
.read(true)
.write(true)
.open(&path)?;
let mmap = unsafe { MmapMut::map_mut(&file)? };
Ok(mmap)
}
pub fn init_file_mut(from: &str, to:&str) -> std::io::Result<memmap::MmapMut> {
Loader::copy_file(from, to.clone())?;
Loader::map_file_mut(to)
}
pub fn load_file_as_string(name: &str) -> std::io::Result<String> {
let mut output = String::new();
let mut file = OpenOptions::new()
.read(true)
.open(&name)?;
file.read_to_string(&mut output)?;
Ok(output)
}
}