rune_alloc/option/
ext.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
use crate::clone::TryClone;
use crate::error::Error;

/// Extensions to `Option<T>`.
pub trait OptionExt<T> {
    /// Maps an `Option<&T>` to an `Option<T>` by cloning the contents of the
    /// option.
    ///
    /// # Examples
    ///
    /// ```
    /// use rune::alloc::prelude::*;
    ///
    /// let x = 12u32;
    /// let opt_x = Some(&x);
    /// assert_eq!(opt_x, Some(&12));
    /// let cloned = opt_x.try_cloned()?;
    /// assert_eq!(cloned, Some(12u32));
    /// # Ok::<_, rune::alloc::Error>(())
    /// ```
    #[must_use = "`self` will be dropped if the result is not used"]
    fn try_cloned(self) -> Result<Option<T>, Error>;
}

impl<T> OptionExt<T> for Option<&T>
where
    T: TryClone,
{
    fn try_cloned(self) -> Result<Option<T>, Error> {
        Ok(match self {
            Some(value) => Some(value.try_clone()?),
            None => None,
        })
    }
}