eosio_scale_info/ty/
composite.rs1use crate::prelude::vec::Vec;
16
17use crate::{
18 form::{
19 Form,
20 MetaForm,
21 PortableForm,
22 },
23 Field,
24 IntoPortable,
25 Registry,
26};
27use derive_more::From;
28use scale::Encode;
29#[cfg(feature = "serde")]
30use serde::{
31 de::DeserializeOwned,
32 Deserialize,
33 Serialize,
34};
35
36#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
63#[cfg_attr(
64 feature = "serde",
65 serde(bound(
66 serialize = "T::Type: Serialize, T::String: Serialize",
67 deserialize = "T::Type: DeserializeOwned, T::String: DeserializeOwned",
68 ))
69)]
70#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
71#[cfg_attr(any(feature = "std", feature = "decode"), derive(scale::Decode))]
72#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, From, Encode)]
73pub struct TypeDefComposite<T: Form = MetaForm> {
74 #[cfg_attr(
76 feature = "serde",
77 serde(skip_serializing_if = "Vec::is_empty", default)
78 )]
79 fields: Vec<Field<T>>,
80}
81
82impl IntoPortable for TypeDefComposite {
83 type Output = TypeDefComposite<PortableForm>;
84
85 fn into_portable(self, registry: &mut Registry) -> Self::Output {
86 TypeDefComposite {
87 fields: registry.map_into_portable(self.fields),
88 }
89 }
90}
91
92impl TypeDefComposite {
93 pub(crate) fn new<I>(fields: I) -> Self
95 where
96 I: IntoIterator<Item = Field>,
97 {
98 Self {
99 fields: fields.into_iter().collect(),
100 }
101 }
102}
103
104impl<T> TypeDefComposite<T>
105where
106 T: Form,
107{
108 pub fn fields(&self) -> &[Field<T>] {
110 &self.fields
111 }
112}