dioxus_rsx/
location.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use std::{cell::Cell, hash::Hash};

/// A simple idx in the code that can be used to track back to the original source location
///
/// Used in two places:
/// - In the `CallBody` to track the location of hotreloadable literals
/// - In the `Body` to track the ID of each template
///
/// We need an ID system, unfortunately, to properly disambiguate between different templates since
/// rustc assigns them all the same line!() and column!() information. Before, we hashed spans but
/// that has collision issues and is eventually relied on specifics of proc macros that aren't available
/// in testing (like snapshot testing). So, we just need an ID for each of these items, hence this struct.
///
/// This is "transparent" to partialeq and eq, so it will always return true when compared to another DynIdx.
#[derive(Clone, Debug, Default)]
pub struct DynIdx {
    idx: Cell<Option<usize>>,
}

impl PartialEq for DynIdx {
    fn eq(&self, _other: &Self) -> bool {
        true
    }
}

impl Eq for DynIdx {}

impl DynIdx {
    pub fn set(&self, idx: usize) {
        self.idx.set(Some(idx));
    }

    pub fn get(&self) -> usize {
        self.idx.get().unwrap_or(usize::MAX)
    }
}

impl Hash for DynIdx {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.idx.get().hash(state);
    }
}