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
use std::hash::{DefaultHasher, Hasher};
use std::marker::PhantomData;
use std::{fmt, hash::Hash};

use sway_types::{Named, Spanned};

use crate::engine_threading::{
    EqWithEngines, HashWithEngines, PartialEqWithEngines, PartialEqWithEnginesContext,
};
use crate::Engines;

use super::parsed_engine::{ParsedDeclEngine, ParsedDeclEngineGet, ParsedDeclEngineIndex};
use super::DeclUniqueId;

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> 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
    }
}