soroban_sdk/
tuple.rs

1//! This module contains conversion helpers for tuple
2
3use crate::{vec, ConversionError, Env, IntoVal, Topics, TryFromVal, Val, Vec};
4
5// Note that the way that this conversion of tuple to Vec<Val> is written results in a need for any
6// supported type to support converting from `&&` (double-ref) to `Val`. This means values that
7// convert to `Val` need to have an impl of TryFromVal converting not only from their owned value
8// (which converts from a ref of that owned value), but from a & value (which converts from a
9// double ref &&).
10//
11// This is why you'll see TryFromVal impls from &values, not just owned values.
12
13impl TryFromVal<Env, ()> for Vec<Val> {
14    type Error = ConversionError;
15
16    fn try_from_val(env: &Env, _v: &()) -> Result<Self, Self::Error> {
17        Ok(Vec::<Val>::new(env))
18    }
19}
20
21macro_rules! impl_into_vec_for_tuple {
22    ( $($typ:ident $idx:tt)* ) => {
23        impl<$($typ),*> TryFromVal<Env, ($($typ,)*)> for Vec<Val>
24        where
25            $($typ: IntoVal<Env, Val>),*
26        {
27            type Error = ConversionError;
28            fn try_from_val(env: &Env, v: &($($typ,)*)) -> Result<Self, Self::Error> {
29                Ok(vec![&env, $(v.$idx.into_val(env), )*])
30            }
31        }
32    };
33}
34impl_into_vec_for_tuple! { T0 0 }
35impl_into_vec_for_tuple! { T0 0 T1 1 }
36impl_into_vec_for_tuple! { T0 0 T1 1 T2 2 }
37impl_into_vec_for_tuple! { T0 0 T1 1 T2 2 T3 3 }
38impl_into_vec_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 }
39impl_into_vec_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 }
40impl_into_vec_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 }
41impl_into_vec_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 }
42impl_into_vec_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 }
43impl_into_vec_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 T9 9 }
44impl_into_vec_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 T9 9 T10 10 }
45impl_into_vec_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 T9 9 T10 10 T11 11 }
46impl_into_vec_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 T9 9 T10 10 T11 11 T12 12 }
47
48macro_rules! impl_topics_for_tuple {
49    ( $($typ:ident $idx:tt)* ) => {
50        impl<$($typ),*> Topics for ($($typ,)*)
51        where
52            $($typ: IntoVal<Env, Val>),*
53        {
54        }
55    };
56}
57
58// 0 topics
59impl Topics for () {}
60// 1-13 topics
61impl_topics_for_tuple! { T0 0 }
62impl_topics_for_tuple! { T0 0 T1 1 }
63impl_topics_for_tuple! { T0 0 T1 1 T2 2 }
64impl_topics_for_tuple! { T0 0 T1 1 T2 2 T3 3 }
65impl_topics_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 }
66impl_topics_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 }
67impl_topics_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 }
68impl_topics_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 }
69impl_topics_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 }
70impl_topics_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 T9 9 }
71impl_topics_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 T9 9 T10 10 }
72impl_topics_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 T9 9 T10 10 T11 11 }
73impl_topics_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 T9 9 T10 10 T11 11 T12 12 }