surfer_translation_types/
scope_ref.rs

1use serde::{Deserialize, Serialize};
2use std::fmt::{Display, Formatter};
3use std::hash::{Hash, Hasher};
4
5#[derive(Clone, Debug, Eq, Serialize, Deserialize, Default)]
6pub struct ScopeRef<ScopeId> {
7    pub strs: Vec<String>,
8    /// Backend specific numeric ID. Performance optimization.
9    pub id: ScopeId,
10}
11
12impl<ScopeId1> ScopeRef<ScopeId1> {
13    pub fn map_id<ScopeId2>(
14        self,
15        mut scope_fn: impl FnMut(ScopeId1) -> ScopeId2,
16    ) -> ScopeRef<ScopeId2> {
17        ScopeRef {
18            strs: self.strs,
19            id: scope_fn(self.id),
20        }
21    }
22}
23
24impl<ScopeId> AsRef<ScopeRef<ScopeId>> for ScopeRef<ScopeId> {
25    fn as_ref(&self) -> &ScopeRef<ScopeId> {
26        self
27    }
28}
29
30impl<ScopeId> Hash for ScopeRef<ScopeId> {
31    fn hash<H: Hasher>(&self, state: &mut H) {
32        // id is intentionally not hashed, since it is only a performance hint
33        self.strs.hash(state)
34    }
35}
36
37impl<ScopeId> PartialEq for ScopeRef<ScopeId> {
38    fn eq(&self, other: &Self) -> bool {
39        // id is intentionally not compared, since it is only a performance hint
40        self.strs.eq(&other.strs)
41    }
42}
43
44impl<ScopeId> Display for ScopeRef<ScopeId> {
45    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
46        write!(f, "{}", self.strs.join("."))
47    }
48}