@@ -28,108 +28,111 @@ extension Bool: ScaleCodable {
28
28
}
29
29
}
30
30
31
- extension UInt8 : ScaleCodable {
32
- public init ( from decoder : ScaleDecoder ) throws {
33
- self = try decoder . readOrError ( count : 1 , type : UInt8 . self ) . first!
31
+ extension UInt8 : ScaleFixedData {
32
+ public init ( decoding data : Data ) throws {
33
+ self = data . first!
34
34
}
35
35
36
- public func encode( in encoder : ScaleEncoder ) throws {
37
- encoder . write ( Data ( [ self ] ) )
36
+ public func encode( ) throws -> Data {
37
+ return Data ( repeating : self , count : 1 )
38
38
}
39
+
40
+ public static var fixedBytesCount : Int = 1
39
41
}
40
42
41
- extension Int8 : ScaleCodable {
42
- public init ( from decoder: ScaleDecoder ) throws {
43
- let uint = try decoder. readOrError ( count: 1 , type: Int8 . self) . first!
44
- self = Int8 ( bitPattern: uint)
43
+ extension Int8 : ScaleFixedData {
44
+ public init ( decoding data: Data ) throws {
45
+ self = Int8 ( bitPattern: data. first!)
45
46
}
46
47
47
- public func encode( in encoder : ScaleEncoder ) throws {
48
+ public func encode( ) throws -> Data {
48
49
let uint = UInt8 ( bitPattern: self )
49
- encoder . write ( Data ( [ uint] ) )
50
+ return Data ( repeating : uint, count : 1 )
50
51
}
52
+
53
+ public static var fixedBytesCount : Int = 1
51
54
}
52
55
53
- extension UInt16 : ScaleCodable {
54
- public init ( from decoder : ScaleDecoder ) throws {
55
- self = try decoder . readOrError ( count : 2 , type : UInt16 . self ) . withUnsafeBytes {
56
+ extension UInt16 : ScaleFixedData {
57
+ public init ( decoding data : Data ) throws {
58
+ self = data . withUnsafeBytes {
56
59
$0. load ( as: UInt16 . self) . littleEndian
57
60
}
58
61
}
59
62
60
- public func encode( in encoder: ScaleEncoder ) throws {
61
- withUnsafeBytes ( of: self . littleEndian) {
62
- encoder. write ( Data ( $0) )
63
- }
63
+ public func encode( ) throws -> Data {
64
+ return withUnsafeBytes ( of: self . littleEndian) { Data ( $0) }
64
65
}
66
+
67
+ public static var fixedBytesCount : Int = 2
65
68
}
66
69
67
- extension Int16 : ScaleCodable {
68
- public init ( from decoder : ScaleDecoder ) throws {
69
- self = try decoder . readOrError ( count : 2 , type : Int16 . self ) . withUnsafeBytes {
70
+ extension Int16 : ScaleFixedData {
71
+ public init ( decoding data : Data ) throws {
72
+ self = data . withUnsafeBytes {
70
73
$0. load ( as: Int16 . self) . littleEndian
71
74
}
72
75
}
73
76
74
- public func encode( in encoder: ScaleEncoder ) throws {
75
- withUnsafeBytes ( of: self . littleEndian) {
76
- encoder. write ( Data ( $0) )
77
- }
77
+ public func encode( ) throws -> Data {
78
+ return withUnsafeBytes ( of: self . littleEndian) { Data ( $0) }
78
79
}
80
+
81
+ public static var fixedBytesCount : Int = 2
79
82
}
80
83
81
- extension UInt32 : ScaleCodable {
82
- public init ( from decoder : ScaleDecoder ) throws {
83
- self = try decoder . readOrError ( count : 4 , type : UInt32 . self ) . withUnsafeBytes {
84
+ extension UInt32 : ScaleFixedData {
85
+ public init ( decoding data : Data ) throws {
86
+ self = data . withUnsafeBytes {
84
87
$0. load ( as: UInt32 . self) . littleEndian
85
88
}
86
89
}
87
90
88
- public func encode( in encoder: ScaleEncoder ) throws {
89
- withUnsafeBytes ( of: self . littleEndian) {
90
- encoder. write ( Data ( $0) )
91
- }
91
+ public func encode( ) throws -> Data {
92
+ return withUnsafeBytes ( of: self . littleEndian) { Data ( $0) }
92
93
}
94
+
95
+ public static var fixedBytesCount : Int = 4
93
96
}
94
97
95
- extension Int32 : ScaleCodable {
96
- public init ( from decoder : ScaleDecoder ) throws {
97
- self = try decoder . readOrError ( count : 4 , type : Int32 . self ) . withUnsafeBytes {
98
+ extension Int32 : ScaleFixedData {
99
+ public init ( decoding data : Data ) throws {
100
+ self = data . withUnsafeBytes {
98
101
$0. load ( as: Int32 . self) . littleEndian
99
102
}
100
103
}
101
104
102
- public func encode( in encoder: ScaleEncoder ) throws {
103
- withUnsafeBytes ( of: self . littleEndian) {
104
- encoder. write ( Data ( $0) )
105
- }
105
+ public func encode( ) throws -> Data {
106
+ return withUnsafeBytes ( of: self . littleEndian) { Data ( $0) }
106
107
}
108
+
109
+ public static var fixedBytesCount : Int = 4
107
110
}
108
111
109
- extension UInt64 : ScaleCodable {
110
- public init ( from decoder : ScaleDecoder ) throws {
111
- self = try decoder . readOrError ( count : 8 , type : UInt64 . self ) . withUnsafeBytes {
112
+ extension UInt64 : ScaleFixedData {
113
+ public init ( decoding data : Data ) throws {
114
+ self = data . withUnsafeBytes {
112
115
$0. load ( as: UInt64 . self) . littleEndian
113
116
}
114
117
}
115
-
116
- public func encode( in encoder: ScaleEncoder ) throws {
117
- withUnsafeBytes ( of: self . littleEndian) {
118
- encoder. write ( Data ( $0) )
119
- }
118
+
119
+ public func encode( ) throws -> Data {
120
+ return withUnsafeBytes ( of: self . littleEndian) { Data ( $0) }
120
121
}
122
+
123
+ public static var fixedBytesCount : Int = 8
121
124
}
122
125
123
- extension Int64 : ScaleCodable {
124
- public init ( from decoder : ScaleDecoder ) throws {
125
- self = try decoder . readOrError ( count : 8 , type : Int64 . self ) . withUnsafeBytes {
126
+ extension Int64 : ScaleFixedData {
127
+ public init ( decoding data : Data ) throws {
128
+ self = data . withUnsafeBytes {
126
129
$0. load ( as: Int64 . self) . littleEndian
127
130
}
128
131
}
129
-
130
- public func encode( in encoder: ScaleEncoder ) throws {
131
- withUnsafeBytes ( of: self . littleEndian) {
132
- encoder. write ( Data ( $0) )
133
- }
132
+
133
+ public func encode( ) throws -> Data {
134
+ return withUnsafeBytes ( of: self . littleEndian) { Data ( $0) }
134
135
}
136
+
137
+ public static var fixedBytesCount : Int = 8
135
138
}
0 commit comments