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
use crate::{Hash, StaticType};
use std::cmp;
use std::fmt;
use std::hash;
#[derive(Debug, Clone, Copy)]
pub enum ValueType {
StaticType(&'static StaticType),
Type(Hash),
}
impl ValueType {
pub fn as_type_hash(&self) -> Hash {
match self {
Self::StaticType(ty) => ty.hash,
Self::Type(hash) => *hash,
}
}
}
impl cmp::PartialEq for ValueType {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::StaticType(a), b) => match b {
Self::StaticType(b) => a.eq(b),
Self::Type(b) => &a.hash == b,
},
(Self::Type(a), b) => match b {
Self::StaticType(b) => a == &b.hash,
Self::Type(b) => a == b,
},
}
}
}
impl cmp::PartialEq<Hash> for ValueType {
fn eq(&self, other: &Hash) -> bool {
match self {
Self::StaticType(a) => &a.hash == other,
Self::Type(hash) => hash == other,
}
}
}
impl cmp::Eq for ValueType {}
impl hash::Hash for ValueType {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
match self {
Self::StaticType(ty) => ty.hash.hash(state),
Self::Type(hash) => hash.hash(state),
}
}
}
impl fmt::Display for ValueType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::StaticType(ty) => write!(f, "type({})", ty.name),
Self::Type(hash) => write!(f, "type({})", hash),
}
}
}
#[cfg(test)]
mod tests {
use super::ValueType;
#[test]
fn test_size() {
assert_eq! {
std::mem::size_of::<ValueType>(),
16,
};
}
}