Skip to main content

interstice_abi/interstice_value/convert/
vec.rs

1use crate::{IntersticeAbiError, IntersticeValue};
2
3impl<T> Into<IntersticeValue> for Vec<T>
4where
5    T: Into<IntersticeValue>,
6{
7    fn into(self) -> IntersticeValue {
8        let values = self.into_iter().map(|x| x.into()).collect();
9        IntersticeValue::Vec(values)
10    }
11}
12impl<T> TryFrom<IntersticeValue> for Vec<T>
13where
14    T: TryFrom<IntersticeValue>,
15    T::Error: std::fmt::Display,
16{
17    type Error = IntersticeAbiError;
18
19    fn try_from(value: IntersticeValue) -> Result<Self, Self::Error> {
20        match value {
21            IntersticeValue::Vec(v) => v
22                .into_iter()
23                .map(|x| {
24                    T::try_from(x).map_err(|e| {
25                        IntersticeAbiError::ConversionError(format!(
26                            "Vec element conversion failed: {}",
27                            e
28                        ))
29                    })
30                })
31                .collect(),
32            _ => Err(IntersticeAbiError::ConversionError(
33                "Expected IntersticeValue::Vec".into(),
34            )),
35        }
36    }
37}