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
//! Utilities for mapping source code to intermediate representations.
#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Span {
pub file_id: u32,
pub start: u32,
pub end: u32,
}
impl Span {
/// Convenience method to construct a [Span] with [usize]s instead of [u32].
///
/// # Panics
/// Panics if *file_id*, *start* or *end* exceed `u32::MAX`.
#[inline]
pub fn new(file_id: usize, start: usize, end: usize) -> Self {
Self {
file_id: file_id as u32,
start: start as u32,
end: end as u32,
}
}
/// Returns the ID of the file which the [Span] originated in.
#[inline]
pub fn file_id(&self) -> usize {
self.file_id as usize
}
/// Returns the starting position of the span.
#[inline]
pub fn start(&self) -> usize {
self.start as usize
}
/// Returns the ending position of the span.
#[inline]
pub fn end(&self) -> usize {
self.end as usize
}
/// Returns a [Span] after this [Span].
#[inline]
pub fn next(&self) -> Span {
Span::new(self.file_id(), self.end(), self.end() + 1)
}
}
impl From<Span> for std::ops::Range<usize> {
fn from(span: Span) -> Self {
span.start()..span.end()
}
}
/// A trait for objects with a [Span].
pub trait Spanned {
/// Returns the [Span] of this object.
fn span(&self) -> Span;
}