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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
use std::{
cmp::Ordering,
fmt,
hash::{Hash, Hasher},
};
use crate::{decl_engine::DeclEngine, TypeEngine};
#[derive(Clone, Copy)]
pub struct Engines<'a> {
type_engine: &'a TypeEngine,
decl_engine: &'a DeclEngine,
}
impl<'a> Engines<'a> {
pub fn new(type_engine: &'a TypeEngine, decl_engine: &'a DeclEngine) -> Engines<'a> {
Engines {
type_engine,
decl_engine,
}
}
pub fn te(&self) -> &TypeEngine {
self.type_engine
}
pub fn de(&self) -> &DeclEngine {
self.decl_engine
}
pub(crate) fn unwrap(self) -> (&'a TypeEngine, &'a DeclEngine) {
(self.type_engine, self.decl_engine)
}
pub fn help_out<T>(&self, thing: T) -> WithEngines<'_, T> {
WithEngines {
thing,
engines: *self,
}
}
pub fn with_thing<T>(self, thing: T) -> WithEngines<'a, T> {
WithEngines {
thing,
engines: self,
}
}
}
#[derive(Clone, Copy)]
pub struct WithEngines<'a, T> {
pub thing: T,
pub engines: Engines<'a>,
}
impl<'a, T> WithEngines<'a, T> {
pub fn new(thing: T, engines: Engines<'a>) -> Self {
WithEngines { thing, engines }
}
}
impl<T: DisplayWithEngines> fmt::Display for WithEngines<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.thing.fmt(f, self.engines)
}
}
impl<T: DebugWithEngines> fmt::Debug for WithEngines<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.thing.fmt(f, self.engines)
}
}
impl<T: HashWithEngines> Hash for WithEngines<'_, T> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.thing.hash(state, self.engines.te())
}
}
impl<T: PartialEqWithEngines> PartialEq for WithEngines<'_, T> {
fn eq(&self, rhs: &Self) -> bool {
self.thing.eq(&rhs.thing, self.engines)
}
}
impl<T: EqWithEngines> Eq for WithEngines<'_, T> {}
pub(crate) trait DisplayWithEngines {
fn fmt(&self, f: &mut fmt::Formatter<'_>, engines: Engines<'_>) -> fmt::Result;
}
impl<T: DisplayWithEngines> DisplayWithEngines for &T {
fn fmt(&self, f: &mut fmt::Formatter<'_>, engines: Engines<'_>) -> fmt::Result {
(*self).fmt(f, engines)
}
}
pub(crate) trait DebugWithEngines {
fn fmt(&self, f: &mut fmt::Formatter<'_>, engines: Engines<'_>) -> fmt::Result;
}
impl<T: DebugWithEngines> DebugWithEngines for &T {
fn fmt(&self, f: &mut fmt::Formatter<'_>, engines: Engines<'_>) -> fmt::Result {
(*self).fmt(f, engines)
}
}
pub trait HashWithEngines {
fn hash<H: Hasher>(&self, state: &mut H, type_engine: &TypeEngine);
}
impl<T: HashWithEngines + ?Sized> HashWithEngines for &T {
fn hash<H: Hasher>(&self, state: &mut H, type_engine: &TypeEngine) {
(*self).hash(state, type_engine)
}
}
impl<T: HashWithEngines> HashWithEngines for Option<T> {
fn hash<H: Hasher>(&self, state: &mut H, type_engine: &TypeEngine) {
match self {
None => state.write_u8(0),
Some(x) => x.hash(state, type_engine),
}
}
}
impl<T: HashWithEngines> HashWithEngines for [T] {
fn hash<H: Hasher>(&self, state: &mut H, type_engine: &TypeEngine) {
for x in self {
x.hash(state, type_engine)
}
}
}
pub trait EqWithEngines: PartialEqWithEngines {}
pub trait PartialEqWithEngines {
fn eq(&self, other: &Self, engines: Engines<'_>) -> bool;
}
pub trait OrdWithEngines {
fn cmp(&self, rhs: &Self, type_engine: &TypeEngine) -> Ordering;
}
impl<T: EqWithEngines + ?Sized> EqWithEngines for &T {}
impl<T: PartialEqWithEngines + ?Sized> PartialEqWithEngines for &T {
fn eq(&self, other: &Self, engines: Engines<'_>) -> bool {
(*self).eq(*other, engines)
}
}
impl<T: OrdWithEngines + ?Sized> OrdWithEngines for &T {
fn cmp(&self, rhs: &Self, type_engine: &TypeEngine) -> Ordering {
(*self).cmp(*rhs, type_engine)
}
}
impl<T: EqWithEngines> EqWithEngines for Option<T> {}
impl<T: PartialEqWithEngines> PartialEqWithEngines for Option<T> {
fn eq(&self, other: &Self, engines: Engines<'_>) -> bool {
match (self, other) {
(None, None) => true,
(Some(x), Some(y)) => x.eq(y, engines),
_ => false,
}
}
}
impl<T: EqWithEngines> EqWithEngines for [T] {}
impl<T: PartialEqWithEngines> PartialEqWithEngines for [T] {
fn eq(&self, other: &Self, engines: Engines<'_>) -> bool {
self.len() == other.len() && self.iter().zip(other.iter()).all(|(x, y)| x.eq(y, engines))
}
}
impl<T: OrdWithEngines> OrdWithEngines for [T] {
fn cmp(&self, rhs: &Self, type_engine: &TypeEngine) -> Ordering {
self.iter()
.zip(rhs.iter())
.map(|(x, y)| x.cmp(y, type_engine))
.find(|o| o.is_ne())
.unwrap_or_else(|| self.len().cmp(&rhs.len()))
}
}