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
/// A value that can be marked as live or erased while preserving the underlying data.
///
/// This enum allows tracking the state of a value without losing access to it.
/// Values can be moved between live and erased states, and the underlying data
/// can be extracted regardless of state using `unjail`.
#[derive(Debug, PartialEq, Eq)]
pub enum DeadOrAlive<T> {
/// A live value that is actively being used.
Live(T),
/// An erased value that is marked as inactive but still accessible.
Erased(T),
}
impl<T> DeadOrAlive<T> {
/// Returns `true` if the value is in the live state.
pub fn is_live(&self) -> bool {
matches!(self, DeadOrAlive::Live(_))
}
/// Returns `true` if the value is in the erased state.
pub fn is_erased(&self) -> bool {
matches!(self, DeadOrAlive::Erased(_))
}
/// Converts from `&DeadOrAlive<T>` to `DeadOrAlive<&T>`.
pub fn as_ref(&self) -> DeadOrAlive<&T> {
match self {
DeadOrAlive::Live(v) => DeadOrAlive::Live(v),
DeadOrAlive::Erased(v) => DeadOrAlive::Erased(v),
}
}
/// Converts from `&mut DeadOrAlive<T>` to `DeadOrAlive<&mut T>`.
pub fn as_mut(&mut self) -> DeadOrAlive<&mut T> {
match self {
DeadOrAlive::Live(v) => DeadOrAlive::Live(v),
DeadOrAlive::Erased(v) => DeadOrAlive::Erased(v),
}
}
/// Extracts the value from a live state.
///
/// # Panics
///
/// Panics if the value is in the erased state.
pub fn unwrap(self) -> T {
match self {
DeadOrAlive::Live(v) => v,
DeadOrAlive::Erased(_) => panic!("Tried to unwrap an erased value"),
}
}
/// Extracts the value from an erased state.
///
/// # Panics
///
/// Panics if the value is in the live state.
pub fn unwrap_erased(self) -> T {
match self {
DeadOrAlive::Erased(v) => v,
DeadOrAlive::Live(_) => panic!("Tried to unwrap_erased a live value"),
}
}
/// Extracts the underlying value regardless of its state.
pub fn unjail(self) -> T {
match self {
DeadOrAlive::Erased(v) => v,
DeadOrAlive::Live(v) => v,
}
}
/// Moves a live value to the erased state.
///
/// If the value is already erased, this method has no effect.
pub fn erase(&mut self) {
if self.is_live() {
let DeadOrAlive::Live(value) = std::mem::replace(self, unsafe { std::mem::zeroed() })
else {
unreachable!()
};
*self = DeadOrAlive::Erased(value);
}
}
}