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 super::Span;
use std::{fmt, ops::Deref};
#[derive(Debug, Clone)]
pub struct Spanned<'f, T> {
pub span: Span<'f>,
pub data: T,
}
impl<'f, T> Eq for Spanned<'f, T> where T: Eq {}
impl<'f, T> PartialEq for Spanned<'f, T>
where
T: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
self.data == other.data
}
}
impl<'f, T> Deref for Spanned<'f, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.data
}
}
impl<T> fmt::Display for Spanned<'_, T>
where
T: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} at {}", self.data, self.span)
}
}
impl<'f, T> Spanned<'f, T> {
pub fn new(span: Span<'f>, value: T) -> Self {
Spanned { span, data: value }
}
pub fn map<U, F>(self, f: F) -> Spanned<'f, U>
where
F: FnOnce(T) -> U,
{
Spanned {
span: self.span,
data: f(self.data),
}
}
pub fn map_ref<U, F>(&self, f: F) -> Spanned<'f, U>
where
F: FnOnce(&T) -> U,
{
Spanned {
span: self.span,
data: f(&self.data),
}
}
pub fn combine_with<Rhs, Res, F>(self, rhs: Spanned<'f, Rhs>, f: F) -> Spanned<'f, Res>
where
F: FnOnce(Self, Spanned<'f, Rhs>) -> Res,
{
Spanned {
span: Span::combine(&self.span, &rhs.span),
data: f(self, rhs),
}
}
pub fn combine_with_boxed<Rhs, Res, F>(
self,
rhs: Box<Spanned<'f, Rhs>>,
f: F,
) -> Spanned<'f, Res>
where
F: FnOnce(Self, Box<Spanned<'f, Rhs>>) -> Res,
{
Spanned {
span: Span::combine(&self.span, &rhs.span),
data: f(self, rhs),
}
}
pub fn combine_boxed_with<Rhs, Res, F>(
self: Box<Self>,
rhs: Spanned<'f, Rhs>,
f: F,
) -> Spanned<'f, Res>
where
F: FnOnce(Box<Self>, Spanned<'f, Rhs>) -> Res,
{
rhs.combine_with_boxed(self, |lhs, rhs| f(rhs, lhs))
}
pub fn combine_boxed_with_boxed<Rhs, Res, F>(
self: Box<Self>,
rhs: Box<Spanned<'f, Rhs>>,
f: F,
) -> Spanned<'f, Res>
where
F: FnOnce(Box<Self>, Box<Spanned<'f, Rhs>>) -> Res,
{
Spanned {
span: Span::combine(&self.span, &rhs.span),
data: f(self, rhs),
}
}
}