Skip to main content

ridx_format/ty_arena/
impl_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 `ImplEntry` and `AssocTyEntry` tables.
12
13use crate::format::{AssocTyEntry, ImplEntry, SimplifiedSelfKind, TyDefId, TyId, IMPL_FLAG_CONST, IMPL_FLAG_NEGATIVE, IMPL_FLAG_RESERVATION, IMPL_FLAG_TRAIT, IMPL_FLAG_UNSAFE};
14
15use super::intern::RawGenericArg;
16use super::iter::{GenericArgIter, U32ListIter};
17use super::TyArenaView;
18
19/// Writer input for an associated type binding inside an `impl` (`type Item = T;`).
20#[derive(Debug, Clone, Copy)]
21pub struct RawAssocTy<'a> {
22    pub def_index: Option<u32>,
23    pub name: &'a str,
24    pub ty: TyId,
25}
26
27/// Writer input for an inherent or trait `impl` header.
28#[derive(Debug, Clone, Copy)]
29pub struct RawImpl<'a> {
30    pub def_index: Option<u32>,
31    pub self_ty: TyId,
32    pub trait_def: Option<TyDefId>,
33    pub trait_args: &'a [RawGenericArg<'a>],
34    pub assoc_tys: &'a [RawAssocTy<'a>],
35    pub assoc_defs: &'a [u32],
36    pub flags: u8,
37}
38
39/// Zero-copy view of a single `AssocTyEntry`.
40#[derive(Debug, Clone, Copy, PartialEq, Eq)]
41pub struct AssocTyView<'a> {
42    pub impl_index: u32,
43    pub def_index: Option<u32>,
44    pub name: &'a str,
45    pub ty: TyId,
46}
47
48/// Zero-allocation iterator over a contiguous slice of `AssocTyEntry` records.
49#[derive(Debug, Clone, Copy)]
50pub struct AssocTyIter<'a> {
51    arena: TyArenaView<'a>,
52    entries: &'a [AssocTyEntry],
53}
54
55impl<'a> AssocTyIter<'a> {
56    #[inline]
57    pub const fn new(arena: TyArenaView<'a>, entries: &'a [AssocTyEntry]) -> Self {
58        Self { arena, entries }
59    }
60
61    #[inline]
62    pub fn len(&self) -> usize {
63        self.entries.len()
64    }
65
66    #[inline]
67    pub fn is_empty(&self) -> bool {
68        self.entries.is_empty()
69    }
70}
71
72impl<'a> Iterator for AssocTyIter<'a> {
73    type Item = AssocTyView<'a>;
74
75    #[inline]
76    fn next(&mut self) -> Option<Self::Item> {
77        let (&raw, rest) = self.entries.split_first()?;
78        self.entries = rest;
79        let e = raw.from_le();
80        Some(AssocTyView { impl_index: e.impl_index, def_index: (e.def_index != u32::MAX).then_some(e.def_index), name: self.arena.str_at(e.name_offset), ty: TyId(e.ty) })
81    }
82
83    #[inline]
84    fn size_hint(&self) -> (usize, Option<usize>) {
85        (self.entries.len(), Some(self.entries.len()))
86    }
87}
88
89impl ExactSizeIterator for AssocTyIter<'_> {}
90
91/// Zero-copy view of a decoded `ImplEntry` paired with its shard `TyArena` and `assoc_tys` table.
92#[derive(Debug, Clone, Copy)]
93pub struct ImplView<'a> {
94    index: u32,
95    entry: ImplEntry,
96    arena: TyArenaView<'a>,
97    all_assoc_tys: &'a [AssocTyEntry],
98}
99
100impl<'a> ImplView<'a> {
101    #[inline]
102    pub fn new(index: u32, raw: ImplEntry, arena: TyArenaView<'a>, all_assoc_tys: &'a [AssocTyEntry]) -> Self {
103        Self { index, entry: raw.from_le(), arena, all_assoc_tys }
104    }
105
106    #[inline]
107    pub const fn index(&self) -> u32 {
108        self.index
109    }
110
111    #[inline]
112    pub const fn def_index(&self) -> Option<u32> {
113        if self.entry.def_index == u32::MAX {
114            None
115        } else {
116            Some(self.entry.def_index)
117        }
118    }
119
120    #[inline]
121    pub const fn self_ty(&self) -> TyId {
122        TyId(self.entry.self_ty)
123    }
124
125    #[inline]
126    pub const fn simplified_self(&self) -> SimplifiedSelfKind {
127        SimplifiedSelfKind::from_u8(self.entry.simplified_self)
128    }
129
130    #[inline]
131    pub const fn trait_def(&self) -> Option<TyDefId> {
132        if self.entry.trait_def == u32::MAX {
133            None
134        } else {
135            Some(TyDefId(self.entry.trait_def))
136        }
137    }
138
139    #[inline]
140    pub fn trait_args(&self) -> GenericArgIter<'a> {
141        GenericArgIter::from_words(self.arena.words_slice(self.entry.trait_args_start, u32::from(self.entry.trait_args_len)))
142    }
143
144    #[inline]
145    pub fn assoc_tys(&self) -> AssocTyIter<'a> {
146        let s = self.entry.assoc_tys_start as usize;
147        let end = s.saturating_add(self.entry.assoc_tys_len as usize);
148        AssocTyIter::new(self.arena, self.all_assoc_tys.get(s..end).unwrap_or(&[]))
149    }
150
151    /// Finds the RHS `TyId` of an associated type binding by name (e.g. `"Item"`, `"Target"`).
152    #[inline]
153    pub fn find_assoc_ty(&self, name: &str) -> Option<TyId> {
154        self.assoc_tys().find_map(|a| (a.name == name).then_some(a.ty))
155    }
156
157    #[inline]
158    pub fn assoc_def_indices(&self) -> U32ListIter<'a> {
159        U32ListIter::from_words(self.arena.words_slice(self.entry.assoc_defs_start, u32::from(self.entry.assoc_defs_len)))
160    }
161
162    #[inline]
163    pub const fn flags(&self) -> u8 {
164        self.entry.flags
165    }
166
167    #[inline]
168    pub const fn is_trait(&self) -> bool {
169        (self.entry.flags & IMPL_FLAG_TRAIT) != 0
170    }
171
172    #[inline]
173    pub const fn is_inherent(&self) -> bool {
174        !self.is_trait()
175    }
176
177    #[inline]
178    pub const fn is_negative(&self) -> bool {
179        (self.entry.flags & IMPL_FLAG_NEGATIVE) != 0
180    }
181
182    #[inline]
183    pub const fn is_unsafe(&self) -> bool {
184        (self.entry.flags & IMPL_FLAG_UNSAFE) != 0
185    }
186
187    #[inline]
188    pub const fn is_reservation(&self) -> bool {
189        (self.entry.flags & IMPL_FLAG_RESERVATION) != 0
190    }
191
192    #[inline]
193    pub const fn is_const(&self) -> bool {
194        (self.entry.flags & IMPL_FLAG_CONST) != 0
195    }
196}
197
198/// Zero-allocation iterator over a slice of `ImplEntry` records.
199#[derive(Debug, Clone, Copy)]
200pub struct ImplIter<'a> {
201    base_index: u32,
202    entries: &'a [ImplEntry],
203    arena: TyArenaView<'a>,
204    all_assoc_tys: &'a [AssocTyEntry],
205}
206
207impl<'a> ImplIter<'a> {
208    #[inline]
209    pub const fn new(base_index: u32, entries: &'a [ImplEntry], arena: TyArenaView<'a>, all_assoc_tys: &'a [AssocTyEntry]) -> Self {
210        Self { base_index, entries, arena, all_assoc_tys }
211    }
212
213    #[inline]
214    pub fn len(&self) -> usize {
215        self.entries.len()
216    }
217
218    #[inline]
219    pub fn is_empty(&self) -> bool {
220        self.entries.is_empty()
221    }
222}
223
224impl<'a> Iterator for ImplIter<'a> {
225    type Item = ImplView<'a>;
226
227    #[inline]
228    fn next(&mut self) -> Option<Self::Item> {
229        let (&raw, rest) = self.entries.split_first()?;
230        let idx = self.base_index;
231        self.base_index = self.base_index.saturating_add(1);
232        self.entries = rest;
233        Some(ImplView::new(idx, raw, self.arena, self.all_assoc_tys))
234    }
235
236    #[inline]
237    fn size_hint(&self) -> (usize, Option<usize>) {
238        (self.entries.len(), Some(self.entries.len()))
239    }
240}
241
242impl ExactSizeIterator for ImplIter<'_> {}