Skip to main content

eosio_scale_info/ty/
composite.rs

1// Copyright 2019-2022 Parity Technologies (UK) Ltd.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use 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/// A composite type, consisting of either named (struct) or unnamed (tuple
37/// struct) fields
38///
39/// # Examples
40///
41/// ## A Rust struct with named fields.
42///
43/// ```
44/// struct Person {
45///     name: String,
46///     age_in_years: u8,
47///     friends: Vec<Person>,
48/// }
49/// ```
50///
51/// ## A tuple struct with unnamed fields.
52///
53/// ```
54/// struct Color(u8, u8, u8);
55/// ```
56///
57/// ## A so-called unit struct
58///
59/// ```
60/// struct JustAMarker;
61/// ```
62#[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    /// The fields of the composite type.
75    #[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    /// Creates a new struct definition with named fields.
94    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    /// Returns the fields of the composite type.
109    pub fn fields(&self) -> &[Field<T>] {
110        &self.fields
111    }
112}