Skip to main content

ridx_format/format/
use_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 `UseOwner`, `UsePrimTy`, flags, and 28-byte `UseEntry` record for `.ridx` format v5 (1.2-G7).
12
13use super::field_item::ItemVis;
14use super::ty_prim::TyDefId;
15
16/// Scope owning a `UseEntry` (`CrateRoot` = `0`, `Def(def_index)` = `def_index + 1`).
17#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
18pub enum UseOwner {
19    /// Crate root module (`lib.rs` / `main.rs`, or `core` / `alloc` / `std` root).
20    CrateRoot,
21    /// Enclosing `DefEntry` index (`DefKind::Mod` for submodules, or item/fn scope for local `use`).
22    Def(u32),
23}
24
25impl UseOwner {
26    #[inline]
27    pub const fn to_raw(self) -> u32 {
28        match self {
29            Self::CrateRoot => 0,
30            Self::Def(idx) => idx.saturating_add(1),
31        }
32    }
33
34    #[inline]
35    pub const fn from_raw(raw: u32) -> Self {
36        if raw == 0 {
37            Self::CrateRoot
38        } else {
39            Self::Def(raw - 1)
40        }
41    }
42}
43
44/// Built-in primitive target for `use core::primitive::*` / `std::primitive::*` imports.
45#[repr(u8)]
46#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
47pub enum UsePrimTy {
48    #[default]
49    None = 0,
50    Bool = 1,
51    Char = 2,
52    Str = 3,
53    I8 = 4,
54    I16 = 5,
55    I32 = 6,
56    I64 = 7,
57    I128 = 8,
58    Isize = 9,
59    U8 = 10,
60    U16 = 11,
61    U32 = 12,
62    U64 = 13,
63    U128 = 14,
64    Usize = 15,
65    F16 = 16,
66    F32 = 17,
67    F64 = 18,
68    F128 = 19,
69}
70
71impl UsePrimTy {
72    #[inline]
73    pub const fn from_u8(v: u8) -> Self {
74        if v <= 19 {
75            // SAFETY: `UsePrimTy` is `#[repr(u8)]` with contiguous variants `0..=19`.
76            unsafe { std::mem::transmute::<u8, Self>(v) }
77        } else {
78            Self::None
79        }
80    }
81
82    #[inline]
83    pub const fn as_str(self) -> Option<&'static str> {
84        match self {
85            Self::None => None,
86            Self::Bool => Some("bool"),
87            Self::Char => Some("char"),
88            Self::Str => Some("str"),
89            Self::I8 => Some("i8"),
90            Self::I16 => Some("i16"),
91            Self::I32 => Some("i32"),
92            Self::I64 => Some("i64"),
93            Self::I128 => Some("i128"),
94            Self::Isize => Some("isize"),
95            Self::U8 => Some("u8"),
96            Self::U16 => Some("u16"),
97            Self::U32 => Some("u32"),
98            Self::U64 => Some("u64"),
99            Self::U128 => Some("u128"),
100            Self::Usize => Some("usize"),
101            Self::F16 => Some("f16"),
102            Self::F32 => Some("f32"),
103            Self::F64 => Some("f64"),
104            Self::F128 => Some("f128"),
105        }
106    }
107}
108
109/// Glob import or glob re-export (`use path::*;`, `name == "*"`, target `Mod`/`Enum` in `type_def`).
110pub const USE_FLAG_GLOB: u8 = 1 << 0;
111/// Exported / re-exported binding (`pub use`, `pub(crate) use`, `ModChild` re-export, or `#[macro_export]`).
112pub const USE_FLAG_REEXPORT: u8 = 1 << 1;
113/// `extern crate foo [as bar];` binding (`type_def` points to target crate root `Mod`).
114pub const USE_FLAG_EXTERN_CRATE: u8 = 1 << 2;
115/// Anonymous trait import (`use Trait as _;`, `name == "_"`).
116pub const USE_FLAG_UNDERSCORE: u8 = 1 << 3;
117/// `prim_tag` carries a non-zero `UsePrimTy` in the type namespace.
118pub const USE_FLAG_PRIM_TY: u8 = 1 << 4;
119/// Compiler-injected `#[prelude_import]` crate-wide prelude glob (`std::prelude::rust_2021::*`, etc.).
120pub const USE_FLAG_PRELUDE: u8 = 1 << 5;
121
122/// Fixed-size 28-byte module `use` / glob / re-export / `extern crate` row in `uses`,
123/// sorted by `(owner_raw, name_str, ...)` for O(log N) lookup by owner and identifier.
124#[repr(C)]
125#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
126pub struct UseEntry {
127    pub owner_raw: u32,
128    pub def_index: u32,
129    pub name_offset: u32,
130    pub type_def: u32,
131    pub value_def: u32,
132    pub macro_def: u32,
133    pub vis: u8,
134    pub flags: u8,
135    pub prim_tag: u8,
136    pub _pad: u8,
137}
138
139impl UseEntry {
140    #[inline]
141    pub const fn to_le(self) -> Self {
142        Self {
143            owner_raw: self.owner_raw.to_le(),
144            def_index: self.def_index.to_le(),
145            name_offset: self.name_offset.to_le(),
146            type_def: self.type_def.to_le(),
147            value_def: self.value_def.to_le(),
148            macro_def: self.macro_def.to_le(),
149            vis: self.vis,
150            flags: self.flags,
151            prim_tag: self.prim_tag,
152            _pad: 0,
153        }
154    }
155
156    #[inline]
157    pub const fn from_le(self) -> Self {
158        Self {
159            owner_raw: u32::from_le(self.owner_raw),
160            def_index: u32::from_le(self.def_index),
161            name_offset: u32::from_le(self.name_offset),
162            type_def: u32::from_le(self.type_def),
163            value_def: u32::from_le(self.value_def),
164            macro_def: u32::from_le(self.macro_def),
165            vis: self.vis,
166            flags: self.flags,
167            prim_tag: self.prim_tag,
168            _pad: 0,
169        }
170    }
171
172    #[inline]
173    pub const fn owner_raw(&self) -> u32 {
174        u32::from_le(self.owner_raw)
175    }
176
177    #[inline]
178    pub const fn owner(&self) -> UseOwner {
179        UseOwner::from_raw(self.owner_raw())
180    }
181
182    #[inline]
183    pub const fn def_index(&self) -> Option<u32> {
184        let v = u32::from_le(self.def_index);
185        if v == u32::MAX {
186            None
187        } else {
188            Some(v)
189        }
190    }
191
192    #[inline]
193    pub const fn name_offset(&self) -> u32 {
194        u32::from_le(self.name_offset)
195    }
196
197    #[inline]
198    pub const fn type_def(&self) -> Option<TyDefId> {
199        let v = u32::from_le(self.type_def);
200        if v == u32::MAX {
201            None
202        } else {
203            Some(TyDefId(v))
204        }
205    }
206
207    #[inline]
208    pub const fn value_def(&self) -> Option<TyDefId> {
209        let v = u32::from_le(self.value_def);
210        if v == u32::MAX {
211            None
212        } else {
213            Some(TyDefId(v))
214        }
215    }
216
217    #[inline]
218    pub const fn macro_def(&self) -> Option<TyDefId> {
219        let v = u32::from_le(self.macro_def);
220        if v == u32::MAX {
221            None
222        } else {
223            Some(TyDefId(v))
224        }
225    }
226
227    #[inline]
228    pub const fn visibility(&self) -> ItemVis {
229        ItemVis::from_u8(self.vis)
230    }
231
232    #[inline]
233    pub const fn prim_ty(&self) -> UsePrimTy {
234        UsePrimTy::from_u8(self.prim_tag)
235    }
236}