use std::path::Path;
use std::io::BufReader;
use std::fs::File;
use std::io;
use std::io::Read;
use std::fmt;
#[derive(Clone, PartialEq)]
pub struct InMemoryFile {
pub(crate) bytes: Vec<u8>,
pub stats: FileStats,
}
impl fmt::Debug for InMemoryFile {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"SizedFile {{ bytes: ..., size: {}, priority: {} }}",
self.stats.size,
self.stats.priority
)
}
}
impl InMemoryFile {
pub fn open<P: AsRef<Path>>(path: P) -> io::Result<InMemoryFile> {
let file = File::open(path.as_ref())?;
let mut reader = BufReader::new(file);
let mut bytes: Vec<u8> = vec![];
let size: usize = reader.read_to_end(&mut bytes)?;
let stats = FileStats {
size,
access_count: 0,
priority: 0,
};
Ok(InMemoryFile { bytes, stats })
}
}
#[derive(Debug, PartialEq, Clone)]
pub struct FileStats {
pub size: usize,
pub access_count: usize,
pub priority: usize,
}