Skip to main content

ridx_format/format/
entries.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
11use super::{Span, FLAG_MACRO_EXPANSION};
12
13/// A reference/usage record pointing to a target definition or symbol.
14#[repr(C)]
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
16pub struct RefEntry {
17    /// Source span of this reference usage
18    pub span: Span,
19    /// Target definition ID if resolved within the same crate, or u32::MAX for external
20    pub target_def_id: u32,
21    /// Target canonical path offset in string pool (e.g. "std::convert::Into")
22    pub target_path_offset: u32,
23    /// Target source file path offset in string pool, or 0 if external/unknown
24    pub target_file_offset: u32,
25    /// Target compilation unit key offset in string pool, or 0 if unknown
26    pub target_unit_offset: u32,
27    /// Target definition line (1-based), or 0
28    pub target_line: u32,
29    /// Target definition col (1-based), or 0
30    pub target_col: u32,
31    /// Kind of symbol being referenced
32    pub kind: u8,
33    /// Usage flags: 1 = write/mutable, 0 = read/call
34    pub is_write: u8,
35    /// Flags (bit 0: is_macro_expansion)
36    pub flags: u8,
37    pub _pad: u8,
38}
39
40impl RefEntry {
41    #[inline]
42    pub fn to_le(self) -> Self {
43        Self {
44            span: self.span.to_le(),
45            target_def_id: self.target_def_id.to_le(),
46            target_path_offset: self.target_path_offset.to_le(),
47            target_file_offset: self.target_file_offset.to_le(),
48            target_unit_offset: self.target_unit_offset.to_le(),
49            target_line: self.target_line.to_le(),
50            target_col: self.target_col.to_le(),
51            kind: self.kind,
52            is_write: self.is_write,
53            flags: self.flags,
54            _pad: self._pad,
55        }
56    }
57
58    #[inline]
59    pub fn from_le(self) -> Self {
60        Self {
61            span: self.span.from_le(),
62            target_def_id: u32::from_le(self.target_def_id),
63            target_path_offset: u32::from_le(self.target_path_offset),
64            target_file_offset: u32::from_le(self.target_file_offset),
65            target_unit_offset: u32::from_le(self.target_unit_offset),
66            target_line: u32::from_le(self.target_line),
67            target_col: u32::from_le(self.target_col),
68            kind: self.kind,
69            is_write: self.is_write,
70            flags: self.flags,
71            _pad: self._pad,
72        }
73    }
74
75    #[inline]
76    pub fn span(&self) -> Span {
77        self.span.from_le()
78    }
79    #[inline]
80    pub fn target_def_id(&self) -> u32 {
81        u32::from_le(self.target_def_id)
82    }
83    #[inline]
84    pub fn target_path_offset(&self) -> u32 {
85        u32::from_le(self.target_path_offset)
86    }
87    #[inline]
88    pub fn target_file_offset(&self) -> u32 {
89        u32::from_le(self.target_file_offset)
90    }
91    #[inline]
92    pub fn target_unit_offset(&self) -> u32 {
93        u32::from_le(self.target_unit_offset)
94    }
95    #[inline]
96    pub fn target_line(&self) -> u32 {
97        u32::from_le(self.target_line)
98    }
99    #[inline]
100    pub fn target_col(&self) -> u32 {
101        u32::from_le(self.target_col)
102    }
103    #[inline]
104    pub fn is_macro_expansion(&self) -> bool {
105        (self.flags & FLAG_MACRO_EXPANSION) != 0
106    }
107}
108
109/// Inferred expression type record.
110#[repr(C)]
111#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
112pub struct TypeEntry {
113    /// Source span of the expression
114    pub span: Span,
115    /// Offset in string pool of the inferred type name (e.g. "Option<JoinHandle<T>>")
116    pub type_offset: u32,
117    /// Offset in string pool of the canonical owner path (e.g. "tokio::fs::File"), or 0
118    pub owner_path_offset: u32,
119}
120
121impl TypeEntry {
122    #[inline]
123    pub fn new(span: Span, type_offset: u32, owner_path_offset: u32) -> Self {
124        Self { span, type_offset, owner_path_offset }
125    }
126
127    #[inline]
128    pub fn to_le(self) -> Self {
129        Self { span: self.span.to_le(), type_offset: self.type_offset.to_le(), owner_path_offset: self.owner_path_offset.to_le() }
130    }
131
132    #[inline]
133    pub fn from_le(self) -> Self {
134        Self { span: self.span.from_le(), type_offset: u32::from_le(self.type_offset), owner_path_offset: u32::from_le(self.owner_path_offset) }
135    }
136
137    #[inline]
138    pub fn span(&self) -> Span {
139        self.span.from_le()
140    }
141    #[inline]
142    pub fn type_offset(&self) -> u32 {
143        u32::from_le(self.type_offset)
144    }
145    #[inline]
146    pub fn owner_path_offset(&self) -> u32 {
147        u32::from_le(self.owner_path_offset)
148    }
149}
150
151/// Hash table entry for $O(\log N)$ or binary search lookup by string hash.
152#[repr(C)]
153#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
154pub struct HashIndexEntry {
155    /// 64-bit FNV-1a hash of the string (path or symbol)
156    pub hash: u64,
157    /// Index into the corresponding records table (defs or refs)
158    pub index: u32,
159    pub _pad: [u8; 4],
160}
161
162impl HashIndexEntry {
163    #[inline]
164    pub fn to_le(self) -> Self {
165        Self { hash: self.hash.to_le(), index: self.index.to_le(), _pad: self._pad }
166    }
167
168    #[inline]
169    pub fn from_le(self) -> Self {
170        Self { hash: u64::from_le(self.hash), index: u32::from_le(self.index), _pad: self._pad }
171    }
172
173    #[inline]
174    pub fn hash(&self) -> u64 {
175        u64::from_le(self.hash)
176    }
177    #[inline]
178    pub fn index(&self) -> u32 {
179        u32::from_le(self.index)
180    }
181}
182
183/// Definition owner index entry for fast lookup of methods, fields, and associated items by owner.
184#[repr(C)]
185#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
186pub struct OwnerIndexEntry {
187    /// 64-bit FNV-1a hash of the canonical owner path (e.g. "tokio::fs::File")
188    pub hash: u64,
189    /// Offset in string pool for the canonical owner path
190    pub owner_path_offset: u32,
191    /// Index into definitions table
192    pub def_index: u32,
193}
194
195impl OwnerIndexEntry {
196    #[inline]
197    pub fn new(hash: u64, owner_path_offset: u32, def_index: u32) -> Self {
198        Self { hash, owner_path_offset, def_index }
199    }
200
201    #[inline]
202    pub fn to_le(self) -> Self {
203        Self { hash: self.hash.to_le(), owner_path_offset: self.owner_path_offset.to_le(), def_index: self.def_index.to_le() }
204    }
205
206    #[inline]
207    pub fn from_le(self) -> Self {
208        Self { hash: u64::from_le(self.hash), owner_path_offset: u32::from_le(self.owner_path_offset), def_index: u32::from_le(self.def_index) }
209    }
210
211    #[inline]
212    pub fn hash(&self) -> u64 {
213        u64::from_le(self.hash)
214    }
215    #[inline]
216    pub fn owner_path_offset(&self) -> u32 {
217        u32::from_le(self.owner_path_offset)
218    }
219    #[inline]
220    pub fn def_index(&self) -> u32 {
221        u32::from_le(self.def_index)
222    }
223}
224
225/// Deref target mapping entry for resolving autoderef chains (e.g. `String` -> `str`).
226#[repr(C)]
227#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
228pub struct DerefEntry {
229    /// 64-bit FNV-1a hash of the source type canonical path (e.g. "alloc::string::String")
230    pub source_type_hash: u64,
231    /// Offset in string pool for source type canonical path
232    pub source_type_offset: u32,
233    /// Offset in string pool for target type canonical path (e.g. "core::primitive::str")
234    pub target_type_offset: u32,
235}
236
237impl DerefEntry {
238    #[inline]
239    pub fn new(source_type_hash: u64, source_type_offset: u32, target_type_offset: u32) -> Self {
240        Self { source_type_hash, source_type_offset, target_type_offset }
241    }
242
243    #[inline]
244    pub fn to_le(self) -> Self {
245        Self { source_type_hash: self.source_type_hash.to_le(), source_type_offset: self.source_type_offset.to_le(), target_type_offset: self.target_type_offset.to_le() }
246    }
247
248    #[inline]
249    pub fn from_le(self) -> Self {
250        Self {
251            source_type_hash: u64::from_le(self.source_type_hash),
252            source_type_offset: u32::from_le(self.source_type_offset),
253            target_type_offset: u32::from_le(self.target_type_offset),
254        }
255    }
256
257    #[inline]
258    pub fn source_type_hash(&self) -> u64 {
259        u64::from_le(self.source_type_hash)
260    }
261    #[inline]
262    pub fn source_type_offset(&self) -> u32 {
263        u32::from_le(self.source_type_offset)
264    }
265    #[inline]
266    pub fn target_type_offset(&self) -> u32 {
267        u32::from_le(self.target_type_offset)
268    }
269}