ridx_format/format/
field_item.rs1use super::TyId;
15
16pub const FIELD_FLAG_TUPLE: u8 = 1 << 0;
18pub const FIELD_FLAG_UNSAFE: u8 = 1 << 1;
20
21pub const DEF_TY_FLAG_NON_EXHAUSTIVE: u8 = 1 << 0;
23pub const DEF_TY_FLAG_MUT_STATIC: u8 = 1 << 1;
25pub const DEF_TY_FLAG_UNSAFE_STATIC: u8 = 1 << 2;
27pub const DEF_TY_FLAG_FUNDAMENTAL: u8 = 1 << 3;
29pub const DEF_TY_FLAG_DEFAULT_ASSOC: u8 = 1 << 4;
31
32#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
34#[repr(u8)]
35pub enum ItemVis {
36 #[default]
38 Public = 0,
39 Crate = 1,
41 Restricted = 2,
43 Private = 3,
45}
46
47impl ItemVis {
48 #[inline]
49 pub const fn from_u8(v: u8) -> Self {
50 match v {
51 0 => Self::Public,
52 1 => Self::Crate,
53 2 => Self::Restricted,
54 _ => Self::Private,
55 }
56 }
57
58 #[inline]
59 pub const fn is_public(self) -> bool {
60 matches!(self, Self::Public)
61 }
62
63 #[inline]
64 pub const fn is_crate_visible(self) -> bool {
65 matches!(self, Self::Public | Self::Crate)
66 }
67}
68
69#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
71#[repr(u8)]
72pub enum CtorShape {
73 #[default]
75 None = 0,
76 Record = 1,
78 Tuple = 2,
80 Unit = 3,
82}
83
84impl CtorShape {
85 #[inline]
86 pub const fn from_u8(v: u8) -> Self {
87 match v {
88 1 => Self::Record,
89 2 => Self::Tuple,
90 3 => Self::Unit,
91 _ => Self::None,
92 }
93 }
94}
95
96#[repr(C)]
101#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
102pub struct FieldEntry {
103 pub owner_def_index: u32,
105 pub def_index: u32,
107 pub name_offset: u32,
109 pub ty: u32,
111 pub field_idx: u16,
113 pub vis: u8,
115 pub flags: u8,
117}
118
119impl FieldEntry {
120 #[inline]
121 pub const fn to_le(self) -> Self {
122 Self {
123 owner_def_index: self.owner_def_index.to_le(),
124 def_index: self.def_index.to_le(),
125 name_offset: self.name_offset.to_le(),
126 ty: self.ty.to_le(),
127 field_idx: self.field_idx.to_le(),
128 vis: self.vis,
129 flags: self.flags,
130 }
131 }
132
133 #[inline]
134 pub const fn from_le(self) -> Self {
135 Self {
136 owner_def_index: u32::from_le(self.owner_def_index),
137 def_index: u32::from_le(self.def_index),
138 name_offset: u32::from_le(self.name_offset),
139 ty: u32::from_le(self.ty),
140 field_idx: u16::from_le(self.field_idx),
141 vis: self.vis,
142 flags: self.flags,
143 }
144 }
145
146 #[inline]
147 pub const fn owner_def_index(&self) -> u32 {
148 u32::from_le(self.owner_def_index)
149 }
150
151 #[inline]
152 pub const fn def_index_opt(&self) -> Option<u32> {
153 let idx = u32::from_le(self.def_index);
154 if idx == u32::MAX {
155 None
156 } else {
157 Some(idx)
158 }
159 }
160
161 #[inline]
162 pub const fn ty_id(&self) -> TyId {
163 TyId(u32::from_le(self.ty))
164 }
165
166 #[inline]
167 pub const fn field_idx(&self) -> u16 {
168 u16::from_le(self.field_idx)
169 }
170
171 #[inline]
172 pub const fn visibility(&self) -> ItemVis {
173 ItemVis::from_u8(self.vis)
174 }
175
176 #[inline]
177 pub const fn is_tuple_field(&self) -> bool {
178 (self.flags & FIELD_FLAG_TUPLE) != 0
179 }
180
181 #[inline]
182 pub const fn is_unsafe(&self) -> bool {
183 (self.flags & FIELD_FLAG_UNSAFE) != 0
184 }
185}
186
187#[repr(C)]
194#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
195pub struct DefTyEntry {
196 pub def_index: u32,
198 pub ty: u32,
200 pub parent_def_index: u32,
202 pub variant_idx: u16,
204 pub ctor_shape: u8,
206 pub flags: u8,
208}
209
210impl DefTyEntry {
211 #[inline]
212 pub const fn to_le(self) -> Self {
213 Self {
214 def_index: self.def_index.to_le(),
215 ty: self.ty.to_le(),
216 parent_def_index: self.parent_def_index.to_le(),
217 variant_idx: self.variant_idx.to_le(),
218 ctor_shape: self.ctor_shape,
219 flags: self.flags,
220 }
221 }
222
223 #[inline]
224 pub const fn from_le(self) -> Self {
225 Self {
226 def_index: u32::from_le(self.def_index),
227 ty: u32::from_le(self.ty),
228 parent_def_index: u32::from_le(self.parent_def_index),
229 variant_idx: u16::from_le(self.variant_idx),
230 ctor_shape: self.ctor_shape,
231 flags: self.flags,
232 }
233 }
234
235 #[inline]
236 pub const fn def_index(&self) -> u32 {
237 u32::from_le(self.def_index)
238 }
239
240 #[inline]
241 pub const fn ty_id(&self) -> TyId {
242 TyId(u32::from_le(self.ty))
243 }
244
245 #[inline]
246 pub const fn parent_def_index_opt(&self) -> Option<u32> {
247 let p = u32::from_le(self.parent_def_index);
248 if p == u32::MAX {
249 None
250 } else {
251 Some(p)
252 }
253 }
254
255 #[inline]
256 pub const fn variant_idx_opt(&self) -> Option<u16> {
257 let v = u16::from_le(self.variant_idx);
258 if v == u16::MAX {
259 None
260 } else {
261 Some(v)
262 }
263 }
264
265 #[inline]
266 pub const fn ctor_shape(&self) -> CtorShape {
267 CtorShape::from_u8(self.ctor_shape)
268 }
269}