dioxus_rsx/location.rs
1use std::{cell::Cell, hash::Hash};
2
3/// A simple idx in the code that can be used to track back to the original source location
4///
5/// Used in two places:
6/// - In the `CallBody` to track the location of hotreloadable literals
7/// - In the `Body` to track the ID of each template
8///
9/// We need an ID system, unfortunately, to properly disambiguate between different templates since
10/// rustc assigns them all the same line!() and column!() information. Before, we hashed spans but
11/// that has collision issues and is eventually relied on specifics of proc macros that aren't available
12/// in testing (like snapshot testing). So, we just need an ID for each of these items, hence this struct.
13///
14/// This is "transparent" to partialeq and eq, so it will always return true when compared to another DynIdx.
15#[derive(Clone, Debug, Default)]
16pub struct DynIdx {
17 idx: Cell<Option<usize>>,
18}
19
20impl PartialEq for DynIdx {
21 fn eq(&self, _other: &Self) -> bool {
22 true
23 }
24}
25
26impl Eq for DynIdx {}
27
28impl DynIdx {
29 pub fn set(&self, idx: usize) {
30 self.idx.set(Some(idx));
31 }
32
33 pub fn get(&self) -> usize {
34 self.idx.get().unwrap_or(usize::MAX)
35 }
36}
37
38impl Hash for DynIdx {
39 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
40 self.idx.get().hash(state);
41 }
42}