use std::ops::{Index, IndexMut}; pub struct Id { idx: usize, _pd: std::marker::PhantomData, } pub struct IdVec { vec: Vec, } impl IdVec { pub fn add(&mut self, val: T) -> Id { let id = Id { idx: self.vec.len(), _pd: Default::default(), }; self.vec.push(val); id } } impl Index> for IdVec { type Output = T; fn index(&self, index: Id) -> &Self::Output { &self.vec[index.idx] } } impl IndexMut> for IdVec { fn index_mut(&mut self, index: Id) -> &mut Self::Output { &mut self.vec[index.idx] } } impl Default for IdVec { fn default() -> Self { Self { vec: Default::default(), } } } impl Clone for Id { fn clone(&self) -> Self { *self } } impl Copy for Id {}