orx_concurrent_option/
states.rs

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
use core::sync::atomic::Ordering;

/// State represented as u8.
pub type StateU8 = u8;

pub const ORDER_LOAD: Ordering = Ordering::Acquire;
pub const ORDER_STORE: Ordering = Ordering::SeqCst;

/// State where the optional does not have a value.
pub const NONE: StateU8 = 0;
/// State where the optional's value is being transitioned.
pub const RESERVED: StateU8 = 1;
/// State where the optional contains a value.
pub const SOME: StateU8 = 2;

/// Concurrent state of the optional.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum State {
    /// Optional has no value.
    None,
    /// Optional has some value.
    Some,
    /// Optional is currently reserved for a mutation.
    Reserved,
}

impl State {
    #[allow(clippy::panic, clippy::missing_panics_doc)]
    pub(crate) fn new(state: StateU8) -> Self {
        match state {
            NONE => Self::None,
            SOME => Self::Some,
            RESERVED => Self::Reserved,
            _ => panic!("should be either of the three valid states"),
        }
    }
}