summaryrefslogtreecommitdiffstats
path: root/vendor/windows-metadata/src/row.rs
blob: 4e0c30593a367aaa5f6585cfb1a3f835179357d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Debug)]
pub struct Row {
    pub row: usize,
    pub file: usize,
}

impl Row {
    pub fn new(row: usize, file: usize) -> Self {
        Self { row, file }
    }

    fn next(&self) -> Self {
        Self { row: self.row + 1, file: self.file }
    }
}

pub trait AsRow: Copy {
    const TABLE: usize;
    fn to_row(&self) -> Row;
    fn from_row(row: Row) -> Self;

    fn file(&self) -> usize {
        self.to_row().file
    }

    fn index(&self) -> usize {
        self.to_row().row
    }

    fn next(&self) -> Self {
        Self::from_row(self.to_row().next())
    }
}

pub struct RowIterator<R: AsRow> {
    file: usize,
    rows: std::ops::Range<usize>,
    phantom: std::marker::PhantomData<R>,
}

impl<R: AsRow> RowIterator<R> {
    pub fn new(file: usize, rows: std::ops::Range<usize>) -> Self {
        Self { file, rows, phantom: std::marker::PhantomData }
    }
}

impl<R: AsRow> Iterator for RowIterator<R> {
    type Item = R;

    fn next(&mut self) -> Option<Self::Item> {
        self.rows.next().map(|row| R::from_row(Row::new(row, self.file)))
    }
}

macro_rules! tables {
    ($(($name:ident, $table:literal))+) => {
        $(
        #[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Debug)]
        pub struct $name(pub(crate) Row);
        impl AsRow for $name {
            const TABLE: usize = $table;
            fn to_row(&self) -> Row {
                self.0
            }
            fn from_row(row: Row) -> Self {
                $name(row)
            }
        }
    )*
};
}

tables! {
    (Attribute, 1)
    (ClassLayout, 16)
    (Constant, 0)
    (Field, 2)
    (GenericParam, 3)
    (ImplMap, 11)
    (InterfaceImpl, 4)
    (MemberRef, 5)
    (MethodDef, 6)
    (Module, 14)
    (ModuleRef, 12)
    (AssemblyRef, 15)
    (Param, 7)
    (TypeDef, 8)
    (TypeRef, 9)
    (TypeSpec, 10)
    (NestedClass, 13)
}