1#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
14pub struct Span {
15 pub start: usize,
17 pub end: usize,
19}
20
21impl Span {
22 #[must_use]
24 pub const fn new(start: usize, end: usize) -> Self {
25 Self { start, end }
26 }
27
28 #[must_use]
30 pub const fn empty(at: usize) -> Self {
31 Self { start: at, end: at }
32 }
33
34 #[must_use]
36 pub const fn len(&self) -> usize {
37 self.end.saturating_sub(self.start)
38 }
39
40 #[must_use]
42 pub const fn is_empty(&self) -> bool {
43 self.len() == 0
44 }
45}
46
47#[cfg(test)]
48mod tests {
49 use super::*;
50
51 #[test]
52 fn len_and_empty() {
53 assert_eq!(Span::new(2, 5).len(), 3);
54 assert!(Span::empty(4).is_empty());
55 assert_eq!(Span::empty(4), Span::new(4, 4));
56 assert_eq!(Span::new(5, 2).len(), 0);
57 assert!(!Span::new(0, 1).is_empty());
58 }
59}