Skip to main content

ridx_format/format/
lang_item.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//! On-disk `LangItemKind` catalog and `LangItemEntry` record for `.ridx` format v5 (1.2-G6).
12
13use super::ty_prim::TyDefId;
14
15/// Canonical compiler `#[lang = "..."]` item (and associated trait item) identifier.
16#[repr(u16)]
17#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
18pub enum LangItemKind {
19    // Core markers & fundamental ADTs
20    Sized = 0,
21    Copy = 1,
22    Clone = 2,
23    Sync = 3,
24    Freeze = 4,
25    Unpin = 5,
26    Drop = 6,
27    Tuple = 7,
28    FnPtrTrait = 8,
29    Option = 9,
30    OptionSome = 10,
31    OptionNone = 11,
32    Result = 12,
33    ResultOk = 13,
34    ResultErr = 14,
35    ControlFlow = 15,
36    ControlFlowContinue = 16,
37    ControlFlowBreak = 17,
38    Poll = 18,
39    PollReady = 19,
40    PollPending = 20,
41    Pin = 21,
42    OwnedBox = 22,
43    String = 23,
44    ManuallyDrop = 24,
45    MaybeUninit = 25,
46    UnsafeCell = 26,
47    PhantomData = 27,
48
49    // Range structs
50    RangeFull = 28,
51    Range = 29,
52    RangeFrom = 30,
53    RangeTo = 31,
54    RangeInclusive = 32,
55    RangeToInclusive = 33,
56
57    // `?` operator (`Try` v2 — unstable in `core`, bypassed into index)
58    Try = 34,
59    TryOutput = 35,
60    TryResidual = 36,
61    TryBranch = 37,
62    TryFromOutput = 38,
63    FromResidual = 39,
64    FromResidualFn = 40,
65    Residual = 41,
66    ResidualTryType = 42,
67
68    // Sync & async callables (`Fn*`, `AsyncFn*` — `call*` unstable in `core`)
69    Fn = 43,
70    FnCall = 44,
71    FnMut = 45,
72    FnMutCallMut = 46,
73    FnOnce = 47,
74    FnOnceCallOnce = 48,
75    FnOnceOutput = 49,
76    AsyncFn = 50,
77    AsyncFnCall = 51,
78    AsyncFnMut = 52,
79    AsyncFnMutCallMut = 53,
80    AsyncFnOnce = 54,
81    AsyncFnOnceCallOnce = 55,
82    AsyncFnOnceOutput = 56,
83    CallOnceFuture = 57,
84    CallRefFuture = 58,
85
86    // Autoderef, receiver & indexing
87    Deref = 59,
88    DerefTarget = 60,
89    DerefFn = 61,
90    DerefMut = 62,
91    DerefMutFn = 63,
92    Receiver = 64,
93    ReceiverTarget = 65,
94    Index = 66,
95    IndexOutput = 67,
96    IndexFn = 68,
97    IndexMut = 69,
98    IndexMutFn = 70,
99
100    // Iteration & `async`/`.await`
101    Iterator = 71,
102    IteratorItem = 72,
103    IteratorNext = 73,
104    IntoIterator = 74,
105    IntoIteratorItem = 75,
106    IntoIteratorIntoIter = 76,
107    IntoIteratorIntoIterFn = 77,
108    Future = 78,
109    FutureOutput = 79,
110    FuturePoll = 80,
111    IntoFuture = 81,
112    IntoFutureOutput = 82,
113    IntoFutureIntoFuture = 83,
114    IntoFutureIntoFutureFn = 84,
115    AsyncIterator = 85,
116    AsyncIteratorPollNext = 86,
117
118    // Operators & conversions
119    Add = 87,
120    Sub = 88,
121    Mul = 89,
122    Div = 90,
123    Rem = 91,
124    Neg = 92,
125    Not = 93,
126    BitXor = 94,
127    BitAnd = 95,
128    BitOr = 96,
129    Shl = 97,
130    Shr = 98,
131    AddAssign = 99,
132    SubAssign = 100,
133    MulAssign = 101,
134    DivAssign = 102,
135    RemAssign = 103,
136    BitXorAssign = 104,
137    BitAndAssign = 105,
138    BitOrAssign = 106,
139    ShlAssign = 107,
140    ShrAssign = 108,
141    PartialEq = 109,
142    PartialOrd = 110,
143    From = 111,
144
145    Other = u16::MAX,
146}
147
148impl LangItemKind {
149    pub const KNOWN_MAX: u16 = 111;
150
151    #[inline]
152    pub const fn from_u16(v: u16) -> Self {
153        if v <= Self::KNOWN_MAX {
154            // SAFETY: `LangItemKind` is `#[repr(u16)]` with contiguous variants `0..=111`.
155            unsafe { std::mem::transmute::<u16, Self>(v) }
156        } else {
157            Self::Other
158        }
159    }
160}
161
162/// Fixed-size 12-byte lang-item mapping row in `lang_items`, sorted by `kind`.
163#[repr(C)]
164#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
165pub struct LangItemEntry {
166    pub kind: u16,
167    pub _pad: u16,
168    pub ty_def: u32,
169    pub def_index: u32,
170}
171
172impl LangItemEntry {
173    #[inline]
174    pub const fn to_le(self) -> Self {
175        Self { kind: self.kind.to_le(), _pad: 0, ty_def: self.ty_def.to_le(), def_index: self.def_index.to_le() }
176    }
177
178    #[inline]
179    pub const fn from_le(self) -> Self {
180        Self { kind: u16::from_le(self.kind), _pad: 0, ty_def: u32::from_le(self.ty_def), def_index: u32::from_le(self.def_index) }
181    }
182
183    #[inline]
184    pub const fn kind_raw(&self) -> u16 {
185        u16::from_le(self.kind)
186    }
187
188    #[inline]
189    pub const fn lang_kind(&self) -> LangItemKind {
190        LangItemKind::from_u16(self.kind_raw())
191    }
192
193    #[inline]
194    pub const fn ty_def_id(&self) -> TyDefId {
195        TyDefId(u32::from_le(self.ty_def))
196    }
197
198    #[inline]
199    pub const fn def_index(&self) -> Option<u32> {
200        let idx = u32::from_le(self.def_index);
201        if idx == u32::MAX {
202            None
203        } else {
204            Some(idx)
205        }
206    }
207}