Skip to content

Instantly share code, notes, and snippets.

@kthwaite
Created January 17, 2019 15:09
Show Gist options
  • Save kthwaite/84a92750f67b81f83b1b539f8b01e304 to your computer and use it in GitHub Desktop.
Save kthwaite/84a92750f67b81f83b1b539f8b01e304 to your computer and use it in GitHub Desktop.
#[macro_export]
macro_rules! associated_idx_type {
($base_type: ty, $id_type :ident) => {
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct $id_type(pub usize);
impl ops::Index<$id_type> for Vec<$base_type> {
type Output = $base_type;
fn index(&self, index: $id_type) -> &$base_type {
&self[index.0]
}
}
impl<'a> ops::Index<&'a $id_type> for Vec<$base_type> {
type Output = $base_type;
fn index(&self, index: &$id_type) -> &$base_type {
&self[index.0]
}
}
impl<'a> ops::IndexMut<&'a $id_type> for Vec<$base_type> {
fn index_mut(&mut self, index: &$id_type) -> &mut $base_type {
&mut self[index.0]
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment