libreda_db/reference_access/
hierarchy_reference_access.rs1use crate::traits::HierarchyBase;
7
8pub trait HierarchyReferenceAccess: HierarchyBase {
10 fn each_cell_ref(&self) -> Box<dyn Iterator<Item = CellRef<Self>> + '_> {
12 Box::new(self.each_cell().map(move |id| self.cell_ref(&id)))
13 }
14
15 fn cell_ref(&self, cell_id: &Self::CellId) -> CellRef<'_, Self> {
17 CellRef {
18 base: self,
19 id: cell_id.clone(),
20 }
21 }
22
23 fn cell_instance_ref(&self, inst_id: &Self::CellInstId) -> CellInstRef<'_, Self> {
25 CellInstRef {
26 base: self,
27 id: inst_id.clone(),
28 }
29 }
30}
31
32impl<T: HierarchyBase> HierarchyReferenceAccess for T {}
33
34pub struct CellRef<'a, H: HierarchyBase + ?Sized> {
37 pub(super) base: &'a H,
39 pub(super) id: H::CellId,
41}
42
43impl<'a, H: HierarchyBase> Eq for CellRef<'a, H> {}
44impl<'a, H: HierarchyBase> PartialEq for CellRef<'a, H> {
45 fn eq(&self, other: &Self) -> bool {
46 self.id == other.id && std::ptr::eq(self.base, other.base)
47 }
48}
49
50impl<'a, H: HierarchyBase> std::fmt::Debug for CellRef<'a, H> {
51 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52 write!(f, "CellRef({})", self.name())
53 }
54}
55
56impl<'a, H: HierarchyBase> Clone for CellRef<'a, H> {
72 fn clone(&self) -> Self {
73 Self {
74 base: self.base,
75 id: self.id.clone(),
76 }
77 }
78}
79
80impl<'a, H: HierarchyBase> CellRef<'a, H> {
81 pub fn base(&self) -> &'_ H {
83 self.base
84 }
85
86 pub fn id(&self) -> H::CellId {
88 self.id.clone()
89 }
90
91 pub fn name(&self) -> H::NameType {
93 self.base.cell_name(&self.id)
94 }
95
96 pub fn each_cell_instance_id(&self) -> impl Iterator<Item = H::CellInstId> + '_ {
98 self.base.each_cell_instance(&self.id)
99 }
100
101 pub fn each_cell_instance(&self) -> impl Iterator<Item = CellInstRef<'a, H>> + '_ {
103 self.each_cell_instance_id().map(move |id| CellInstRef {
104 base: self.base,
105 id,
106 })
107 }
108
109 pub fn cell_instance_by_name(&self, name: &str) -> Option<CellInstRef<'a, H>> {
111 self.base
112 .cell_instance_by_name(&self.id, name)
113 .map(|id| CellInstRef {
114 base: self.base,
115 id,
116 })
117 }
118
119 pub fn each_reference_id(&self) -> impl Iterator<Item = H::CellInstId> + '_ {
121 self.base.each_cell_reference(&self.id)
122 }
123
124 pub fn each_reference(&self) -> impl Iterator<Item = CellInstRef<'a, H>> + '_ {
126 self.each_reference_id().map(move |id| CellInstRef {
127 base: self.base,
128 id,
129 })
130 }
131
132 pub fn num_references(&self) -> usize {
134 self.base.num_cell_references(&self.id)
135 }
136
137 pub fn each_cell_dependency(&self) -> impl Iterator<Item = CellRef<'a, H>> + '_ {
139 self.base
140 .each_cell_dependency(&self.id)
141 .map(move |id| CellRef {
142 base: self.base,
143 id,
144 })
145 }
146
147 pub fn num_cell_dependencies(&self) -> usize {
149 self.base.num_cell_dependencies(&self.id)
150 }
151
152 pub fn each_dependent_cell(&self) -> impl Iterator<Item = CellRef<'a, H>> + '_ {
154 self.base
155 .each_dependent_cell(&self.id)
156 .map(move |id| CellRef {
157 base: self.base,
158 id,
159 })
160 }
161
162 pub fn num_dependent_cells(&self) -> usize {
164 self.base.num_dependent_cells(&self.id)
165 }
166
167 pub fn num_child_instances(&self) -> usize {
169 self.base.num_child_instances(&self.id)
170 }
171}
172
173pub struct CellInstRef<'a, H: HierarchyBase + ?Sized> {
176 pub(super) base: &'a H,
178 pub(super) id: H::CellInstId,
180}
181
182impl<'a, H: HierarchyBase> Eq for CellInstRef<'a, H> {}
183impl<'a, H: HierarchyBase> PartialEq for CellInstRef<'a, H> {
184 fn eq(&self, other: &Self) -> bool {
185 self.id == other.id && std::ptr::eq(self.base, other.base)
186 }
187}
188
189impl<'a, H: HierarchyBase> std::fmt::Debug for CellInstRef<'a, H> {
190 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
191 write!(f, "CellInstRef({})", self.qname(":"))
192 }
193}
194
195impl<'a, H: HierarchyBase> CellInstRef<'a, H> {
196 pub fn base(&self) -> &'_ H {
198 self.base
199 }
200
201 pub fn id(&self) -> H::CellInstId {
203 self.id.clone()
204 }
205
206 pub fn name(&self) -> Option<H::NameType> {
208 self.base.cell_instance_name(&self.id)
209 }
210
211 pub fn qname(&self, separator: &str) -> String {
213 format!(
214 "{}{}{}",
215 self.parent().name(),
216 separator,
217 self.name()
218 .unwrap_or_else(|| "<unnamed>".to_string().into())
219 )
220 }
221
222 pub fn parent(&self) -> CellRef<'a, H> {
224 CellRef {
225 base: self.base,
226 id: self.parent_id(),
227 }
228 }
229
230 pub fn template(&self) -> CellRef<'a, H> {
232 CellRef {
233 base: self.base,
234 id: self.template_id(),
235 }
236 }
237
238 pub fn parent_id(&self) -> H::CellId {
240 self.base.parent_cell(&self.id)
241 }
242
243 pub fn template_id(&self) -> H::CellId {
245 self.base.template_cell(&self.id)
246 }
247}