motion_core/asset/
store.rs1use super::*;
4
5#[derive(Clone, Copy, Debug, PartialEq, Eq)]
10pub struct ResourceLimits {
11 pub max_bytes: usize,
13 pub max_entries: usize,
15}
16
17impl Default for ResourceLimits {
18 fn default() -> Self {
19 Self {
20 max_bytes: MAX_STORE_BYTES,
21 max_entries: MAX_ENTRIES,
22 }
23 }
24}
25
26#[derive(Clone, Default)]
33pub struct ResourceStore(pub(super) Arc<Store>);
34
35pub(super) struct Store {
36 pub(super) bytes: AtomicUsize,
37 pub(super) entries: Mutex<BTreeMap<CacheKey, Weak<Erased>>>,
38 pub(super) limits: ResourceLimits,
39}
40
41impl Default for Store {
42 fn default() -> Self {
43 Self {
44 bytes: AtomicUsize::new(0),
45 entries: Mutex::new(BTreeMap::new()),
46 limits: ResourceLimits::default(),
47 }
48 }
49}
50
51impl ResourceStore {
52 pub fn new() -> Self {
54 Self::default()
55 }
56
57 pub fn with_limits(limits: ResourceLimits) -> crate::Result<Self> {
59 if limits.max_bytes == 0 || !(1..=MAX_ENTRIES).contains(&limits.max_entries) {
60 return Err(io::Error::new(
61 io::ErrorKind::InvalidInput,
62 "Resource limits require positive bytes and 1..=4096 entries",
63 )
64 .into());
65 }
66 Ok(Self(Arc::new(Store {
67 limits,
68 ..Store::default()
69 })))
70 }
71
72 pub fn max_bytes(&self) -> usize {
74 self.0.limits.max_bytes
75 }
76
77 pub fn max_entries(&self) -> usize {
79 self.0.limits.max_entries
80 }
81
82 pub fn retained_bytes(&self) -> usize {
84 self.0.bytes.load(Ordering::Acquire)
85 }
86
87 pub fn same_store(&self, other: &Self) -> bool {
89 Arc::ptr_eq(&self.0, &other.0)
90 }
91}