Skip to main content

ridx_format/format/
cfg_item.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//! Binary layout for active per-crate `cfg` predicates (`#[cfg(...)]` and `--cfg feature="..."`).
12
13/// Sentinel `value_offset` indicating a bare atom predicate (`#[cfg(unix)]`, `#[cfg(test)]`)
14/// as opposed to a key-value predicate (`#[cfg(feature = "std")]`, including empty `""`).
15pub const NO_CFG_VALUE: u32 = u32::MAX;
16
17/// Fixed-size 8-byte `cfg` table entry (`align 4`, zero padding).
18///
19/// Entries in the `cfgs` section are sorted lexicographically by
20/// `(key_str, has_value_bool, value_str)` so bare atoms `cfg(key)` precede
21/// `cfg(key = "...")` and `cfg_values(key)` forms a contiguous range for `O(log N)` lookup.
22#[repr(C)]
23#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
24pub struct CfgEntry {
25    pub key_offset: u32,
26    pub value_offset: u32,
27}
28
29impl CfgEntry {
30    #[inline]
31    pub const fn new_flag(key_offset: u32) -> Self {
32        Self { key_offset, value_offset: NO_CFG_VALUE }
33    }
34
35    #[inline]
36    pub const fn new_key_value(key_offset: u32, value_offset: u32) -> Self {
37        Self { key_offset, value_offset }
38    }
39
40    #[inline]
41    pub const fn has_value(self) -> bool {
42        self.value_offset != NO_CFG_VALUE
43    }
44
45    #[inline]
46    pub const fn key_offset(self) -> u32 {
47        u32::from_le(self.key_offset)
48    }
49
50    #[inline]
51    pub const fn value_offset_opt(self) -> Option<u32> {
52        let v = u32::from_le(self.value_offset);
53        if v == NO_CFG_VALUE {
54            None
55        } else {
56            Some(v)
57        }
58    }
59
60    #[inline]
61    pub const fn to_le(self) -> Self {
62        Self { key_offset: self.key_offset.to_le(), value_offset: self.value_offset.to_le() }
63    }
64
65    #[inline]
66    pub const fn from_le(self) -> Self {
67        Self { key_offset: u32::from_le(self.key_offset), value_offset: u32::from_le(self.value_offset) }
68    }
69}