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
#[derive(Debug, Clone, Copy)]
pub enum Lifespan {
Ephemeral(Span),
Immortal,
}
impl Lifespan {
pub fn is_alive(&self) -> bool {
match self {
Lifespan::Ephemeral(span) => span.length() > 0,
Lifespan::Immortal => true,
}
}
pub fn shorten(&mut self) -> &Self {
self.shorten_by(Span::with_length(1))
}
pub fn lengthen(&mut self) -> &Self {
self.lengthen_by(Span::with_length(1))
}
pub fn shorten_by(&mut self, amount: Span) -> &Self {
if let Lifespan::Ephemeral(span) = self {
span.shorten_by(amount.into());
}
self
}
pub fn lengthen_by(&mut self, amount: Span) -> &Self {
if let Lifespan::Ephemeral(span) = self {
span.lengthen_by(amount.into());
}
self
}
pub fn clear(&mut self) {
*self = Lifespan::Ephemeral(Span::empty())
}
}
#[derive(Debug, Clone, Copy)]
pub struct Span {
length: u64,
}
impl From<u64> for Span {
fn from(length: u64) -> Self {
Self { length }
}
}
impl From<Span> for u64 {
fn from(span: Span) -> Self {
span.length
}
}
impl Span {
pub fn with_length(length: u64) -> Self {
Self { length }
}
pub fn empty() -> Self {
Self { length: 0 }
}
pub fn length(self) -> u64 {
self.length
}
pub fn shorten_by(&mut self, length: u64) {
self.length = self.length.saturating_sub(length);
}
pub fn lengthen_by(&mut self, length: u64) {
self.length = self.length.saturating_add(length);
}
pub fn shorten(&mut self) {
self.shorten_by(1);
}
pub fn lengthen(&mut self) {
self.lengthen_by(1);
}
pub fn clear(&mut self) {
self.length = 0;
}
}