mod cache;
mod lifecycle;
mod names;
mod object;
#[cfg(test)]
mod tests;
mod write;
use alloc::collections::BTreeMap;
use alloc::string::String;
use alloc::vec::Vec;
use hashbrown::HashMap;
use rtic_core::Mutex;
use crate::backend::{Backend, Usage, Wear};
use crate::{ObjShelf, ObjectId};
use cache::Entry;
pub(crate) struct Core<B: Backend> {
backend: Option<B>,
usage: Usage,
wear: Option<Wear>,
directory: BTreeMap<String, ObjectId>,
removed: Vec<(ObjectId, String)>,
next_id: u32,
cache: HashMap<ObjectId, Entry<B>>,
cache_bytes: usize,
used: usize,
modified_bytes: usize,
on_full: fn(),
tick: u64,
}
pub(crate) struct Locked<'a, M>(pub(crate) &'a mut M);
impl<B: Backend, M: Mutex<T = ObjShelf<B>>> Locked<'_, M> {
fn lock<R>(&mut self, f: impl FnOnce(&mut Core<B>) -> R) -> R {
self.0.lock(|mem| f(&mut mem.core))
}
pub(crate) fn usage(&mut self) -> Usage {
self.lock(|core| core.usage)
}
pub(crate) fn wear(&mut self) -> Option<Wear> {
self.lock(|core| core.wear)
}
}
impl<B: Backend> Core<B> {
fn next_tick(&mut self) -> u64 {
self.tick += 1;
self.tick
}
fn fits_modified(&self, size: usize) -> bool {
self.modified_bytes + size <= self.cache_bytes
}
fn full<R>(&self) -> Option<R> {
(self.on_full)();
None
}
fn restore(&mut self, backend: B) {
self.usage = backend.usage();
self.wear = backend.wear();
self.backend = Some(backend);
}
}