sway_core/decl_engine/
parsed_id.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use super::{
    parsed_engine::{ParsedDeclEngine, ParsedDeclEngineGet, ParsedDeclEngineIndex},
    DeclUniqueId,
};
use crate::{
    engine_threading::{
        EqWithEngines, HashWithEngines, PartialEqWithEngines, PartialEqWithEnginesContext,
    },
    Engines,
};
use serde::{Deserialize, Serialize};
use std::{
    hash::{DefaultHasher, Hasher},
    marker::PhantomData,
    {fmt, hash::Hash},
};
use sway_types::{Named, Spanned};

pub type ParsedDeclIdIndexType = usize;

/// An ID used to refer to an item in the [ParsedDeclEngine](super::decl_engine::ParsedDeclEngine)
pub struct ParsedDeclId<T>(ParsedDeclIdIndexType, PhantomData<T>);

impl<T> fmt::Debug for ParsedDeclId<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("ParsedDeclId").field(&self.0).finish()
    }
}

impl<T> ParsedDeclId<T> {
    pub(crate) fn inner(&self) -> ParsedDeclIdIndexType {
        self.0
    }

    pub fn unique_id(&self) -> DeclUniqueId
    where
        T: 'static,
    {
        let mut hasher = DefaultHasher::default();
        std::any::TypeId::of::<T>().hash(&mut hasher);
        self.0.hash(&mut hasher);

        DeclUniqueId(hasher.finish())
    }
}

impl<T> Copy for ParsedDeclId<T> {}
impl<T> Clone for ParsedDeclId<T> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<T> Eq for ParsedDeclId<T> {}
impl<T> PartialEq for ParsedDeclId<T> {
    fn eq(&self, other: &Self) -> bool {
        self.0.eq(&other.0)
    }
}

impl<T> EqWithEngines for ParsedDeclId<T> {}
impl<T> PartialEqWithEngines for ParsedDeclId<T> {
    fn eq(&self, other: &Self, _ctx: &PartialEqWithEnginesContext) -> bool {
        self.0 == other.0
    }
}

impl<T> Hash for ParsedDeclId<T> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.0.hash(state)
    }
}

impl<T> HashWithEngines for ParsedDeclId<T>
where
    ParsedDeclEngine: ParsedDeclEngineIndex<T>,
    T: Named + Spanned + HashWithEngines,
{
    fn hash<H: Hasher>(&self, state: &mut H, engines: &Engines) {
        let decl_engine = engines.pe();
        let decl = decl_engine.get(self);
        decl.name().hash(state);
        decl.hash(state, engines);
    }
}

impl<T> PartialOrd for ParsedDeclId<T> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}
impl<T> Ord for ParsedDeclId<T> {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.0.cmp(&other.0)
    }
}

impl<T> Serialize for ParsedDeclId<T> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        self.0.serialize(serializer)
    }
}

impl<'de, T> Deserialize<'de> for ParsedDeclId<T> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let id = usize::deserialize(deserializer)?;
        Ok(ParsedDeclId::new(id))
    }
}

impl<T> ParsedDeclId<T> {
    pub(crate) fn new(id: usize) -> Self {
        ParsedDeclId(id, PhantomData)
    }

    #[allow(dead_code)]
    pub(crate) fn replace_id(&mut self, index: Self) {
        self.0 = index.0;
    }

    #[allow(dead_code)]
    pub(crate) fn dummy() -> Self {
        // we assume that `usize::MAX` id is not possible in practice
        Self(usize::MAX, PhantomData)
    }
}

#[allow(clippy::from_over_into)]
impl<T> Into<usize> for ParsedDeclId<T> {
    fn into(self) -> usize {
        self.0
    }
}