soroban_env_common/
option.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
use crate::{Env, TryFromVal, TryIntoVal, Val};

impl<E: Env, T> TryFromVal<E, Val> for Option<T>
where
    T: TryFromVal<E, Val>,
{
    type Error = T::Error;

    fn try_from_val(env: &E, val: &Val) -> Result<Self, Self::Error> {
        if val.is_void() {
            Ok(None)
        } else {
            Ok(Some(T::try_from_val(env, val)?))
        }
    }
}

impl<E: Env, T> TryFromVal<E, Option<T>> for Val
where
    T: TryIntoVal<E, Val>,
{
    type Error = T::Error;

    fn try_from_val(env: &E, v: &Option<T>) -> Result<Self, Self::Error> {
        match v {
            Some(t) => t.try_into_val(env),
            None => Ok(Val::VOID.into()),
        }
    }
}

impl<E: Env, T> TryFromVal<E, &Option<T>> for Val
where
    T: TryIntoVal<E, Val>,
{
    type Error = T::Error;

    fn try_from_val(env: &E, v: &&Option<T>) -> Result<Self, Self::Error> {
        Self::try_from_val(env, *v)
    }
}