]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_data_structures/src/vec_linked_list.rs
New upstream version 1.71.1+dfsg1
[rustc.git] / compiler / rustc_data_structures / src / vec_linked_list.rs
CommitLineData
49aad941 1use rustc_index::{Idx, IndexVec};
b7449926
XL
2
3pub fn iter<Ls>(
4 first: Option<Ls::LinkIndex>,
a2a8927a
XL
5 links: &Ls,
6) -> impl Iterator<Item = Ls::LinkIndex> + '_
b7449926
XL
7where
8 Ls: Links,
9{
dfeec247 10 VecLinkedListIterator { links, current: first }
b7449926
XL
11}
12
13pub struct VecLinkedListIterator<Ls>
14where
15 Ls: Links,
16{
17 links: Ls,
18 current: Option<Ls::LinkIndex>,
19}
20
21impl<Ls> Iterator for VecLinkedListIterator<Ls>
22where
23 Ls: Links,
24{
25 type Item = Ls::LinkIndex;
26
27 fn next(&mut self) -> Option<Ls::LinkIndex> {
28 if let Some(c) = self.current {
29 self.current = <Ls as Links>::next(&self.links, c);
30 Some(c)
31 } else {
32 None
33 }
34 }
35}
36
37pub trait Links {
38 type LinkIndex: Copy;
39
40 fn next(links: &Self, index: Self::LinkIndex) -> Option<Self::LinkIndex>;
41}
42
43impl<Ls> Links for &Ls
44where
45 Ls: Links,
46{
47 type LinkIndex = Ls::LinkIndex;
48
49 fn next(links: &Self, index: Ls::LinkIndex) -> Option<Ls::LinkIndex> {
50 <Ls as Links>::next(links, index)
51 }
52}
53
54pub trait LinkElem {
55 type LinkIndex: Copy;
56
57 fn next(elem: &Self) -> Option<Self::LinkIndex>;
58}
59
60impl<L, E> Links for IndexVec<L, E>
61where
62 E: LinkElem<LinkIndex = L>,
63 L: Idx,
64{
65 type LinkIndex = L;
66
67 fn next(links: &Self, index: L) -> Option<L> {
68 <E as LinkElem>::next(&links[index])
69 }
70}