use std::path::PathBuf;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FileStorageIntent {
Cached,
OnDisk,
}
#[derive(Debug, Clone)]
pub struct ComponentFileEntry {
pub path: PathBuf,
pub intent: FileStorageIntent,
}
#[derive(Debug, Clone)]
pub struct ComponentMemoryUsage {
pub files: Vec<ComponentFileEntry>,
pub extra_ram_bytes: Option<u64>,
}
impl ComponentMemoryUsage {
pub fn empty() -> Self {
Self {
files: Vec::new(),
extra_ram_bytes: None,
}
}
pub fn ram_only(bytes: u64) -> Self {
Self {
files: Vec::new(),
extra_ram_bytes: Some(bytes),
}
}
pub fn from_files(paths: Vec<PathBuf>, intent: FileStorageIntent) -> Self {
Self {
files: paths
.into_iter()
.map(|path| ComponentFileEntry { path, intent })
.collect(),
extra_ram_bytes: None,
}
}
pub fn from_files_and_ram(
paths: Vec<PathBuf>,
intent: FileStorageIntent,
extra_ram_bytes: u64,
) -> Self {
Self {
files: paths
.into_iter()
.map(|path| ComponentFileEntry { path, intent })
.collect(),
extra_ram_bytes: Some(extra_ram_bytes),
}
}
pub fn merge(&mut self, other: &ComponentMemoryUsage) {
self.files.extend(other.files.iter().cloned());
match (self.extra_ram_bytes, other.extra_ram_bytes) {
(Some(a), Some(b)) => self.extra_ram_bytes = Some(a + b),
(None, Some(b)) => self.extra_ram_bytes = Some(b),
(_, None) => {}
}
}
}
pub trait MemoryReporter {
fn memory_usage(&self) -> ComponentMemoryUsage;
}