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