orx_concurrent_option/common_traits/from.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 55 56 57 58 59 60 61 62 63 64
use crate::ConcurrentOption;
// FROM
impl<T> From<T> for ConcurrentOption<T> {
    /// Wraps the existing value to a `ConcurrentOption` of Some variant.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use orx_concurrent_option::*;
    ///
    /// let x: ConcurrentOption<String> = 3.to_string().into();
    /// assert_eq!(unsafe { x.as_ref() }, Some(&3.to_string()));
    /// ```
    fn from(value: T) -> Self {
        ConcurrentOption::some(value)
    }
}
impl<T> From<Option<T>> for ConcurrentOption<T> {
    /// Converts an `Option` to a `ConcurrentOption`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use orx_concurrent_option::*;
    ///
    /// let x: ConcurrentOption<String> = Some(3.to_string()).into();
    /// assert_eq!(unsafe { x.as_ref() }, Some(&3.to_string()));
    ///
    /// let x: ConcurrentOption<String> = None.into();
    /// assert_eq!(unsafe { x.as_ref() }, None);
    /// ```
    fn from(value: Option<T>) -> Self {
        match value {
            Some(value) => ConcurrentOption::some(value),
            None => ConcurrentOption::none(),
        }
    }
}
// INTO
impl<T> From<ConcurrentOption<T>> for Option<T> {
    /// Converts a `ConcurrentOption` to a `Option`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use orx_concurrent_option::*;
    ///
    /// let x = ConcurrentOption::some(3.to_string());
    /// let y: Option<_> = x.into();
    /// assert_eq!(y, Some(3.to_string()));
    ///
    /// let x: ConcurrentOption<String> = ConcurrentOption::none();
    /// let y: Option<String> = x.into();
    /// assert_eq!(y, None);
    /// ```
    fn from(mut value: ConcurrentOption<T>) -> Self {
        value.exclusive_take()
    }
}