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
use crate::tm_std::*;
use crate::{
form::{CompactForm, Form},
interner::{Interner, UntrackedSymbol},
meta_type::MetaType,
Type,
};
use scale::{Decode, Encode};
use serde::{Deserialize, Serialize};
pub trait IntoCompact {
type Output;
fn into_compact(self, registry: &mut Registry) -> Self::Output;
}
impl IntoCompact for &'static str {
type Output = <CompactForm as Form>::String;
fn into_compact(self, _registry: &mut Registry) -> Self::Output {
self.to_string()
}
}
#[derive(Debug, PartialEq, Eq, Serialize)]
pub struct Registry {
#[serde(skip)]
type_table: Interner<TypeId>,
#[serde(serialize_with = "serialize_registry_types")]
types: BTreeMap<UntrackedSymbol<core::any::TypeId>, Type<CompactForm>>,
}
fn serialize_registry_types<S>(
types: &BTreeMap<UntrackedSymbol<core::any::TypeId>, Type<CompactForm>>,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let types = types.values().collect::<Vec<_>>();
types.serialize(serializer)
}
impl Default for Registry {
fn default() -> Self {
Self::new()
}
}
impl Encode for Registry {
fn size_hint(&self) -> usize {
mem::size_of::<u32>() + mem::size_of::<Type<CompactForm>>() * self.types.len()
}
fn encode_to<W: scale::Output>(&self, dest: &mut W) {
if self.types.len() > u32::max_value() as usize {
panic!("Attempted to encode too many elements.");
}
scale::Compact(self.types.len() as u32).encode_to(dest);
for ty in self.types.values() {
ty.encode_to(dest);
}
}
}
impl Registry {
pub fn new() -> Self {
Self {
type_table: Interner::new(),
types: BTreeMap::new(),
}
}
fn intern_type_id(&mut self, type_id: TypeId) -> (bool, UntrackedSymbol<TypeId>) {
let (inserted, symbol) = self.type_table.intern_or_get(type_id);
(inserted, symbol.into_untracked())
}
pub fn register_type(&mut self, ty: &MetaType) -> UntrackedSymbol<TypeId> {
let (inserted, symbol) = self.intern_type_id(ty.type_id());
if inserted {
let compact_id = ty.type_info().into_compact(self);
self.types.insert(symbol, compact_id);
}
symbol
}
pub fn register_types<I>(&mut self, iter: I) -> Vec<UntrackedSymbol<TypeId>>
where
I: IntoIterator<Item = MetaType>,
{
iter.into_iter().map(|i| self.register_type(&i)).collect::<Vec<_>>()
}
pub fn map_into_compact<I, T>(&mut self, iter: I) -> Vec<T::Output>
where
I: IntoIterator<Item = T>,
T: IntoCompact,
{
iter.into_iter().map(|i| i.into_compact(self)).collect::<Vec<_>>()
}
}
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Decode)]
pub struct RegistryReadOnly {
types: Vec<Type<CompactForm>>,
}
impl From<Registry> for RegistryReadOnly {
fn from(registry: Registry) -> Self {
RegistryReadOnly {
types: registry.types.values().cloned().collect::<Vec<_>>(),
}
}
}
impl RegistryReadOnly {
pub fn resolve(&self, id: NonZeroU32) -> Option<&Type<CompactForm>> {
self.types.get((id.get() - 1) as usize)
}
}