ridx_format/ty_arena/
field_view.rs1use crate::format::{
14 CtorShape, DefTyEntry, FieldEntry, ItemVis, TyId, DEF_TY_FLAG_DEFAULT_ASSOC, DEF_TY_FLAG_FUNDAMENTAL, DEF_TY_FLAG_MUT_STATIC, DEF_TY_FLAG_NON_EXHAUSTIVE,
15 DEF_TY_FLAG_UNSAFE_STATIC, FIELD_FLAG_TUPLE, FIELD_FLAG_UNSAFE,
16};
17
18use super::TyArenaView;
19
20#[derive(Debug, Clone, Copy)]
22pub struct RawField<'a> {
23 pub owner_def_index: u32,
24 pub def_index: Option<u32>,
25 pub name: &'a str,
26 pub ty: TyId,
27 pub field_idx: u16,
28 pub vis: ItemVis,
29 pub flags: u8,
30}
31
32#[derive(Debug, Clone, Copy)]
34pub struct RawDefTy {
35 pub def_index: u32,
36 pub ty: TyId,
37 pub parent_def_index: Option<u32>,
38 pub variant_idx: Option<u16>,
39 pub ctor_shape: CtorShape,
40 pub flags: u8,
41}
42
43#[derive(Debug, Clone, Copy, PartialEq, Eq)]
45pub struct FieldView<'a> {
46 pub owner_def_index: u32,
47 pub def_index: Option<u32>,
48 pub name: &'a str,
49 pub ty: TyId,
50 pub field_idx: u16,
51 pub vis: ItemVis,
52 pub flags: u8,
53}
54
55impl<'a> FieldView<'a> {
56 #[inline]
57 pub const fn is_tuple_field(&self) -> bool {
58 (self.flags & FIELD_FLAG_TUPLE) != 0
59 }
60
61 #[inline]
62 pub const fn is_unsafe(&self) -> bool {
63 (self.flags & FIELD_FLAG_UNSAFE) != 0
64 }
65}
66
67#[derive(Debug, Clone, Copy)]
69pub struct FieldIter<'a> {
70 arena: TyArenaView<'a>,
71 entries: &'a [FieldEntry],
72}
73
74impl<'a> FieldIter<'a> {
75 #[inline]
76 pub const fn new(arena: TyArenaView<'a>, entries: &'a [FieldEntry]) -> Self {
77 Self { arena, entries }
78 }
79
80 #[inline]
81 pub const fn len(&self) -> usize {
82 self.entries.len()
83 }
84
85 #[inline]
86 pub const fn is_empty(&self) -> bool {
87 self.entries.is_empty()
88 }
89
90 #[inline]
91 pub fn get(&self, idx: usize) -> Option<FieldView<'a>> {
92 let raw = self.entries.get(idx)?.from_le();
93 Some(FieldView {
94 owner_def_index: raw.owner_def_index,
95 def_index: if raw.def_index == u32::MAX { None } else { Some(raw.def_index) },
96 name: self.arena.str_at(raw.name_offset),
97 ty: TyId(raw.ty),
98 field_idx: raw.field_idx,
99 vis: ItemVis::from_u8(raw.vis),
100 flags: raw.flags,
101 })
102 }
103}
104
105impl<'a> Iterator for FieldIter<'a> {
106 type Item = FieldView<'a>;
107
108 #[inline]
109 fn next(&mut self) -> Option<Self::Item> {
110 let (first, rest) = self.entries.split_first()?;
111 self.entries = rest;
112 let raw = first.from_le();
113 Some(FieldView {
114 owner_def_index: raw.owner_def_index,
115 def_index: if raw.def_index == u32::MAX { None } else { Some(raw.def_index) },
116 name: self.arena.str_at(raw.name_offset),
117 ty: TyId(raw.ty),
118 field_idx: raw.field_idx,
119 vis: ItemVis::from_u8(raw.vis),
120 flags: raw.flags,
121 })
122 }
123
124 #[inline]
125 fn size_hint(&self) -> (usize, Option<usize>) {
126 (self.entries.len(), Some(self.entries.len()))
127 }
128}
129
130impl<'a> ExactSizeIterator for FieldIter<'a> {}
131
132#[derive(Debug, Clone, Copy, PartialEq, Eq)]
134pub struct DefTyView {
135 entry: DefTyEntry,
136}
137
138impl DefTyView {
139 #[inline]
140 pub fn new(raw: DefTyEntry) -> Self {
141 Self { entry: raw.from_le() }
142 }
143
144 #[inline]
145 pub const fn def_index(&self) -> u32 {
146 self.entry.def_index
147 }
148
149 #[inline]
150 pub const fn ty(&self) -> TyId {
151 TyId(self.entry.ty)
152 }
153
154 #[inline]
155 pub const fn parent_def_index(&self) -> Option<u32> {
156 if self.entry.parent_def_index == u32::MAX {
157 None
158 } else {
159 Some(self.entry.parent_def_index)
160 }
161 }
162
163 #[inline]
164 pub const fn variant_idx(&self) -> Option<u16> {
165 if self.entry.variant_idx == u16::MAX {
166 None
167 } else {
168 Some(self.entry.variant_idx)
169 }
170 }
171
172 #[inline]
173 pub const fn ctor_shape(&self) -> CtorShape {
174 CtorShape::from_u8(self.entry.ctor_shape)
175 }
176
177 #[inline]
178 pub const fn flags(&self) -> u8 {
179 self.entry.flags
180 }
181
182 #[inline]
183 pub const fn is_non_exhaustive(&self) -> bool {
184 (self.entry.flags & DEF_TY_FLAG_NON_EXHAUSTIVE) != 0
185 }
186
187 #[inline]
188 pub const fn is_mut_static(&self) -> bool {
189 (self.entry.flags & DEF_TY_FLAG_MUT_STATIC) != 0
190 }
191
192 #[inline]
193 pub const fn is_unsafe_static(&self) -> bool {
194 (self.entry.flags & DEF_TY_FLAG_UNSAFE_STATIC) != 0
195 }
196
197 #[inline]
198 pub const fn is_fundamental(&self) -> bool {
199 (self.entry.flags & DEF_TY_FLAG_FUNDAMENTAL) != 0
200 }
201
202 #[inline]
203 pub const fn is_default_assoc_ty(&self) -> bool {
204 (self.entry.flags & DEF_TY_FLAG_DEFAULT_ASSOC) != 0
205 }
206}