Skip to main content

orx_concurrent_option/common_traits/
from.rs

1use crate::ConcurrentOption;
2
3// FROM
4
5impl<T> From<T> for ConcurrentOption<T> {
6    /// Wraps the existing value to a `ConcurrentOption` of Some variant.
7    ///
8    /// # Examples
9    ///
10    /// ```rust
11    /// use orx_concurrent_option::*;
12    ///
13    /// let x: ConcurrentOption<String> = 3.to_string().into();
14    /// assert_eq!(unsafe { x.as_ref() }, Some(&3.to_string()));
15    /// ```
16    fn from(value: T) -> Self {
17        ConcurrentOption::some(value)
18    }
19}
20
21impl<T> From<Option<T>> for ConcurrentOption<T> {
22    /// Converts an `Option` to a `ConcurrentOption`.
23    ///
24    /// # Examples
25    ///
26    /// ```rust
27    /// use orx_concurrent_option::*;
28    ///
29    /// let x: ConcurrentOption<String> = Some(3.to_string()).into();
30    /// assert_eq!(unsafe { x.as_ref() }, Some(&3.to_string()));
31    ///
32    /// let x: ConcurrentOption<String> = None.into();
33    /// assert_eq!(unsafe { x.as_ref() }, None);
34    /// ```
35    fn from(value: Option<T>) -> Self {
36        match value {
37            Some(value) => ConcurrentOption::some(value),
38            None => ConcurrentOption::none(),
39        }
40    }
41}
42
43// INTO
44
45impl<T> From<ConcurrentOption<T>> for Option<T> {
46    /// Converts a `ConcurrentOption` to a `Option`.
47    ///
48    /// # Examples
49    ///
50    /// ```rust
51    /// use orx_concurrent_option::*;
52    ///
53    /// let x = ConcurrentOption::some(3.to_string());
54    /// let y: Option<_> = x.into();
55    /// assert_eq!(y, Some(3.to_string()));
56    ///
57    /// let x: ConcurrentOption<String> = ConcurrentOption::none();
58    /// let y: Option<String> = x.into();
59    /// assert_eq!(y, None);
60    /// ```
61    fn from(mut value: ConcurrentOption<T>) -> Self {
62        value.exclusive_take()
63    }
64}