Skip to main content

ridx_format/format/
impl_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 `#[repr(C)]` layout for structured `impl` headers (`ImplEntry`) and
12//! associated type bindings (`AssocTyEntry`) in `.ridx` format version 5.
13
14use super::ty::{TyEntry, TyTag};
15use super::ty_prim::TyMutability;
16
17/// `ImplEntry.flags`: `impl Trait for Self` (unset for inherent `impl Self`).
18pub const IMPL_FLAG_TRAIT: u8 = 1 << 0;
19/// `ImplEntry.flags`: negative polarity (`impl !Send for Self`).
20pub const IMPL_FLAG_NEGATIVE: u8 = 1 << 1;
21/// `ImplEntry.flags`: `unsafe impl Trait for Self`.
22pub const IMPL_FLAG_UNSAFE: u8 = 1 << 2;
23/// `ImplEntry.flags`: compiler reservation impl (`#[rustc_reservation_impl]`).
24pub const IMPL_FLAG_RESERVATION: u8 = 1 << 3;
25/// `ImplEntry.flags`: `impl const Trait for Self`.
26pub const IMPL_FLAG_CONST: u8 = 1 << 4;
27
28/// Coarse head-constructor discriminator for `ImplEntry.self_ty` (`1` byte).
29///
30/// Crucially preserves exact type constructors without `peel_refs`:
31/// `&T` (`RefShared`), `&mut T` (`RefMut`), blanket `T` (`Param`), tuples, and `fn` pointers
32/// each occupy their own distinct bucket so `IndexReader` can binary-search impls in `O(log N)`.
33#[repr(u8)]
34#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
35pub enum SimplifiedSelfKind {
36    Adt = 0,
37    Bool = 1,
38    Char = 2,
39    Int = 3,
40    Uint = 4,
41    Float = 5,
42    Str = 6,
43    Slice = 7,
44    Array = 8,
45    Tuple = 9,
46    RefShared = 10,
47    RefMut = 11,
48    PtrConst = 12,
49    PtrMut = 13,
50    FnPtr = 14,
51    Dynamic = 15,
52    Never = 16,
53    Param = 17,
54    Unit = 18,
55    Other = 19,
56}
57
58impl SimplifiedSelfKind {
59    #[inline]
60    pub const fn from_u8(v: u8) -> Self {
61        match v {
62            0 => Self::Adt,
63            1 => Self::Bool,
64            2 => Self::Char,
65            3 => Self::Int,
66            4 => Self::Uint,
67            5 => Self::Float,
68            6 => Self::Str,
69            7 => Self::Slice,
70            8 => Self::Array,
71            9 => Self::Tuple,
72            10 => Self::RefShared,
73            11 => Self::RefMut,
74            12 => Self::PtrConst,
75            13 => Self::PtrMut,
76            14 => Self::FnPtr,
77            15 => Self::Dynamic,
78            16 => Self::Never,
79            17 => Self::Param,
80            18 => Self::Unit,
81            _ => Self::Other,
82        }
83    }
84
85    #[inline]
86    pub const fn from_ty_entry(entry: TyEntry) -> Self {
87        match TyTag::from_u8(entry.tag) {
88            TyTag::Adt => Self::Adt,
89            TyTag::Bool => Self::Bool,
90            TyTag::Char => Self::Char,
91            TyTag::Int => Self::Int,
92            TyTag::Uint => Self::Uint,
93            TyTag::Float => Self::Float,
94            TyTag::Str => Self::Str,
95            TyTag::Slice => Self::Slice,
96            TyTag::Array => Self::Array,
97            TyTag::Tuple => Self::Tuple,
98            TyTag::Ref => match TyMutability::from_u8(entry.flags) {
99                TyMutability::Shared => Self::RefShared,
100                TyMutability::Mut => Self::RefMut,
101            },
102            TyTag::Ptr => match TyMutability::from_u8(entry.flags) {
103                TyMutability::Shared => Self::PtrConst,
104                TyMutability::Mut => Self::PtrMut,
105            },
106            TyTag::FnPtr => Self::FnPtr,
107            TyTag::Dyn => Self::Dynamic,
108            TyTag::Never => Self::Never,
109            TyTag::Param => Self::Param,
110            TyTag::Unit => Self::Unit,
111            TyTag::FnDef | TyTag::Assoc | TyTag::ImplTrait | TyTag::Unknown => Self::Other,
112        }
113    }
114}
115
116/// Structured inherent or trait `impl` header (32 bytes, 4-byte aligned).
117///
118/// Sorted on disk by `(simplified_self, is_trait_u8, self_ty, trait_def, def_index)`.
119#[repr(C)]
120#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
121pub struct ImplEntry {
122    /// Local `DefEntry` index of this `impl` block, or `u32::MAX` if metadata-only (e.g. sysroot).
123    pub def_index: u32,
124    /// Exact `Self` type (`TyId` in `TyArena`, never `peel_refs`-collapsed).
125    pub self_ty: u32,
126    /// Implemented trait (`TyDefId` in `TyArena`), or `u32::MAX` (`TyDefId::NONE`) for inherent impls.
127    pub trait_def: u32,
128    /// Start index in `ty_args` for generic arguments applied to `trait_def` (excluding `Self`).
129    pub trait_args_start: u32,
130    /// Start index in `assoc_tys` for `AssocTyEntry` bindings belonging to this `impl`.
131    pub assoc_tys_start: u32,
132    /// Start index in `ty_args` for local `DefEntry` indices (`AssocFn` / `AssocConst`) of this `impl`.
133    pub assoc_defs_start: u32,
134    /// Number of encoded `GenericArgWord`s starting at `trait_args_start`.
135    pub trait_args_len: u16,
136    /// Number of `AssocTyEntry` rows starting at `assoc_tys_start`.
137    pub assoc_tys_len: u16,
138    /// Number of `u32` `DefEntry` indices starting at `assoc_defs_start`.
139    pub assoc_defs_len: u16,
140    /// Bitflags (`IMPL_FLAG_*`).
141    pub flags: u8,
142    /// `SimplifiedSelfKind` discriminator (`u8`).
143    pub simplified_self: u8,
144}
145
146impl ImplEntry {
147    #[inline]
148    pub const fn to_le(self) -> Self {
149        Self {
150            def_index: self.def_index.to_le(),
151            self_ty: self.self_ty.to_le(),
152            trait_def: self.trait_def.to_le(),
153            trait_args_start: self.trait_args_start.to_le(),
154            assoc_tys_start: self.assoc_tys_start.to_le(),
155            assoc_defs_start: self.assoc_defs_start.to_le(),
156            trait_args_len: self.trait_args_len.to_le(),
157            assoc_tys_len: self.assoc_tys_len.to_le(),
158            assoc_defs_len: self.assoc_defs_len.to_le(),
159            flags: self.flags,
160            simplified_self: self.simplified_self,
161        }
162    }
163
164    #[inline]
165    pub const fn from_le(self) -> Self {
166        Self {
167            def_index: u32::from_le(self.def_index),
168            self_ty: u32::from_le(self.self_ty),
169            trait_def: u32::from_le(self.trait_def),
170            trait_args_start: u32::from_le(self.trait_args_start),
171            assoc_tys_start: u32::from_le(self.assoc_tys_start),
172            assoc_defs_start: u32::from_le(self.assoc_defs_start),
173            trait_args_len: u16::from_le(self.trait_args_len),
174            assoc_tys_len: u16::from_le(self.assoc_tys_len),
175            assoc_defs_len: u16::from_le(self.assoc_defs_len),
176            flags: self.flags,
177            simplified_self: self.simplified_self,
178        }
179    }
180
181    #[inline]
182    pub const fn simplified_self_kind(&self) -> SimplifiedSelfKind {
183        SimplifiedSelfKind::from_u8(self.simplified_self)
184    }
185
186    #[inline]
187    pub const fn is_trait(&self) -> bool {
188        (self.flags & IMPL_FLAG_TRAIT) != 0
189    }
190}
191
192/// Associated type binding inside an `ImplEntry` (`type Item = T;`, 16 bytes, 4-byte aligned).
193///
194/// Stored contiguously per `ImplEntry` (`assoc_tys_start..assoc_tys_start + assoc_tys_len`).
195#[repr(C)]
196#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
197pub struct AssocTyEntry {
198    /// Index of the owning `ImplEntry` in `impls`.
199    pub impl_index: u32,
200    /// Local `DefEntry` index in `definitions`, or `u32::MAX` if metadata-only.
201    pub def_index: u32,
202    /// Offset of the associated type identifier in `string_pool`.
203    pub name_offset: u32,
204    /// Lowered RHS `TyId` in `TyArena`.
205    pub ty: u32,
206}
207
208impl AssocTyEntry {
209    #[inline]
210    pub const fn to_le(self) -> Self {
211        Self { impl_index: self.impl_index.to_le(), def_index: self.def_index.to_le(), name_offset: self.name_offset.to_le(), ty: self.ty.to_le() }
212    }
213
214    #[inline]
215    pub const fn from_le(self) -> Self {
216        Self { impl_index: u32::from_le(self.impl_index), def_index: u32::from_le(self.def_index), name_offset: u32::from_le(self.name_offset), ty: u32::from_le(self.ty) }
217    }
218}