This commit is contained in:
Chris Fallin 2022-11-10 00:50:09 -08:00
parent e53d801107
commit 26244fbfbd
No known key found for this signature in database
GPG key ID: 31649E4FE65EB465

View file

@ -148,12 +148,14 @@ pub struct PerEntity<Idx: EntityRef, T: Clone + Debug + Default>(Vec<T>, Phantom
impl<Idx: EntityRef, T: Clone + Debug + Default> Index<Idx> for PerEntity<Idx, T> { impl<Idx: EntityRef, T: Clone + Debug + Default> Index<Idx> for PerEntity<Idx, T> {
type Output = T; type Output = T;
fn index(&self, idx: Idx) -> &T { fn index(&self, idx: Idx) -> &T {
debug_assert!(idx.is_valid());
self.0.get(idx.index()).unwrap_or(&self.2) self.0.get(idx.index()).unwrap_or(&self.2)
} }
} }
impl<Idx: EntityRef, T: Clone + Debug + Default> IndexMut<Idx> for PerEntity<Idx, T> { impl<Idx: EntityRef, T: Clone + Debug + Default> IndexMut<Idx> for PerEntity<Idx, T> {
fn index_mut(&mut self, idx: Idx) -> &mut T { fn index_mut(&mut self, idx: Idx) -> &mut T {
debug_assert!(idx.is_valid());
if idx.index() >= self.0.len() { if idx.index() >= self.0.len() {
self.0.resize(idx.index() + 1, T::default()); self.0.resize(idx.index() + 1, T::default());
} }