Skip to main content

ridx_format/format/
header.rs

1/*
2 * rust-intelligence — Ultra-fast, zero-memory code intelligence for Rust
3 * Copyright (c) 2026 Alexander Panasenko
4 *
5 * Contact: alex@prod.codes
6 * Author: https://prod.codes/about/
7 * Project: https://github.com/alex09x/rust-intelligence
8 * SPDX-License-Identifier: MIT OR Apache-2.0
9 */
10
11/// File table entry: string pool offset for file path, and 64-bit source text provenance hash.
12#[repr(C)]
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
14pub struct FileEntry {
15    pub path_offset: u32,
16    pub _pad: u32,
17    pub source_hash: u64,
18}
19
20impl FileEntry {
21    #[inline]
22    pub fn new(path_offset: u32, source_hash: u64) -> Self {
23        Self { path_offset, _pad: 0, source_hash }
24    }
25
26    #[inline]
27    pub fn path_offset(&self) -> u32 {
28        u32::from_le(self.path_offset)
29    }
30
31    #[inline]
32    pub fn source_hash(&self) -> u64 {
33        u64::from_le(self.source_hash)
34    }
35
36    #[inline]
37    pub fn to_le(self) -> Self {
38        Self { path_offset: self.path_offset.to_le(), _pad: 0, source_hash: self.source_hash.to_le() }
39    }
40}
41
42/// Fixed-size header at offset 0 of any .ridx file (format version 5, 416 bytes; v4 is 176, v3 is 160).
43#[repr(C)]
44#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
45pub struct Header {
46    /// Magic signature ("RIDX")
47    pub magic: [u8; 4],
48    /// Format version (currently 5)
49    pub version: u32,
50    /// Offset in string pool for crate name
51    pub crate_name_offset: u32,
52    /// Offset in string pool for crate version
53    pub crate_version_offset: u32,
54
55    /// Offset of the string intern pool
56    pub string_pool_offset: u64,
57    /// Length in bytes of the string intern pool
58    pub string_pool_len: u64,
59
60    /// Offset of the file path table (array of FileEntry)
61    pub files_offset: u64,
62    pub files_count: u32,
63    /// Offset in string pool for compilation unit key (or 0)
64    pub unit_key_offset: u32,
65
66    /// Offset of the definitions table (array of DefEntry)
67    pub defs_offset: u64,
68    pub defs_count: u32,
69    /// Offset in string pool for source snapshot generation (or 0)
70    pub generation_offset: u32,
71
72    /// Offset of the definitions hash index (array of HashIndexEntry sorted by hash)
73    pub defs_by_hash_offset: u64,
74    pub defs_by_hash_count: u32,
75    pub _pad2: u32,
76
77    /// Offset of the references table (array of RefEntry sorted by span)
78    pub refs_offset: u64,
79    pub refs_count: u32,
80    pub _pad3: u32,
81
82    /// Offset of the references-by-target hash index (array of HashIndexEntry sorted by target hash)
83    pub refs_by_target_offset: u64,
84    pub refs_by_target_count: u32,
85    pub _pad4: u32,
86
87    /// Offset of the types table (array of TypeEntry sorted by span)
88    pub types_offset: u64,
89    pub types_count: u32,
90    pub _pad5: u32,
91
92    /// Offset of the definitions-by-owner table (array of OwnerIndexEntry sorted by hash)
93    pub defs_by_owner_offset: u64,
94    pub defs_by_owner_count: u32,
95    pub _pad6: u32,
96
97    /// Offset of the deref mapping table (array of DerefEntry sorted by source_type_hash)
98    pub derefs_offset: u64,
99    pub derefs_count: u32,
100    pub _pad7: u32,
101
102    /// Offset of the notes table (array of NoteEntry sorted by span); version 4.
103    pub notes_offset: u64,
104    pub notes_count: u32,
105    pub _pad8: u32,
106
107    /// Offset of the `TyArena` definition table (`TyDefEntry`); version 5.
108    pub ty_defs_offset: u64,
109    pub ty_defs_count: u32,
110    pub _pad9: u32,
111
112    /// Offset of the `TyArena` interned type table (`TyEntry`); version 5.
113    pub ty_entries_offset: u64,
114    pub ty_entries_count: u32,
115    pub _pad10: u32,
116
117    /// Offset of the `TyArena` trait bound table (`TyBoundEntry`); version 5.
118    pub ty_bounds_offset: u64,
119    pub ty_bounds_count: u32,
120    pub _pad11: u32,
121
122    /// Offset of the `TyArena` packed generic argument pool (`u32`); version 5.
123    pub ty_args_offset: u64,
124    pub ty_args_count: u32,
125    pub _pad12: u32,
126
127    /// Offset of the structured `FnSigEntry` table (sorted by `def_index`); version 5.
128    pub fn_sigs_offset: u64,
129    pub fn_sigs_count: u32,
130    pub _pad13: u32,
131
132    /// Offset of the structured `ImplEntry` table (sorted by `simplified_self`); version 5.
133    pub impls_offset: u64,
134    pub impls_count: u32,
135    pub _pad14: u32,
136
137    /// Offset of the `AssocTyEntry` table (contiguous per `ImplEntry`); version 5.
138    pub assoc_tys_offset: u64,
139    pub assoc_tys_count: u32,
140    pub _pad15: u32,
141
142    /// Offset of the `FieldEntry` table (sorted by `(owner_def_index, field_idx)`); version 5.
143    pub fields_offset: u64,
144    pub fields_count: u32,
145    pub _pad16: u32,
146
147    /// Offset of the `DefTyEntry` table (sorted by `def_index`); version 5.
148    pub def_tys_offset: u64,
149    pub def_tys_count: u32,
150    pub _pad17: u32,
151
152    /// Offset of the `GenericParamEntry` table (sorted by `(owner_key, param_idx)`); version 5.
153    pub generic_params_offset: u64,
154    pub generic_params_count: u32,
155    pub _pad18: u32,
156
157    /// Offset of the `WherePredEntry` table (sorted by `owner_key`); version 5.
158    pub where_preds_offset: u64,
159    pub where_preds_count: u32,
160    pub _pad19: u32,
161
162    /// Offset of the `LangItemEntry` table (sorted by `kind`); version 5.
163    pub lang_items_offset: u64,
164    pub lang_items_count: u32,
165    pub _pad20: u32,
166
167    /// Offset of the `UseEntry` table (sorted by `(owner_raw, name_str)`); version 5.
168    pub uses_offset: u64,
169    pub uses_count: u32,
170    pub _pad21: u32,
171
172    /// Offset of the `CfgEntry` table (sorted by `(key_str, Option<val_str>)`); version 5.
173    pub cfgs_offset: u64,
174    pub cfgs_count: u32,
175    /// Offset in string pool for effective target triple (or 0); version 5.
176    pub target_offset: u32,
177
178    /// Offset of the `MacroDefEntry` table (sorted by `def_index`); version 5.
179    pub macro_defs_offset: u64,
180    pub macro_defs_count: u32,
181    pub _pad22: u32,
182}
183
184impl Header {
185    #[inline]
186    pub fn to_le(self) -> Self {
187        Self {
188            magic: self.magic,
189            version: self.version.to_le(),
190            crate_name_offset: self.crate_name_offset.to_le(),
191            crate_version_offset: self.crate_version_offset.to_le(),
192            string_pool_offset: self.string_pool_offset.to_le(),
193            string_pool_len: self.string_pool_len.to_le(),
194            files_offset: self.files_offset.to_le(),
195            files_count: self.files_count.to_le(),
196            unit_key_offset: self.unit_key_offset.to_le(),
197            defs_offset: self.defs_offset.to_le(),
198            defs_count: self.defs_count.to_le(),
199            generation_offset: self.generation_offset.to_le(),
200            defs_by_hash_offset: self.defs_by_hash_offset.to_le(),
201            defs_by_hash_count: self.defs_by_hash_count.to_le(),
202            _pad2: 0,
203            refs_offset: self.refs_offset.to_le(),
204            refs_count: self.refs_count.to_le(),
205            _pad3: 0,
206            refs_by_target_offset: self.refs_by_target_offset.to_le(),
207            refs_by_target_count: self.refs_by_target_count.to_le(),
208            _pad4: 0,
209            types_offset: self.types_offset.to_le(),
210            types_count: self.types_count.to_le(),
211            _pad5: 0,
212            defs_by_owner_offset: self.defs_by_owner_offset.to_le(),
213            defs_by_owner_count: self.defs_by_owner_count.to_le(),
214            _pad6: 0,
215            derefs_offset: self.derefs_offset.to_le(),
216            derefs_count: self.derefs_count.to_le(),
217            _pad7: 0,
218            notes_offset: self.notes_offset.to_le(),
219            notes_count: self.notes_count.to_le(),
220            _pad8: 0,
221            ty_defs_offset: self.ty_defs_offset.to_le(),
222            ty_defs_count: self.ty_defs_count.to_le(),
223            _pad9: 0,
224            ty_entries_offset: self.ty_entries_offset.to_le(),
225            ty_entries_count: self.ty_entries_count.to_le(),
226            _pad10: 0,
227            ty_bounds_offset: self.ty_bounds_offset.to_le(),
228            ty_bounds_count: self.ty_bounds_count.to_le(),
229            _pad11: 0,
230            ty_args_offset: self.ty_args_offset.to_le(),
231            ty_args_count: self.ty_args_count.to_le(),
232            _pad12: 0,
233            fn_sigs_offset: self.fn_sigs_offset.to_le(),
234            fn_sigs_count: self.fn_sigs_count.to_le(),
235            _pad13: 0,
236            impls_offset: self.impls_offset.to_le(),
237            impls_count: self.impls_count.to_le(),
238            _pad14: 0,
239            assoc_tys_offset: self.assoc_tys_offset.to_le(),
240            assoc_tys_count: self.assoc_tys_count.to_le(),
241            _pad15: 0,
242            fields_offset: self.fields_offset.to_le(),
243            fields_count: self.fields_count.to_le(),
244            _pad16: 0,
245            def_tys_offset: self.def_tys_offset.to_le(),
246            def_tys_count: self.def_tys_count.to_le(),
247            _pad17: 0,
248            generic_params_offset: self.generic_params_offset.to_le(),
249            generic_params_count: self.generic_params_count.to_le(),
250            _pad18: 0,
251            where_preds_offset: self.where_preds_offset.to_le(),
252            where_preds_count: self.where_preds_count.to_le(),
253            _pad19: 0,
254            lang_items_offset: self.lang_items_offset.to_le(),
255            lang_items_count: self.lang_items_count.to_le(),
256            _pad20: 0,
257            uses_offset: self.uses_offset.to_le(),
258            uses_count: self.uses_count.to_le(),
259            _pad21: 0,
260            cfgs_offset: self.cfgs_offset.to_le(),
261            cfgs_count: self.cfgs_count.to_le(),
262            target_offset: self.target_offset.to_le(),
263            macro_defs_offset: self.macro_defs_offset.to_le(),
264            macro_defs_count: self.macro_defs_count.to_le(),
265            _pad22: 0,
266        }
267    }
268
269    #[inline]
270    pub fn from_le(self) -> Self {
271        Self {
272            magic: self.magic,
273            version: u32::from_le(self.version),
274            crate_name_offset: u32::from_le(self.crate_name_offset),
275            crate_version_offset: u32::from_le(self.crate_version_offset),
276            string_pool_offset: u64::from_le(self.string_pool_offset),
277            string_pool_len: u64::from_le(self.string_pool_len),
278            files_offset: u64::from_le(self.files_offset),
279            files_count: u32::from_le(self.files_count),
280            unit_key_offset: u32::from_le(self.unit_key_offset),
281            defs_offset: u64::from_le(self.defs_offset),
282            defs_count: u32::from_le(self.defs_count),
283            generation_offset: u32::from_le(self.generation_offset),
284            defs_by_hash_offset: u64::from_le(self.defs_by_hash_offset),
285            defs_by_hash_count: u32::from_le(self.defs_by_hash_count),
286            _pad2: 0,
287            refs_offset: u64::from_le(self.refs_offset),
288            refs_count: u32::from_le(self.refs_count),
289            _pad3: 0,
290            refs_by_target_offset: u64::from_le(self.refs_by_target_offset),
291            refs_by_target_count: u32::from_le(self.refs_by_target_count),
292            _pad4: 0,
293            types_offset: u64::from_le(self.types_offset),
294            types_count: u32::from_le(self.types_count),
295            _pad5: 0,
296            defs_by_owner_offset: u64::from_le(self.defs_by_owner_offset),
297            defs_by_owner_count: u32::from_le(self.defs_by_owner_count),
298            _pad6: 0,
299            derefs_offset: u64::from_le(self.derefs_offset),
300            derefs_count: u32::from_le(self.derefs_count),
301            _pad7: 0,
302            notes_offset: u64::from_le(self.notes_offset),
303            notes_count: u32::from_le(self.notes_count),
304            _pad8: 0,
305            ty_defs_offset: u64::from_le(self.ty_defs_offset),
306            ty_defs_count: u32::from_le(self.ty_defs_count),
307            _pad9: 0,
308            ty_entries_offset: u64::from_le(self.ty_entries_offset),
309            ty_entries_count: u32::from_le(self.ty_entries_count),
310            _pad10: 0,
311            ty_bounds_offset: u64::from_le(self.ty_bounds_offset),
312            ty_bounds_count: u32::from_le(self.ty_bounds_count),
313            _pad11: 0,
314            ty_args_offset: u64::from_le(self.ty_args_offset),
315            ty_args_count: u32::from_le(self.ty_args_count),
316            _pad12: 0,
317            fn_sigs_offset: u64::from_le(self.fn_sigs_offset),
318            fn_sigs_count: u32::from_le(self.fn_sigs_count),
319            _pad13: 0,
320            impls_offset: u64::from_le(self.impls_offset),
321            impls_count: u32::from_le(self.impls_count),
322            _pad14: 0,
323            assoc_tys_offset: u64::from_le(self.assoc_tys_offset),
324            assoc_tys_count: u32::from_le(self.assoc_tys_count),
325            _pad15: 0,
326            fields_offset: u64::from_le(self.fields_offset),
327            fields_count: u32::from_le(self.fields_count),
328            _pad16: 0,
329            def_tys_offset: u64::from_le(self.def_tys_offset),
330            def_tys_count: u32::from_le(self.def_tys_count),
331            _pad17: 0,
332            generic_params_offset: u64::from_le(self.generic_params_offset),
333            generic_params_count: u32::from_le(self.generic_params_count),
334            _pad18: 0,
335            where_preds_offset: u64::from_le(self.where_preds_offset),
336            where_preds_count: u32::from_le(self.where_preds_count),
337            _pad19: 0,
338            lang_items_offset: u64::from_le(self.lang_items_offset),
339            lang_items_count: u32::from_le(self.lang_items_count),
340            _pad20: 0,
341            uses_offset: u64::from_le(self.uses_offset),
342            uses_count: u32::from_le(self.uses_count),
343            _pad21: 0,
344            cfgs_offset: u64::from_le(self.cfgs_offset),
345            cfgs_count: u32::from_le(self.cfgs_count),
346            target_offset: u32::from_le(self.target_offset),
347            macro_defs_offset: u64::from_le(self.macro_defs_offset),
348            macro_defs_count: u32::from_le(self.macro_defs_count),
349            _pad22: 0,
350        }
351    }
352
353    #[inline]
354    pub fn target_offset(&self) -> u32 {
355        u32::from_le(self.target_offset)
356    }
357
358    #[inline]
359    pub fn unit_key_offset(&self) -> u32 {
360        u32::from_le(self.unit_key_offset)
361    }
362
363    #[inline]
364    pub fn generation_offset(&self) -> u32 {
365        u32::from_le(self.generation_offset)
366    }
367}