Skip to main content

ridx_format/ty_arena/
generic_view.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//! Input descriptors and zero-copy views over `GenericParamEntry` and `WherePredEntry` tables.
12
13use crate::format::{
14    GenericOwner, GenericParamEntry, GenericParamKind, TyDefId, TyId, WherePredEntry, WherePredKind, GENERIC_PARAM_FLAG_HAS_DEFAULT, GENERIC_PARAM_FLAG_HOST_EFFECT,
15    GENERIC_PARAM_FLAG_IS_SELF, GENERIC_PARAM_FLAG_SIZED, GENERIC_PARAM_FLAG_SYNTHETIC, WHERE_FLAG_CONST, WHERE_FLAG_HRTB, WHERE_FLAG_NEGATIVE,
16};
17
18use super::intern::RawGenericArg;
19use super::iter::GenericArgIter;
20use super::TyArenaView;
21
22/// Writer input for a single generic parameter (`GenericParamEntry`).
23#[derive(Debug, Clone, Copy)]
24pub struct RawGenericParam<'a> {
25    pub def_index: Option<u32>,
26    pub name: &'a str,
27    pub param_idx: u16,
28    pub kind: GenericParamKind,
29    pub ty_or_default: Option<TyId>,
30    pub flags: u8,
31}
32
33/// Writer input for a single `where` / bound / supertrait / projection predicate (`WherePredEntry`).
34#[derive(Debug, Clone, Copy)]
35pub enum RawWherePred<'a> {
36    TraitBound { lhs_ty: TyId, trait_def: TyDefId, trait_args: &'a [RawGenericArg<'a>], flags: u8 },
37    ProjectionEq { self_ty: TyId, trait_def: TyDefId, trait_args: &'a [RawGenericArg<'a>], assoc_name: &'a str, rhs_ty: Option<TyId>, flags: u8 },
38    TypeOutlives { lhs_ty: TyId, region: u32, flags: u8 },
39    RegionOutlives { lhs_region: u32, rhs_region: u32, flags: u8 },
40}
41
42/// Zero-copy decoded view of a `GenericParamEntry`.
43#[derive(Debug, Clone, Copy, PartialEq, Eq)]
44pub struct GenericParamView<'a> {
45    pub owner: GenericOwner,
46    pub def_index: Option<u32>,
47    pub name: &'a str,
48    pub param_idx: u16,
49    pub kind: GenericParamKind,
50    pub ty_or_default: Option<TyId>,
51    pub flags: u8,
52}
53
54impl GenericParamView<'_> {
55    #[inline]
56    pub const fn has_default(&self) -> bool {
57        (self.flags & GENERIC_PARAM_FLAG_HAS_DEFAULT) != 0
58    }
59
60    #[inline]
61    pub const fn is_synthetic(&self) -> bool {
62        (self.flags & GENERIC_PARAM_FLAG_SYNTHETIC) != 0
63    }
64
65    #[inline]
66    pub const fn is_self(&self) -> bool {
67        (self.flags & GENERIC_PARAM_FLAG_IS_SELF) != 0
68    }
69
70    #[inline]
71    pub const fn is_sized(&self) -> bool {
72        (self.flags & GENERIC_PARAM_FLAG_SIZED) != 0
73    }
74
75    #[inline]
76    pub const fn is_host_effect(&self) -> bool {
77        (self.flags & GENERIC_PARAM_FLAG_HOST_EFFECT) != 0
78    }
79
80    /// Default `TyId` for `Type` parameters that declare `= DefaultTy`.
81    #[inline]
82    pub const fn default_ty(&self) -> Option<TyId> {
83        if matches!(self.kind, GenericParamKind::Type) && self.has_default() {
84            self.ty_or_default
85        } else {
86            None
87        }
88    }
89
90    /// Value `TyId` for `Const` parameters (`const N: Ty`).
91    #[inline]
92    pub const fn const_ty(&self) -> Option<TyId> {
93        if matches!(self.kind, GenericParamKind::Const) {
94            self.ty_or_default
95        } else {
96            None
97        }
98    }
99}
100
101/// Zero-allocation iterator over a contiguous slice of `GenericParamEntry` records.
102#[derive(Debug, Clone, Copy)]
103pub struct GenericParamIter<'a> {
104    arena: TyArenaView<'a>,
105    entries: &'a [GenericParamEntry],
106}
107
108impl<'a> GenericParamIter<'a> {
109    #[inline]
110    pub const fn new(arena: TyArenaView<'a>, entries: &'a [GenericParamEntry]) -> Self {
111        Self { arena, entries }
112    }
113
114    #[inline]
115    pub const fn len(&self) -> usize {
116        self.entries.len()
117    }
118
119    #[inline]
120    pub const fn is_empty(&self) -> bool {
121        self.entries.is_empty()
122    }
123
124    #[inline]
125    fn decode(arena: TyArenaView<'a>, raw: GenericParamEntry) -> GenericParamView<'a> {
126        let e = raw.from_le();
127        GenericParamView {
128            owner: GenericOwner::from_raw(e.owner_key),
129            def_index: (e.def_index != u32::MAX).then_some(e.def_index),
130            name: arena.str_at(e.name_offset),
131            param_idx: e.param_idx,
132            kind: GenericParamKind::from_u8(e.kind),
133            ty_or_default: (e.ty_or_default != u32::MAX).then_some(TyId(e.ty_or_default)),
134            flags: e.flags,
135        }
136    }
137
138    #[inline]
139    pub fn get(&self, idx: usize) -> Option<GenericParamView<'a>> {
140        self.entries.get(idx).copied().map(|r| Self::decode(self.arena, r))
141    }
142}
143
144impl<'a> Iterator for GenericParamIter<'a> {
145    type Item = GenericParamView<'a>;
146
147    #[inline]
148    fn next(&mut self) -> Option<Self::Item> {
149        let (&first, rest) = self.entries.split_first()?;
150        self.entries = rest;
151        Some(Self::decode(self.arena, first))
152    }
153
154    #[inline]
155    fn size_hint(&self) -> (usize, Option<usize>) {
156        (self.entries.len(), Some(self.entries.len()))
157    }
158}
159
160impl ExactSizeIterator for GenericParamIter<'_> {}
161
162/// Zero-copy decoded view of a `WherePredEntry`.
163#[derive(Debug, Clone, Copy)]
164pub struct WherePredView<'a> {
165    arena: TyArenaView<'a>,
166    entry: WherePredEntry,
167}
168
169impl<'a> WherePredView<'a> {
170    #[inline]
171    pub fn new(arena: TyArenaView<'a>, raw: WherePredEntry) -> Self {
172        Self { arena, entry: raw.from_le() }
173    }
174
175    #[inline]
176    pub const fn owner(&self) -> GenericOwner {
177        GenericOwner::from_raw(self.entry.owner_key)
178    }
179
180    #[inline]
181    pub const fn kind(&self) -> WherePredKind {
182        WherePredKind::from_u8(self.entry.kind)
183    }
184
185    #[inline]
186    pub const fn is_hrtb(&self) -> bool {
187        (self.entry.flags & WHERE_FLAG_HRTB) != 0
188    }
189
190    #[inline]
191    pub const fn is_negative(&self) -> bool {
192        (self.entry.flags & WHERE_FLAG_NEGATIVE) != 0
193    }
194
195    #[inline]
196    pub const fn is_const(&self) -> bool {
197        (self.entry.flags & WHERE_FLAG_CONST) != 0
198    }
199
200    #[inline]
201    pub const fn lhs_ty(&self) -> TyId {
202        TyId(self.entry.lhs_ty)
203    }
204
205    #[inline]
206    pub const fn lhs_region(&self) -> u32 {
207        self.entry.lhs_ty
208    }
209
210    #[inline]
211    pub const fn trait_def(&self) -> Option<TyDefId> {
212        if self.entry.trait_def == u32::MAX {
213            None
214        } else {
215            Some(TyDefId(self.entry.trait_def))
216        }
217    }
218
219    #[inline]
220    pub fn trait_args(&self) -> GenericArgIter<'a> {
221        GenericArgIter::from_words(self.arena.words_slice(self.entry.trait_args_start, u32::from(self.entry.trait_args_len)))
222    }
223
224    #[inline]
225    pub fn assoc_name(&self) -> Option<&'a str> {
226        (self.entry.assoc_name_offset != u32::MAX).then(|| self.arena.str_at(self.entry.assoc_name_offset))
227    }
228
229    #[inline]
230    pub const fn rhs_ty(&self) -> Option<TyId> {
231        if matches!(self.kind(), WherePredKind::ProjectionEq) && self.entry.rhs_or_region != u32::MAX {
232            Some(TyId(self.entry.rhs_or_region))
233        } else {
234            None
235        }
236    }
237
238    #[inline]
239    pub const fn outlives_region(&self) -> Option<u32> {
240        if matches!(self.kind(), WherePredKind::TypeOutlives | WherePredKind::RegionOutlives) {
241            Some(self.entry.rhs_or_region)
242        } else {
243            None
244        }
245    }
246}
247
248/// Zero-allocation iterator over a contiguous slice of `WherePredEntry` records.
249#[derive(Debug, Clone, Copy)]
250pub struct WherePredIter<'a> {
251    arena: TyArenaView<'a>,
252    entries: &'a [WherePredEntry],
253}
254
255impl<'a> WherePredIter<'a> {
256    #[inline]
257    pub const fn new(arena: TyArenaView<'a>, entries: &'a [WherePredEntry]) -> Self {
258        Self { arena, entries }
259    }
260
261    #[inline]
262    pub const fn len(&self) -> usize {
263        self.entries.len()
264    }
265
266    #[inline]
267    pub const fn is_empty(&self) -> bool {
268        self.entries.is_empty()
269    }
270}
271
272impl<'a> Iterator for WherePredIter<'a> {
273    type Item = WherePredView<'a>;
274
275    #[inline]
276    fn next(&mut self) -> Option<Self::Item> {
277        let (&first, rest) = self.entries.split_first()?;
278        self.entries = rest;
279        Some(WherePredView::new(self.arena, first))
280    }
281
282    #[inline]
283    fn size_hint(&self) -> (usize, Option<usize>) {
284        (self.entries.len(), Some(self.entries.len()))
285    }
286}
287
288impl ExactSizeIterator for WherePredIter<'_> {}