Skip to main content

ridx_format/reader/
cfg_access.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-copy `O(log N)` reader accessors for `CfgEntry`, enabled features, and target triple.
12
13use super::IndexReader;
14use crate::format::{CfgEntry, NO_CFG_VALUE};
15
16impl<B: AsRef<[u8]>> IndexReader<B> {
17    /// Effective target triple recorded for this shard (or `""` if none).
18    #[inline]
19    pub fn target_triple(&self) -> &str {
20        let off = self.header.target_offset;
21        if off == 0 {
22            ""
23        } else {
24            self.get_str(off)
25        }
26    }
27
28    /// Raw slice of `CfgEntry` rows sorted by `(key_str, Option<value_str>)`.
29    #[inline]
30    pub fn cfgs(&self) -> &[CfgEntry] {
31        let h = self.header();
32        let count = h.cfgs_count as usize;
33        if count == 0 {
34            return &[];
35        }
36        let start = h.cfgs_offset as usize;
37        let len = count * std::mem::size_of::<CfgEntry>();
38        let bytes = self.bytes();
39        if start + len > bytes.len() {
40            return &[];
41        }
42        unsafe { std::slice::from_raw_parts(bytes[start..].as_ptr() as *const CfgEntry, count) }
43    }
44
45    /// Iterates over all `(key, Option<value>)` `cfg` predicates in sorted order.
46    pub fn cfg_iter(&self) -> impl ExactSizeIterator<Item = (&str, Option<&str>)> + '_ {
47        self.cfgs().iter().map(|raw| {
48            let e = raw.from_le();
49            let k = self.get_str(e.key_offset);
50            let v = if e.value_offset == NO_CFG_VALUE { None } else { Some(self.get_str(e.value_offset)) };
51            (k, v)
52        })
53    }
54
55    /// `O(log N)` lookup for a bare `cfg` flag atom (`#[cfg(unix)]`, `#[cfg(test)]`).
56    pub fn has_cfg_flag(&self, key: &str) -> bool {
57        let entries = self.cfgs();
58        entries
59            .binary_search_by(|raw| {
60                let e = raw.from_le();
61                let ek = self.get_str(e.key_offset);
62                let ev: Option<&str> = if e.value_offset == NO_CFG_VALUE { None } else { Some(self.get_str(e.value_offset)) };
63                (ek, ev).cmp(&(key, None))
64            })
65            .is_ok()
66    }
67
68    /// `O(log N)` lookup for a `cfg(key = "value")` predicate (`#[cfg(feature = "std")]`).
69    pub fn has_cfg_key_value(&self, key: &str, value: &str) -> bool {
70        let entries = self.cfgs();
71        entries
72            .binary_search_by(|raw| {
73                let e = raw.from_le();
74                let ek = self.get_str(e.key_offset);
75                let ev: Option<&str> = if e.value_offset == NO_CFG_VALUE { None } else { Some(self.get_str(e.value_offset)) };
76                (ek, ev).cmp(&(key, Some(value)))
77            })
78            .is_ok()
79    }
80
81    /// `O(log N)` zero-allocation iterator over all values `v` of `cfg(key = "v")` in sorted order.
82    pub fn cfg_values<'a>(&'a self, key: &str) -> impl ExactSizeIterator<Item = &'a str> + 'a {
83        let entries = self.cfgs();
84        let start = entries.partition_point(|raw| {
85            let e = raw.from_le();
86            let ek = self.get_str(e.key_offset);
87            match ek.cmp(key) {
88                std::cmp::Ordering::Less => true,
89                std::cmp::Ordering::Equal => e.value_offset == NO_CFG_VALUE,
90                std::cmp::Ordering::Greater => false,
91            }
92        });
93        let end = entries[start..].partition_point(|raw| {
94            let e = raw.from_le();
95            self.get_str(e.key_offset) == key
96        }) + start;
97        entries[start..end].iter().map(move |raw| self.get_str(raw.from_le().value_offset))
98    }
99
100    /// `O(log N)` zero-allocation iterator over all enabled Cargo features (`feature = "..."`).
101    #[inline]
102    pub fn enabled_features(&self) -> impl ExactSizeIterator<Item = &str> + '_ {
103        self.cfg_values("feature")
104    }
105}