| 1 |
use alloc::{sync::Arc, vec::Vec}; |
| 2 |
use core::mem; |
| 3 |
|
| 4 |
use crate::id::{Id, Marker}; |
| 5 |
use crate::resource::ResourceType; |
| 6 |
use crate::{Epoch, Index}; |
| 7 |
|
| 8 |
/// An entry in a `Storage::map` table. |
| 9 |
#[derive(Debug)] |
| 10 |
pub(crate) enum Element<T> |
| 11 |
where |
| 12 |
T: StorageItem, |
| 13 |
{ |
| 14 |
/// There are no live ids with this index. |
| 15 |
Vacant, |
| 16 |
|
| 17 |
/// There is one live id with this index, allocated at the given |
| 18 |
/// epoch. |
| 19 |
Occupied(T, Epoch), |
| 20 |
} |
| 21 |
|
| 22 |
pub(crate) trait StorageItem: ResourceType { |
| 23 |
type Marker: Marker; |
| 24 |
} |
| 25 |
|
| 26 |
impl<T: ResourceType> ResourceType for Arc<T> { |
| 27 |
const TYPE: &'static str = T::TYPE; |
| 28 |
} |
| 29 |
|
| 30 |
impl<T: StorageItem> StorageItem for Arc<T> { |
| 31 |
type Marker = T::Marker; |
| 32 |
} |
| 33 |
|
| 34 |
#[macro_export] |
| 35 |
macro_rules! impl_storage_item { |
| 36 |
($ty:ident) => { |
| 37 |
impl $crate::storage::StorageItem for $ty { |
| 38 |
type Marker = $crate::id::markers::$ty; |
| 39 |
} |
| 40 |
}; |
| 41 |
} |
| 42 |
|
| 43 |
/// A table of `T` values indexed by the id type `I`. |
| 44 |
/// |
| 45 |
/// `Storage` implements [`core::ops::Index`], accepting `Id` values as |
| 46 |
/// indices. |
| 47 |
/// |
| 48 |
/// The table is represented as a vector indexed by the ids' index |
| 49 |
/// values, so you should use an id allocator like `IdentityManager` |
| 50 |
/// that keeps the index values dense and close to zero. |
| 51 |
#[derive(Debug)] |
| 52 |
pub(crate) struct Storage<T> |
| 53 |
where |
| 54 |
T: StorageItem, |
| 55 |
{ |
| 56 |
pub(crate) map: Vec<Element<T>>, |
| 57 |
kind: &'static str, |
| 58 |
} |
| 59 |
|
| 60 |
impl<T> Storage<T> |
| 61 |
where |
| 62 |
T: StorageItem, |
| 63 |
{ |
| 64 |
pub(crate) fn new() -> Self { |
| 65 |
Self { |
| 66 |
map: Vec::new(), |
| 67 |
kind: T::TYPE, |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
impl<T> Storage<T> |
| 73 |
where |
| 74 |
T: StorageItem, |
| 75 |
{ |
| 76 |
pub(crate) fn insert(&mut self, id: Id<T::Marker>, value: T) { |
| 77 |
let (index, epoch) = id.unzip(); |
| 78 |
let index = index as usize; |
| 79 |
if index >= self.map.len() { |
| 80 |
self.map.resize_with(index + 1, || Element::Vacant); |
| 81 |
} |
| 82 |
match mem::replace(&mut self.map[index], Element::Occupied(value, epoch)) { |
| 83 |
Element::Vacant => {} |
| 84 |
Element::Occupied(_, storage_epoch) => { |
| 85 |
assert_ne!( |
| 86 |
epoch, |
| 87 |
storage_epoch, |
| 88 |
"Index {index:?} of {} is already occupied", |
| 89 |
T::TYPE |
| 90 |
); |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
pub(crate) fn remove(&mut self, id: Id<T::Marker>) -> T { |
| 96 |
let (index, epoch) = id.unzip(); |
| 97 |
match mem::replace(&mut self.map[index as usize], Element::Vacant) { |
| 98 |
Element::Occupied(value, storage_epoch) => { |
| 99 |
assert_eq!(epoch, storage_epoch); |
| 100 |
value |
| 101 |
} |
| 102 |
Element::Vacant => panic!("Cannot remove a vacant resource"), |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
pub(crate) fn iter(&self) -> impl Iterator<Item = (Id<T::Marker>, &T)> { |
| 107 |
self.map |
| 108 |
.iter() |
| 109 |
.enumerate() |
| 110 |
.filter_map(move |(index, x)| match *x { |
| 111 |
Element::Occupied(ref value, storage_epoch) => { |
| 112 |
Some((Id::zip(index as Index, storage_epoch), value)) |
| 113 |
} |
| 114 |
_ => None, |
| 115 |
}) |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
impl<T> Storage<T> |
| 120 |
where |
| 121 |
T: StorageItem + Clone, |
| 122 |
{ |
| 123 |
/// Get an owned reference to an item. |
| 124 |
/// Panics if there is an epoch mismatch, the entry is empty or in error. |
| 125 |
pub(crate) fn get(&self, id: Id<T::Marker>) -> T { |
| 126 |
let (index, epoch) = id.unzip(); |
| 127 |
let (result, storage_epoch) = match self.map.get(index as usize) { |
| 128 |
Some(&Element::Occupied(ref v, epoch)) => (v.clone(), epoch), |
| 129 |
None | Some(&Element::Vacant) => panic!("{}[{:?}] does not exist", self.kind, id), |
| 130 |
}; |
| 131 |
assert_eq!( |
| 132 |
epoch, storage_epoch, |
| 133 |
"{}[{:?}] is no longer alive", |
| 134 |
self.kind, id |
| 135 |
); |
| 136 |
result |
| 137 |
} |
| 138 |
} |