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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
use crate::{
form::PortableForm,
interner::Interner,
prelude::{
fmt::Debug,
vec::Vec,
},
Registry,
Type,
};
use scale::Encode;
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(all(feature = "serde", feature = "decode"), derive(serde::Deserialize))]
#[cfg_attr(any(feature = "std", feature = "decode"), derive(scale::Decode))]
#[derive(Clone, Debug, PartialEq, Eq, Encode)]
pub struct PortableRegistry {
types: Vec<PortableType>,
}
impl From<Registry> for PortableRegistry {
fn from(registry: Registry) -> Self {
PortableRegistry {
types: registry
.types()
.map(|(k, v)| {
PortableType {
id: k.id(),
ty: v.clone(),
}
})
.collect::<Vec<_>>(),
}
}
}
impl PortableRegistry {
pub fn resolve(&self, id: u32) -> Option<&Type<PortableForm>> {
self.types.get(id as usize).map(|ty| ty.ty())
}
pub fn types(&self) -> &[PortableType] {
&self.types
}
}
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(all(feature = "serde", feature = "decode"), derive(serde::Deserialize))]
#[cfg_attr(any(feature = "std", feature = "decode"), derive(scale::Decode))]
#[derive(Clone, Debug, PartialEq, Eq, Encode)]
pub struct PortableType {
#[codec(compact)]
id: u32,
#[cfg_attr(feature = "serde", serde(rename = "type"))]
ty: Type<PortableForm>,
}
impl PortableType {
pub fn new(id: u32, ty: Type<PortableForm>) -> Self {
Self { id, ty }
}
pub fn id(&self) -> u32 {
self.id
}
pub fn ty(&self) -> &Type<PortableForm> {
&self.ty
}
}
#[derive(Debug, Default)]
pub struct PortableRegistryBuilder {
types: Interner<Type<PortableForm>>,
}
impl PortableRegistryBuilder {
pub fn new() -> Self {
Default::default()
}
pub fn register_type(&mut self, ty: Type<PortableForm>) -> u32 {
self.types.intern_or_get(ty).1.into_untracked().id()
}
pub fn next_type_id(&self) -> u32 {
self.types.elements().len() as u32
}
pub fn get(&self, id: u32) -> Option<&Type<PortableForm>> {
self.types.elements().get(id as usize)
}
pub fn finish(&self) -> PortableRegistry {
let types = self
.types
.elements()
.iter()
.enumerate()
.map(|(i, ty)| {
PortableType {
id: i as u32,
ty: ty.clone(),
}
})
.collect();
PortableRegistry { types }
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
build::*,
prelude::vec,
*,
};
#[test]
fn type_ids_are_sequential() {
let mut registry = Registry::new();
registry.register_type(&MetaType::new::<u32>());
registry.register_type(&MetaType::new::<bool>());
registry.register_type(&MetaType::new::<Option<(u32, bool)>>());
let readonly: PortableRegistry = registry.into();
assert_eq!(4, readonly.types().len());
for (expected, ty) in readonly.types().iter().enumerate() {
assert_eq!(expected as u32, ty.id());
}
}
#[test]
fn construct_portable_registry() {
let mut builder = PortableRegistryBuilder::new();
let u32_type = Type::new(Path::default(), vec![], TypeDefPrimitive::U32, vec![]);
let u32_type_id = builder.register_type(u32_type.clone());
let vec_u32_type = Type::new(
Path::default(),
vec![],
TypeDefSequence::new(u32_type_id.into()),
vec![],
);
let vec_u32_type_id = builder.register_type(vec_u32_type.clone());
let self_referential_type_id = builder.next_type_id();
let composite_type = Type::builder_portable()
.path(Path::from_segments_unchecked(["MyStruct".into()]))
.composite(
Fields::named()
.field_portable(|f| f.name("primitive".into()).ty(u32_type_id))
.field_portable(|f| f.name("vec_of_u32".into()).ty(vec_u32_type_id))
.field_portable(|f| {
f.name("self_referential".into())
.ty(self_referential_type_id)
}),
);
let composite_type_id = builder.register_type(composite_type.clone());
assert_eq!(self_referential_type_id, composite_type_id);
assert_eq!(builder.get(u32_type_id).unwrap(), &u32_type);
assert_eq!(builder.get(vec_u32_type_id).unwrap(), &vec_u32_type);
assert_eq!(builder.get(composite_type_id).unwrap(), &composite_type);
let registry = builder.finish();
assert_eq!(Some(&u32_type), registry.resolve(u32_type_id));
assert_eq!(Some(&vec_u32_type), registry.resolve(vec_u32_type_id));
assert_eq!(Some(&composite_type), registry.resolve(composite_type_id));
}
}