Skip to main content

orx_concurrent_option/
new.rs

1use crate::concurrent_option::ConcurrentOption;
2use crate::states::*;
3use core::mem::MaybeUninit;
4
5impl<T> ConcurrentOption<T> {
6    /// Creates a concurrent option of the Some variant with an existing value.
7    ///
8    /// # Examples
9    ///
10    /// ```rust
11    /// use orx_concurrent_option::*;
12    ///
13    /// let x = ConcurrentOption::some(3.to_string());
14    /// assert_eq!(x, ConcurrentOption::some(3.to_string()));
15    /// assert_ne!(x, ConcurrentOption::none());
16    ///
17    /// assert!(x.is_some());
18    /// assert!(!x.is_none());
19    /// ```
20    pub fn some(value: T) -> Self {
21        Self {
22            value: MaybeUninit::new(value).into(),
23            state: SOME.into(),
24        }
25    }
26
27    /// Creates a concurrent option of the None variant with a missing value.
28    ///
29    /// # Examples
30    ///
31    /// ```rust
32    /// use orx_concurrent_option::*;
33    ///
34    /// let x = ConcurrentOption::<String>::none();
35    /// assert_ne!(x, ConcurrentOption::some(3.to_string()));
36    /// assert_eq!(x, ConcurrentOption::none());
37    /// assert!(!x.is_some());
38    /// assert!(x.is_none());
39    ///
40    /// let x = ConcurrentOption::default();
41    /// assert_ne!(x, ConcurrentOption::some(3.to_string()));
42    /// assert_eq!(x, ConcurrentOption::none());
43    /// assert!(!x.is_some());
44    /// assert!(x.is_none());
45    /// ```
46    pub fn none() -> Self {
47        let value = MaybeUninit::uninit();
48        let value = unsafe { value.assume_init() };
49        Self {
50            value,
51            state: NONE.into(),
52        }
53    }
54}