ploidy_core/ir/views/
wrappers.rs1use petgraph::graph::NodeIndex;
2
3use crate::ir::{
4 graph::{IrGraph, IrGraphNode},
5 types::{IrType, PrimitiveIrType},
6};
7
8use super::{IrTypeView, ViewNode};
9
10#[derive(Debug)]
12pub struct IrArrayView<'a> {
13 graph: &'a IrGraph<'a>,
14 index: NodeIndex,
15 inner: &'a IrType<'a>,
16}
17
18impl<'a> IrArrayView<'a> {
19 #[inline]
20 pub(in crate::ir) fn new(
21 graph: &'a IrGraph<'a>,
22 index: NodeIndex,
23 inner: &'a IrType<'a>,
24 ) -> Self {
25 Self {
26 graph,
27 index,
28 inner,
29 }
30 }
31
32 #[inline]
34 pub fn inner(&self) -> IrTypeView<'a> {
35 let node = IrGraphNode::from_ref(self.graph.spec, self.inner.as_ref());
36 IrTypeView::new(self.graph, self.graph.indices[&node])
37 }
38}
39
40impl<'a> ViewNode<'a> for IrArrayView<'a> {
41 #[inline]
42 fn graph(&self) -> &'a IrGraph<'a> {
43 self.graph
44 }
45
46 #[inline]
47 fn index(&self) -> NodeIndex {
48 self.index
49 }
50}
51
52#[derive(Debug)]
54pub struct IrMapView<'a> {
55 graph: &'a IrGraph<'a>,
56 index: NodeIndex,
57 inner: &'a IrType<'a>,
58}
59
60impl<'a> IrMapView<'a> {
61 #[inline]
62 pub(in crate::ir) fn new(
63 graph: &'a IrGraph<'a>,
64 index: NodeIndex,
65 inner: &'a IrType<'a>,
66 ) -> Self {
67 Self {
68 graph,
69 index,
70 inner,
71 }
72 }
73
74 #[inline]
76 pub fn inner(&self) -> IrTypeView<'a> {
77 let node = IrGraphNode::from_ref(self.graph.spec, self.inner.as_ref());
78 IrTypeView::new(self.graph, self.graph.indices[&node])
79 }
80}
81
82impl<'a> ViewNode<'a> for IrMapView<'a> {
83 #[inline]
84 fn graph(&self) -> &'a IrGraph<'a> {
85 self.graph
86 }
87
88 #[inline]
89 fn index(&self) -> NodeIndex {
90 self.index
91 }
92}
93
94#[derive(Debug)]
96pub struct IrOptionalView<'a> {
97 graph: &'a IrGraph<'a>,
98 index: NodeIndex,
99 inner: &'a IrType<'a>,
100}
101
102impl<'a> IrOptionalView<'a> {
103 #[inline]
104 pub(in crate::ir) fn new(
105 graph: &'a IrGraph<'a>,
106 index: NodeIndex,
107 inner: &'a IrType<'a>,
108 ) -> Self {
109 Self {
110 graph,
111 index,
112 inner,
113 }
114 }
115
116 #[inline]
118 pub fn inner(&self) -> IrTypeView<'a> {
119 let node = IrGraphNode::from_ref(self.graph.spec, self.inner.as_ref());
120 IrTypeView::new(self.graph, self.graph.indices[&node])
121 }
122}
123
124impl<'a> ViewNode<'a> for IrOptionalView<'a> {
125 #[inline]
126 fn graph(&self) -> &'a IrGraph<'a> {
127 self.graph
128 }
129
130 #[inline]
131 fn index(&self) -> NodeIndex {
132 self.index
133 }
134}
135
136#[derive(Debug)]
138pub struct IrPrimitiveView<'a> {
139 graph: &'a IrGraph<'a>,
140 index: NodeIndex,
141 ty: PrimitiveIrType,
142}
143
144impl<'a> IrPrimitiveView<'a> {
145 #[inline]
146 pub(in crate::ir) fn new(
147 graph: &'a IrGraph<'a>,
148 index: NodeIndex,
149 ty: PrimitiveIrType,
150 ) -> Self {
151 Self { graph, index, ty }
152 }
153
154 #[inline]
156 pub fn ty(&self) -> PrimitiveIrType {
157 self.ty
158 }
159}
160
161impl<'a> ViewNode<'a> for IrPrimitiveView<'a> {
162 #[inline]
163 fn graph(&self) -> &'a IrGraph<'a> {
164 self.graph
165 }
166
167 #[inline]
168 fn index(&self) -> NodeIndex {
169 self.index
170 }
171}