Skip to main content

ridx_format/ty_arena/
iter.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//! Zero-allocation iterators and decoded element views over `TyArena` packed slices.
12
13use crate::format::{ConstArg, GenericArgWord, TyBoundEntry, TyDefId, TyId};
14
15use super::TyArenaView;
16
17/// Decoded generic argument from `ty_args`.
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19pub enum GenericArgView {
20    Type(TyId),
21    Const(ConstArg),
22    Lifetime(u32),
23}
24
25impl GenericArgView {
26    #[inline]
27    pub const fn decode(raw: u32) -> Self {
28        let w = GenericArgWord(raw);
29        match w.tag_bits() {
30            GenericArgWord::TAG_TYPE => Self::Type(TyId(w.payload())),
31            GenericArgWord::TAG_LIFETIME => Self::Lifetime(w.payload()),
32            _ => Self::Const(GenericArgWord::decode_const(raw)),
33        }
34    }
35}
36
37/// Zero-allocation iterator over `GenericArgWord`s stored in `ty_args`.
38#[derive(Debug, Clone, Copy)]
39pub struct GenericArgIter<'a> {
40    pub(crate) words: &'a [u32],
41}
42
43impl<'a> GenericArgIter<'a> {
44    pub const EMPTY: Self = Self { words: &[] };
45
46    #[inline]
47    pub const fn from_words(words: &'a [u32]) -> Self {
48        Self { words }
49    }
50
51    #[inline]
52    pub fn len(&self) -> usize {
53        self.words.len()
54    }
55
56    #[inline]
57    pub fn is_empty(&self) -> bool {
58        self.words.is_empty()
59    }
60
61    #[inline]
62    pub fn get(&self, idx: usize) -> Option<GenericArgView> {
63        self.words.get(idx).map(|&w| GenericArgView::decode(u32::from_le(w)))
64    }
65}
66
67impl<'a> Iterator for GenericArgIter<'a> {
68    type Item = GenericArgView;
69
70    #[inline]
71    fn next(&mut self) -> Option<Self::Item> {
72        let (&first, rest) = self.words.split_first()?;
73        self.words = rest;
74        Some(GenericArgView::decode(u32::from_le(first)))
75    }
76
77    #[inline]
78    fn size_hint(&self) -> (usize, Option<usize>) {
79        (self.words.len(), Some(self.words.len()))
80    }
81}
82
83impl ExactSizeIterator for GenericArgIter<'_> {}
84
85/// Zero-allocation iterator over `TyId`s stored in `ty_args` (tuples and `fn` inputs).
86#[derive(Debug, Clone, Copy)]
87pub struct TyListIter<'a> {
88    pub(crate) words: &'a [u32],
89}
90
91impl<'a> TyListIter<'a> {
92    pub const EMPTY: Self = Self { words: &[] };
93
94    #[inline]
95    pub const fn from_words(words: &'a [u32]) -> Self {
96        Self { words }
97    }
98
99    #[inline]
100    pub fn len(&self) -> usize {
101        self.words.len()
102    }
103
104    #[inline]
105    pub fn is_empty(&self) -> bool {
106        self.words.is_empty()
107    }
108
109    #[inline]
110    pub fn get(&self, idx: usize) -> Option<TyId> {
111        self.words.get(idx).map(|&w| TyId(u32::from_le(w)))
112    }
113}
114
115impl<'a> Iterator for TyListIter<'a> {
116    type Item = TyId;
117
118    #[inline]
119    fn next(&mut self) -> Option<Self::Item> {
120        let (&first, rest) = self.words.split_first()?;
121        self.words = rest;
122        Some(TyId(u32::from_le(first)))
123    }
124
125    #[inline]
126    fn size_hint(&self) -> (usize, Option<usize>) {
127        (self.words.len(), Some(self.words.len()))
128    }
129}
130
131impl ExactSizeIterator for TyListIter<'_> {}
132
133/// Zero-allocation iterator over raw `u32` indices stored in `ty_args` (e.g. `ImplEntry` assoc def indices).
134#[derive(Debug, Clone, Copy)]
135pub struct U32ListIter<'a> {
136    words: &'a [u32],
137}
138
139impl<'a> U32ListIter<'a> {
140    pub const EMPTY: Self = Self { words: &[] };
141
142    #[inline]
143    pub const fn from_words(words: &'a [u32]) -> Self {
144        Self { words }
145    }
146
147    #[inline]
148    pub fn len(&self) -> usize {
149        self.words.len()
150    }
151
152    #[inline]
153    pub fn is_empty(&self) -> bool {
154        self.words.is_empty()
155    }
156
157    #[inline]
158    pub fn get(&self, idx: usize) -> Option<u32> {
159        self.words.get(idx).map(|&w| u32::from_le(w))
160    }
161}
162
163impl<'a> Iterator for U32ListIter<'a> {
164    type Item = u32;
165
166    #[inline]
167    fn next(&mut self) -> Option<Self::Item> {
168        let (&first, rest) = self.words.split_first()?;
169        self.words = rest;
170        Some(u32::from_le(first))
171    }
172
173    #[inline]
174    fn size_hint(&self) -> (usize, Option<usize>) {
175        (self.words.len(), Some(self.words.len()))
176    }
177}
178
179impl ExactSizeIterator for U32ListIter<'_> {}
180
181/// Zero-copy associated type equality binding (`Item = u8`) inside a `TyBoundView`.
182#[derive(Debug, Clone, Copy, PartialEq, Eq)]
183pub struct AssocBindingView<'a> {
184    pub name: &'a str,
185    pub ty: TyId,
186}
187
188/// Zero-allocation iterator over `(assoc_name_off, assoc_ty_id)` pairs in `ty_args`.
189#[derive(Debug, Clone, Copy)]
190pub struct AssocBindingIter<'a> {
191    pub(crate) arena: TyArenaView<'a>,
192    pub(crate) words: &'a [u32],
193}
194
195impl<'a> Iterator for AssocBindingIter<'a> {
196    type Item = AssocBindingView<'a>;
197
198    #[inline]
199    fn next(&mut self) -> Option<Self::Item> {
200        if self.words.len() < 2 {
201            self.words = &[];
202            return None;
203        }
204        let name_off = u32::from_le(self.words[0]);
205        let ty = TyId(u32::from_le(self.words[1]));
206        self.words = &self.words[2..];
207        Some(AssocBindingView { name: self.arena.str_at(name_off), ty })
208    }
209
210    #[inline]
211    fn size_hint(&self) -> (usize, Option<usize>) {
212        let n = self.words.len() / 2;
213        (n, Some(n))
214    }
215}
216
217impl ExactSizeIterator for AssocBindingIter<'_> {}
218
219/// Zero-copy view of a single `TyBoundEntry`.
220#[derive(Debug, Clone, Copy)]
221pub struct TyBoundView<'a> {
222    pub trait_def: TyDefId,
223    pub is_maybe: bool,
224    pub flags: u32,
225    pub args: GenericArgIter<'a>,
226    pub assocs: AssocBindingIter<'a>,
227}
228
229/// Zero-allocation iterator over a contiguous slice of `TyBoundEntry` records.
230#[derive(Debug, Clone, Copy)]
231pub struct TyBoundIter<'a> {
232    pub(crate) arena: TyArenaView<'a>,
233    pub(crate) bounds: &'a [TyBoundEntry],
234}
235
236impl<'a> TyBoundIter<'a> {
237    #[inline]
238    pub fn len(&self) -> usize {
239        self.bounds.len()
240    }
241
242    #[inline]
243    pub fn is_empty(&self) -> bool {
244        self.bounds.is_empty()
245    }
246}
247
248impl<'a> Iterator for TyBoundIter<'a> {
249    type Item = TyBoundView<'a>;
250
251    #[inline]
252    fn next(&mut self) -> Option<Self::Item> {
253        let (&raw, rest) = self.bounds.split_first()?;
254        self.bounds = rest;
255        Some(self.arena.decode_bound(raw.from_le()))
256    }
257
258    #[inline]
259    fn size_hint(&self) -> (usize, Option<usize>) {
260        (self.bounds.len(), Some(self.bounds.len()))
261    }
262}
263
264impl ExactSizeIterator for TyBoundIter<'_> {}