grit_util/
code_range.rs

1#[derive(Debug, Clone, Eq, Hash, PartialEq)]
2pub struct CodeRange {
3    /// Start byte of the range.
4    pub start: u32,
5
6    /// End byte of the range.
7    pub end: u32,
8
9    /// Address of the source code to which the range applies.
10    ///
11    /// Stored as only the address to improve the performance of hashing.
12    pub address: usize,
13}
14
15impl CodeRange {
16    pub fn new(start: u32, end: u32, src: &str) -> Self {
17        let raw_ptr = src as *const str;
18        let thin_ptr = raw_ptr as *const u8;
19        let address = thin_ptr as usize;
20        Self {
21            start,
22            end,
23            address,
24        }
25    }
26
27    /// Returns whether the code range applies to the given source code.
28    pub fn applies_to(&self, source: &str) -> bool {
29        let raw_ptr = source as *const str;
30        let thin_ptr = raw_ptr as *const u8;
31        let address = thin_ptr as usize;
32        self.address == address
33    }
34
35    /// Returns whether the given index is contained within the range.
36    pub fn contains(&self, index: u32) -> bool {
37        self.start <= index && self.end > index
38    }
39}