77
88import Foundation
99
10- extension Array : ScaleEncodable where Element: ScaleEncodable {
11- public func encode( in encoder: ScaleEncoder ) throws {
10+ extension Array : ScaleContainerEncodable {
11+ public typealias EElement = Element
12+
13+ public func encode( in encoder: ScaleEncoder , writer: @escaping ( EElement , ScaleEncoder ) throws -> Void ) throws {
1214 try encoder. encode ( compact: UInt32 ( count) )
1315 for element in self {
14- try encoder . encode ( element)
16+ try writer ( element, encoder )
1517 }
1618 }
1719}
1820
19- extension Array : ScaleDecodable where Element: ScaleDecodable {
20- public init ( from decoder: ScaleDecoder ) throws {
21+ extension Array : ScaleContainerDecodable {
22+ public typealias DElement = Element
23+
24+ public init ( from decoder: ScaleDecoder , reader: @escaping ( ScaleDecoder ) throws -> Element ) throws {
2125 let size = try Int ( decoder. decode ( UInt32 . self, . compact) )
2226 var array = Array < Element > ( )
2327 array. reserveCapacity ( size)
2428 for _ in 0 ..< size {
25- try array. append ( decoder . decode ( ) )
29+ try array. append ( reader ( decoder ) )
2630 }
2731 self = array
2832 }
2933}
3034
35+ extension Array : ScaleEncodable where Element: ScaleEncodable { }
36+
37+ extension Array : ScaleDecodable where Element: ScaleDecodable { }
38+
3139extension ScaleDecoder {
3240 public func decode< T: ScaleDecodable > ( _ type: [ T ] . Type, _ fixed: ScaleFixedTypeMarker ) throws -> [ T ] {
3341 return try self . decode ( fixed)
3442 }
3543
36- public func decode< T: ScaleDecodable > ( _ fixed: ScaleFixedTypeMarker ) throws -> [ T ] {
44+ public func decode< T> (
45+ _ type: [ T ] . Type, _ fixed: ScaleFixedTypeMarker ,
46+ reader: @escaping ( ScaleDecoder ) throws -> T
47+ ) throws -> [ T ] {
48+ return try self . decode ( fixed, reader: reader)
49+ }
50+
51+ public func decode< T> (
52+ _ fixed: ScaleFixedTypeMarker ,
53+ reader: @escaping ( ScaleDecoder ) throws -> T
54+ ) throws -> [ T ] {
3755 guard case . fixed( let size) = fixed else { fatalError ( ) } // Compiler error silencing.
3856 var values = Array < T > ( )
3957 values. reserveCapacity ( Int ( size) )
4058 for _ in 0 ..< size {
41- try values. append ( self . decode ( ) )
59+ try values. append ( reader ( self ) )
4260 }
4361 return values
4462 }
63+
64+ public func decode< T: ScaleDecodable > ( _ fixed: ScaleFixedTypeMarker ) throws -> [ T ] {
65+ try self . decode ( fixed) { decoder in try decoder. decode ( ) }
66+ }
4567}
4668
4769extension ScaleEncoder {
4870 @discardableResult
4971 public func encode< T: ScaleEncodable > ( _ values: [ T ] , fixed: UInt ) throws -> ScaleEncoder {
72+ try self . encode ( values, fixed: fixed) { val, enc in try enc. encode ( val) }
73+ return self
74+ }
75+
76+ @discardableResult
77+ public func encode< T> (
78+ _ values: [ T ] , fixed: UInt ,
79+ writer: @escaping ( T , ScaleEncoder ) throws -> Void
80+ ) throws -> ScaleEncoder {
5081 guard values. count == fixed else {
5182 throw SEncodingError . invalidValue (
5283 values, SEncodingError . Context (
@@ -56,22 +87,45 @@ extension ScaleEncoder {
5687 )
5788 }
5889 for val in values {
59- try self . encode ( val)
90+ try writer ( val, self )
6091 }
6192 return self
6293 }
6394}
6495
6596extension SCALE {
66- public func decode< T: ScaleDecodable > ( _ type: [ T ] . Type, _ fixed: ScaleFixedTypeMarker , from data: Data ) throws -> [ T ] {
97+ public func decode< T: ScaleDecodable > (
98+ _ type: [ T ] . Type, _ fixed: ScaleFixedTypeMarker , from data: Data
99+ ) throws -> [ T ] {
67100 return try self . decode ( fixed, from: data)
68101 }
69102
103+ public func decode< T> (
104+ _ type: [ T ] . Type, _ fixed: ScaleFixedTypeMarker , from data: Data ,
105+ reader: @escaping ( ScaleDecoder ) throws -> T
106+ ) throws -> [ T ] {
107+ return try self . decode ( fixed, from: data, reader: reader)
108+ }
109+
70110 public func decode< T: ScaleDecodable > ( _ fixed: ScaleFixedTypeMarker , from data: Data ) throws -> [ T ] {
71111 return try self . decoder ( data: data) . decode ( fixed)
72112 }
73113
114+ public func decode< T> (
115+ _ fixed: ScaleFixedTypeMarker , from data: Data ,
116+ reader: @escaping ( ScaleDecoder ) throws -> T
117+ ) throws -> [ T ] {
118+ return try self . decoder ( data: data) . decode ( fixed, reader: reader)
119+ }
120+
74121 public func encode< T: ScaleEncodable > ( _ values: [ T ] , fixed: UInt ) throws -> Data {
75122 return try self . encoder ( ) . encode ( values, fixed: fixed) . output
76123 }
124+
125+ public func encode< T> (
126+ _ values: [ T ] , fixed: UInt ,
127+ writer: @escaping ( T , ScaleEncoder ) throws -> Void
128+ ) throws -> Data {
129+ return try self . encoder ( ) . encode ( values, fixed: fixed, writer: writer) . output
130+ }
77131}
0 commit comments