@@ -31,6 +31,47 @@ impl<T> MutableTableColumn<'_, T> {
3131 }
3232}
3333
34+ macro_rules! make_table_column {
35+ ( $name: ident, $index: ident) => {
36+ /// Immutable view of a column
37+ pub struct $name<' table, T >( & ' table [ T ] ) ;
38+
39+ impl <' table, T > $name<' table, T > {
40+ pub ( crate ) fn new( column: & ' table [ T ] ) -> $name<' table, T > {
41+ Self ( column)
42+ }
43+
44+ /// View the underlying slice
45+ pub fn as_slice( & self ) -> & [ T ] {
46+ self . 0
47+ }
48+ }
49+
50+ impl <T > std:: ops:: Index <usize > for $name<' _, T > {
51+ type Output = T ;
52+ fn index( & self , index: usize ) -> & Self :: Output {
53+ & self . 0 [ index]
54+ }
55+ }
56+
57+ impl <T > std:: ops:: Index <crate :: $index> for $name<' _, T > {
58+ type Output = T ;
59+ fn index( & self , index: crate :: $index) -> & Self :: Output {
60+ & self . 0 [ usize :: try_from( index) . unwrap( ) ]
61+ }
62+ }
63+
64+ impl <T > std:: ops:: Index <crate :: SizeType > for $name<' _, T > {
65+ type Output = T ;
66+ fn index( & self , index: crate :: SizeType ) -> & Self :: Output {
67+ & self . 0 [ usize :: try_from( index) . unwrap( ) ]
68+ }
69+ }
70+ } ;
71+ }
72+
73+ make_table_column ! ( NodeTableColumn , NodeId ) ;
74+
3475// NOT part of tskit's public API
3576pub fn new_table_column < T > ( data : & [ T ] ) -> TableColumn < ' _ , T > {
3677 TableColumn ( data)
0 commit comments