Skip to main content

ridx_format/ty_arena/
use_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//! Writer input and zero-copy decoded view/iterator over `UseEntry` tables (1.2-G7).
12
13use crate::format::{
14    ItemVis, TyDefId, UseEntry, UseOwner, UsePrimTy, USE_FLAG_EXTERN_CRATE, USE_FLAG_GLOB, USE_FLAG_PRELUDE, USE_FLAG_PRIM_TY, USE_FLAG_REEXPORT, USE_FLAG_UNDERSCORE,
15};
16
17use super::TyArenaView;
18
19/// Writer input for a module `use`, glob, re-export, or `extern crate` binding.
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub struct RawUse<'a> {
22    pub owner: UseOwner,
23    pub def_index: Option<u32>,
24    pub name: &'a str,
25    pub type_def: Option<TyDefId>,
26    pub value_def: Option<TyDefId>,
27    pub macro_def: Option<TyDefId>,
28    pub vis: ItemVis,
29    pub flags: u8,
30    pub prim_ty: UsePrimTy,
31}
32
33/// Zero-copy decoded view of a `UseEntry`.
34#[derive(Debug, Clone, Copy, PartialEq, Eq)]
35pub struct UseView<'a> {
36    pub owner: UseOwner,
37    pub def_index: Option<u32>,
38    pub name: &'a str,
39    pub type_def: Option<TyDefId>,
40    pub value_def: Option<TyDefId>,
41    pub macro_def: Option<TyDefId>,
42    pub vis: ItemVis,
43    pub flags: u8,
44    pub prim_ty: UsePrimTy,
45}
46
47impl<'a> UseView<'a> {
48    #[inline]
49    pub fn from_raw(arena: TyArenaView<'a>, raw: UseEntry) -> Self {
50        Self {
51            owner: raw.owner(),
52            def_index: raw.def_index(),
53            name: arena.str_at(raw.name_offset()),
54            type_def: raw.type_def(),
55            value_def: raw.value_def(),
56            macro_def: raw.macro_def(),
57            vis: raw.visibility(),
58            flags: raw.flags,
59            prim_ty: raw.prim_ty(),
60        }
61    }
62
63    #[inline]
64    pub const fn is_glob(&self) -> bool {
65        (self.flags & USE_FLAG_GLOB) != 0
66    }
67
68    #[inline]
69    pub const fn is_reexport(&self) -> bool {
70        (self.flags & USE_FLAG_REEXPORT) != 0
71    }
72
73    #[inline]
74    pub const fn is_extern_crate(&self) -> bool {
75        (self.flags & USE_FLAG_EXTERN_CRATE) != 0
76    }
77
78    #[inline]
79    pub const fn is_underscore(&self) -> bool {
80        (self.flags & USE_FLAG_UNDERSCORE) != 0
81    }
82
83    #[inline]
84    pub const fn has_prim_ty(&self) -> bool {
85        (self.flags & USE_FLAG_PRIM_TY) != 0
86    }
87
88    #[inline]
89    pub const fn is_prelude(&self) -> bool {
90        (self.flags & USE_FLAG_PRELUDE) != 0
91    }
92}
93
94/// Zero-allocation iterator over `UseEntry` records.
95#[derive(Debug, Clone, Copy)]
96pub struct UseIter<'a> {
97    arena: TyArenaView<'a>,
98    entries: &'a [UseEntry],
99}
100
101impl<'a> UseIter<'a> {
102    #[inline]
103    pub const fn new(arena: TyArenaView<'a>, entries: &'a [UseEntry]) -> Self {
104        Self { arena, entries }
105    }
106
107    #[inline]
108    pub const fn len(&self) -> usize {
109        self.entries.len()
110    }
111
112    #[inline]
113    pub const fn is_empty(&self) -> bool {
114        self.entries.is_empty()
115    }
116}
117
118impl<'a> Iterator for UseIter<'a> {
119    type Item = UseView<'a>;
120
121    #[inline]
122    fn next(&mut self) -> Option<Self::Item> {
123        let (&first, rest) = self.entries.split_first()?;
124        self.entries = rest;
125        Some(UseView::from_raw(self.arena, first))
126    }
127
128    #[inline]
129    fn size_hint(&self) -> (usize, Option<usize>) {
130        (self.entries.len(), Some(self.entries.len()))
131    }
132}
133
134impl ExactSizeIterator for UseIter<'_> {}