orx_concurrent_option/
new.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use crate::concurrent_option::ConcurrentOption;
use crate::states::*;
use core::mem::MaybeUninit;

impl<T> ConcurrentOption<T> {
    /// Creates a concurrent option of the Some variant with an existing value.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use orx_concurrent_option::*;
    ///
    /// let x = ConcurrentOption::some(3.to_string());
    /// assert_eq!(x, ConcurrentOption::some(3.to_string()));
    /// assert_ne!(x, ConcurrentOption::none());
    ///
    /// assert!(x.is_some());
    /// assert!(!x.is_none());
    /// ```
    pub fn some(value: T) -> Self {
        Self {
            value: MaybeUninit::new(value).into(),
            state: SOME.into(),
        }
    }

    /// Creates a concurrent option of the None variant with a missing value.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use orx_concurrent_option::*;
    ///
    /// let x = ConcurrentOption::<String>::none();
    /// assert_ne!(x, ConcurrentOption::some(3.to_string()));
    /// assert_eq!(x, ConcurrentOption::none());
    /// assert!(!x.is_some());
    /// assert!(x.is_none());
    ///
    /// let x = ConcurrentOption::default();
    /// assert_ne!(x, ConcurrentOption::some(3.to_string()));
    /// assert_eq!(x, ConcurrentOption::none());
    /// assert!(!x.is_some());
    /// assert!(x.is_none());
    /// ```
    pub fn none() -> Self {
        let value = MaybeUninit::uninit();
        let value = unsafe { value.assume_init() };
        Self {
            value,
            state: NONE.into(),
        }
    }
}