You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
let uint32 =trySCALE.default.decode(UInt32.self, from: data)
63
+
let uint32 =trydecode(UInt32.self, from: data)
64
64
assert(uint32 ==UInt32.max)
65
65
```
66
66
@@ -77,20 +77,20 @@ import ScaleCodec
77
77
78
78
let data =Data([0x07, 0x00, 0x00, 0x00, 0x00, 0x01])
79
79
80
-
let encoded =trySCALE.default.encode(UInt64(1<<32), .compact)
80
+
let encoded =tryencode(UInt64(1<<32), .compact)
81
81
assert(encoded == data))
82
82
83
-
let compact =trySCALE.default.decode(UInt64.self, .compact, from: data)
83
+
let compact =trydecode(UInt64.self, .compact, from: data)
84
84
assert(compact ==UInt64(1<<32))
85
85
86
-
// without helper methods
87
-
// let encoded = try SCALE.default.encode(SCompact(UInt64(1 << 32)))
88
-
// let compact = try SCALE.default.decode(SCompact<UInt64>.self, from: data).value
86
+
// without custom encoding methods
87
+
// let encoded = try encode(Compact(UInt64(1 << 32)))
88
+
// let compact = try decode(Compact<UInt64>.self, from: data).value
89
89
```
90
90
91
-
#### Int[128-512] and UInt[128-512]
91
+
#### Int[128-1024] and UInt[128-1024]
92
92
93
-
`Int[128-512]` and `UInt[128-512]` types implemented as `BigInt` and `BigUInt` Swift types. For proper encoding ScaleCodec has `SInt[128-512]` and `SUInt[128-512]` wrappers. Decoder and encoder has extension methods for simpler usage.
93
+
`Int[128-1024]` and `UInt[128-1024]` types implemented with `DoubleWidth` Swift type from Apple. It works fine for 128-256 bits but slow for 512-1024 bits.
94
94
95
95
```Swift
96
96
importScaleCodec
@@ -100,15 +100,11 @@ let data = Data([
100
100
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
101
101
])
102
102
103
-
let encoded =trySCALE.default.encode(BigUInt(2).pow(128)-1, .b128)
103
+
let encoded =tryencode(UInt128(UInt256(2)^128-1))
104
104
assert(encoded == data))
105
105
106
-
let compact =try SCALE.default.decode(BigUInt.self, .b128, from: data)
107
-
assert(compact ==BigUInt(2).pow(128) -1)
108
-
109
-
// without helper methods
110
-
// let encoded = try SCALE.default.encode(SUInt256(BigUInt(2).pow(128) - 1))
111
-
// let compact = try SCALE.default.decode(SUInt256.self, from: data).int
106
+
let compact =trydecode(UInt128.self, from: data)
107
+
assert(compact ==UInt128(UInt256(2)^128-1))
112
108
```
113
109
114
110
#### Data fixed encoding
@@ -120,11 +116,13 @@ import ScaleCodec
120
116
121
117
let data =Data([0x07, 0x00, 0x00, 0x00, 0x00, 0x01]
122
118
123
-
let encoded =try SCALE.default.encoder().encode(data, .fixed(6)).output
124
-
assert(encoded == data))
119
+
var encoder =encoder()
120
+
try encoder.encode(data, .fixed(6))
121
+
assert(encoder.output== data))
125
122
126
-
let decoded =try SCALE.default.decoder(data: encoded).decode(Data.self, .fixed(6))
127
-
assert(decoded == encoded == data)
123
+
var decoder =decoder(from: encoder.output)
124
+
let decoded =try decoder.decode(Data.self, .fixed(6))
125
+
assert(decoded == encoder.output== data)
128
126
```
129
127
130
128
### Container types
@@ -136,9 +134,9 @@ import ScaleCodec
136
134
137
135
let array: [UInt32] = [1, 2, 3, 4, 5]
138
136
139
-
let data =trySCALE.default.encode(array)
137
+
let data =tryencode(array)
140
138
141
-
let decoded: [UInt32] =trySCALE.default.decode(from: data)
139
+
let decoded: [UInt32] =trydecode(from: data)
142
140
143
141
assert(array == decoded)
144
142
```
@@ -152,32 +150,31 @@ import ScaleCodec
152
150
153
151
let array: [UInt32] = [1, 2, 3, 4, 5]
154
152
155
-
let data =trySCALE.default.encode(array, .fixed(5))
153
+
let data =tryencode(array, .fixed(5))
156
154
157
-
let decoded: [UInt32] =trySCALE.default.decode(.fixed(5), from: data)
155
+
let decoded: [UInt32] =trydecode(.fixed(5), from: data)
158
156
159
157
assert(array == decoded)
160
158
```
161
159
162
-
163
160
### Tuples
164
161
165
-
Tuple encoding and decoding supported through `STuple*` set of wrappers. ScaleCodec provides `STuple()` helper which can create approptiate `STuple*` instance for a tuple. `STuple*` wrappers can be nested to support bigger tuples. ScaleCodec also has set of helper methods for tuples support.
162
+
Tuple encoding and decoding supported through `Tuple*` set of wrappers. ScaleCodec provides `Tuple()` helper which can create approptiate `Tuple*` instance for a tuple. `Tuple*` wrappers can be nested to support bigger tuples. ScaleCodec also has set of helper methods for tuples support.
166
163
167
164
```Swift
168
165
importScaleCodec
169
166
170
167
let tuple = (UInt32.max, "Hello")
171
168
172
-
let encoded =trySCALE.default.encode(tuple)
169
+
let encoded =tryencode(tuple)
173
170
174
-
let decoded: (UInt32, String) =trySCALE.default.decode(from: encoded)
171
+
let decoded: (UInt32, String) =trydecode(from: encoded)
175
172
176
173
assert(tuple == decoded)
177
174
178
175
// without helper methods
179
-
// let encoded = try SCALE.default.encode(STuple(tuple)) // or directly STuple2(tuple)
180
-
// let decoded = try SCALE.default.decode(STuple2<UInt32, String>.self, from: encoded).tuple
176
+
// let encoded = try encode(Tuple(tuple)) // or directly Tuple2(tuple)
177
+
// let decoded = try decode(Tuple2<UInt32, String>.self, from: encoded).tuple
181
178
```
182
179
183
180
### Enums
@@ -189,30 +186,30 @@ Simple enums without associated values can be encoded automatically if enum supp
189
186
```Swift
190
187
importScaleCodec
191
188
192
-
enumTest: CaseIterable, ScaleCodable{
189
+
enumTest: CaseIterable, ScaleCodec.Codable{
193
190
caseA
194
191
caseB
195
192
}
196
193
197
-
let data =trySCALE.default.encode(Test.A)
194
+
let data =tryencode(Test.A)
198
195
199
-
let decoded: Test =trySCALE.default.decode(from: data)
196
+
let decoded: Test =trydecode(from: data)
200
197
201
198
assert(decoded == Test.A)
202
199
```
203
200
204
201
#### Complex enums
205
202
206
-
Encoding and decoding for complex enums with associated values should be implemented manually. Two protocols need to be implemented: `ScaleEncodable` and `ScaleDecodable` (`ScaleCodable` can be used as common alias).
203
+
Encoding and decoding for complex enums with associated values should be implemented manually. Two protocols need to be implemented: `Encodable` and `Decodable` (`Codable` can be used as common alias).
207
204
208
205
```Swift
209
206
importScaleCodec
210
207
211
-
enumTest: ScaleCodable{
208
+
enumTest: ScaleCodec.Codable{
212
209
caseA(String?)
213
210
caseB(UInt32, String) // UInt32 will use Compact encoding.
case .A(let str):try encoder.encode(0, .enumCaseId).encode(str)
227
-
case .B(let int, let str):try encoder.encode(1, .enumCaseId).encode(int, .compact).encode(str)
223
+
case .A(let str):
224
+
try encoder.encode(0, .enumCaseId)
225
+
try encoder.encode(str)
226
+
case .B(let int, let str):
227
+
try encoder.encode(1, .enumCaseId)
228
+
try encoder.encode(int, .compact)
229
+
try encoder.encode(str)
228
230
}
229
231
}
230
232
}
231
233
232
234
let val = Test.B(100, "World!")
233
235
234
-
let data =trySCALE.default.encode(val)
236
+
let data =tryencode(val)
235
237
236
-
let decoded: Test =trySCALE.default.decode(from: data)
238
+
let decoded: Test =trydecode(from: data)
237
239
238
240
assert(decoded == val)
239
241
```
240
242
241
243
### Classes and Structures
242
244
243
-
`ScaleEncodable` and `ScaleDecodable` should be implemented for classes and structures. `ScaleEncoder` and `ScaleDecoder` have helpers methods for standard containers and types.
245
+
`Encodable` and `Decodable` should be implemented for classes and structures. `Encoder` and `Decoder` have helpers methods for standard containers and types.
244
246
245
247
```Swift
246
248
importScaleCodec
247
249
248
-
structTest: ScaleCodable, Equatable{
250
+
structTest: ScaleCodec.Codable, Equatable{
249
251
let var1: String?
250
252
let var2: BigUInt // will use Compact encoding.
251
253
let var3: [UInt32] // UInt32 will use Compact encoding.
let decoded: Test =trySCALE.default.decode(from: data)
276
+
let decoded: Test =trydecode(from: data)
276
277
277
278
assert(decoded == val)
278
279
```
279
280
280
281
#### Fixed classes and structures
281
282
282
-
Classes and structures can be created from fixed encoded `Array` and `Data` object. For convenience ScaleCodec has two sets of protocols: (`ScaleFixedEncodable`, `ScaleFixedDecodable`) and (`ScaleFixedDataEncodable`, `ScaleFixedDataDecodable`).
283
+
Classes and structures can be created from fixed encoded `Array` and `Data` object. For convenience ScaleCodec has two sets of protocols: (`FixedEncodable`, `FixedDecodable`) and (`FixedDataEncodable`, `FixedDataDecodable`).
283
284
284
285
Example:
285
286
286
287
```Swift
287
288
importScaleCodec
288
289
289
-
structStringArray4: Equatable, ScaleFixed{
290
+
structStringArray4: Equatable, FixedCodable{
290
291
typealiasElement=String// Fixed Array element type
291
292
292
293
staticvar fixedElementCount: Int=4// amount of elements in Fixed Array
funcencode() throws-> Data { // encoding to Fixed Data
323
+
funcserialize() throws-> Data { // encoding to Fixed Data
323
324
returnself.data
324
325
}
325
326
}
326
327
327
328
let string4 =StringArray4(["1", "2", "3", "4"])
328
329
329
-
let dataS4 =trySCALE.default.encode(string4)
330
+
let dataS4 =tryencode(string4)
330
331
331
-
let decoded: StringArray4 =trySCALE.default.decode(from: dataS4)
332
+
let decoded: StringArray4 =trydecode(from: dataS4)
332
333
333
334
assert(decoded == string4)
334
335
335
336
let data4 =Data4(Data([1, 2, 3, 4]))
336
337
337
-
let dataE4 =trySCALE.default.encode(data4)
338
+
let dataE4 =tryencode(data4)
338
339
339
-
let decoded: Data4 =trySCALE.default.decode(from: dataE4)
340
+
let decoded: Data4 =trydecode(from: dataE4)
340
341
341
342
assert(decoded == data4)
342
343
343
344
```
344
345
346
+
### Size calculation instead of full parsing
347
+
For some cases it is better to calculate size of encoded type, instead of full parsing. In most cases it will be much quicker. For this purposes ScaleCodec has `SizeCalculable` protocol. It implemented for all base types and containers.
348
+
349
+
```Swift
350
+
importScaleCodec
351
+
352
+
let data =Data([0x10, 0x04, 0x41, 0x04, 0x42, 0x04, 0x43, 0x0c, 0x44, 0x44, 0x44])
353
+
var decoder =decoder(from: data)
354
+
var skipDecoder = decoder.skippable() // special decoder which can skip data
355
+
356
+
let size =tryArray<String>.calculateSize(in: &skipDecoder)
357
+
assert(size ==11)
358
+
359
+
let decoded =try decoder.decode(Array<String>.self)
360
+
assert(decoded == ["A", "B", "C", "DDD"])
361
+
```
362
+
345
363
## License
346
364
347
365
ScaleCodec can be used, distributed and modified under [the Apache 2.0 license](LICENSE).
0 commit comments