|
| 1 | +macro_rules! make_table_column { |
| 2 | + ($name: ident, $index: ident) => { |
| 3 | + /// Immutable view of a column |
| 4 | + #[derive(Clone, Debug)] |
| 5 | + #[repr(transparent)] |
| 6 | + pub struct $name<'table, T>(&'table [T]); |
| 7 | + |
| 8 | + impl<'table, T> $name<'table, T> { |
| 9 | + pub(crate) fn new(column: &'table [T]) -> $name<'table, T> { |
| 10 | + Self(column) |
| 11 | + } |
| 12 | + |
| 13 | + /// View the underlying slice |
| 14 | + pub fn as_slice(&self) -> &[T] { |
| 15 | + self.0 |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + impl<T> std::ops::Index<usize> for $name<'_, T> { |
| 20 | + type Output = T; |
| 21 | + fn index(&self, index: usize) -> &Self::Output { |
| 22 | + &self.0[index] |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + impl<T> std::ops::Index<crate::$index> for $name<'_, T> { |
| 27 | + type Output = T; |
| 28 | + fn index(&self, index: crate::$index) -> &Self::Output { |
| 29 | + &self.0[usize::try_from(index).unwrap()] |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + impl<T> std::ops::Index<crate::SizeType> for $name<'_, T> { |
| 34 | + type Output = T; |
| 35 | + fn index(&self, index: crate::SizeType) -> &Self::Output { |
| 36 | + &self.0[usize::try_from(index).unwrap()] |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + impl<T> std::convert::AsRef<[T]> for $name<'_, T> { |
| 41 | + fn as_ref(&self) -> &[T] { |
| 42 | + self.0 |
| 43 | + } |
| 44 | + } |
| 45 | + }; |
| 46 | +} |
| 47 | + |
| 48 | +make_table_column!(NodeTableColumn, NodeId); |
0 commit comments