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
use std::fmt::{self, Debug, Formatter};
use std::sync::atomic::{AtomicU8, Ordering};
#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(u8)]
pub enum State {
Uninitialized = 0,
Initializing = 1,
Initialized = 2,
ShutDown = 3,
Exited = 4,
}
pub struct ServerState(AtomicU8);
impl ServerState {
#[inline]
pub const fn new() -> Self {
ServerState(AtomicU8::new(State::Uninitialized as u8))
}
#[inline]
pub fn set(&self, state: State) {
self.0.store(state as u8, Ordering::SeqCst);
}
#[inline]
pub fn get(&self) -> State {
match self.0.load(Ordering::SeqCst) {
0 => State::Uninitialized,
1 => State::Initializing,
2 => State::Initialized,
3 => State::ShutDown,
4 => State::Exited,
_ => unreachable!(),
}
}
}
impl Debug for ServerState {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
self.get().fmt(f)
}
}