Skip to content

Commit fd2c9a4

Browse files
committed
added fork to encoder
1 parent 5f3e31b commit fd2c9a4

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

Sources/ScaleCodec/Encoder.swift

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public protocol ScaleEncoder {
1818
@discardableResult
1919
func encode(_ value: ScaleEncodable) throws -> ScaleEncoder
2020
func write(_ data: Data)
21+
22+
// Make a copy of encoder preserving `path`.
23+
// If full = True data will be copied too
24+
func fork(full: Bool) -> ScaleEncoder
2125
}
2226

2327
internal class SEncoder: ScaleEncoder {
@@ -28,9 +32,13 @@ internal class SEncoder: ScaleEncoder {
2832
return context.currentPath
2933
}
3034

31-
init() {
32-
output = Data()
33-
context = SContext()
35+
convenience init() {
36+
self.init(output: Data(), context: SContext())
37+
}
38+
39+
init(output: Data, context: SContext) {
40+
self.output = output
41+
self.context = context
3442
}
3543

3644
@discardableResult
@@ -44,4 +52,9 @@ internal class SEncoder: ScaleEncoder {
4452
func write(_ data: Data) {
4553
output.append(data)
4654
}
55+
56+
func fork(full: Bool) -> ScaleEncoder {
57+
let out = full ? Data(output) : Data()
58+
return SEncoder(output: out, context: SContext(Array(context.currentPath)))
59+
}
4760
}

0 commit comments

Comments
 (0)