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
use std::convert::{
AsRef,
AsMut
};
use std::ops::{
Deref,
DerefMut
};
use std::hash::{
Hash,
Hasher
};
use std::cmp::{
Ord,
PartialOrd,
Ordering
};
use std::convert::TryInto;
use std::fmt;
use crate::Span;
pub struct Loc<T: ?Sized> {
span: Span,
value: T
}
impl<T: ?Sized> Loc<T> {
pub fn new(t: T, span: Span) -> Loc<T> where T: Sized {
Self {
span: span,
value: t
}
}
pub fn span(&self) -> Span {
self.span
}
pub fn map<U, F>(self, f: F) -> Loc<U> where F: FnOnce(T) -> U, T: Sized {
Loc {
span: self.span,
value: f(self.value)
}
}
pub fn inner_into<U>(self) -> Loc<U> where T: Into<U> {
Loc {
span: self.span,
value: self.value.into()
}
}
pub fn try_map<U, F, E>(self, f: F) -> Result<Loc<U>, E> where F: FnOnce(T) -> Result<U, E>, T: Sized {
Ok(Loc {
span: self.span,
value: f(self.value)?
})
}
pub fn inner_try_into<U>(self) -> Result<Loc<U>, <T as TryInto<U>>::Error> where T: TryInto<U> {
Ok(Loc {
span: self.span,
value: self.value.try_into()?
})
}
pub fn into_inner(self) -> T where T: Sized {
self.value
}
pub fn into_raw_parts(self) -> (T, Span) where T: Sized {
(self.value, self.span)
}
}
impl<T> Loc<Option<T>> {
pub fn transposed(t: Option<Loc<T>>, span: Span) -> Self {
match t {
Some(t) => t.map(|t| Some(t)),
None => Self::new(None, span)
}
}
}
impl<T: Clone> Clone for Loc<T> {
fn clone(&self) -> Loc<T> {
Loc {
span: self.span,
value: self.value.clone()
}
}
}
impl<T> AsRef<T> for Loc<T> {
fn as_ref(&self) -> &T {
&self.value
}
}
impl<T> AsMut<T> for Loc<T> {
fn as_mut(&mut self) -> &mut T {
&mut self.value
}
}
impl<T: fmt::Display> fmt::Display for Loc<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.value.fmt(f)
}
}
impl<T: fmt::Debug> fmt::Debug for Loc<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}:{}", self.value, self.span)
}
}
impl<T: Hash> Hash for Loc<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.span.hash(state);
self.value.hash(state);
}
}
impl<T: PartialEq> PartialEq for Loc<T> {
fn eq(&self, other: &Loc<T>) -> bool {
self.span == other.span && self.value == other.value
}
}
impl<T: Eq> Eq for Loc<T> {}
impl<T: PartialOrd> PartialOrd for Loc<T> {
fn partial_cmp(&self, other: &Loc<T>) -> Option<Ordering> {
self.value.partial_cmp(&other.value)
}
}
impl<T: Ord> Ord for Loc<T> {
fn cmp(&self, other: &Loc<T>) -> Ordering {
self.value.cmp(&other.value)
}
}
impl<T> Deref for Loc<T> {
type Target = T;
fn deref(&self) -> &T {
&self.value
}
}
impl<T> DerefMut for Loc<T> {
fn deref_mut(&mut self) -> &mut T {
&mut self.value
}
}