Skip to content

Commit 5f3e31b

Browse files
committed
moved Integers to Fixed
1 parent 1d66197 commit 5f3e31b

File tree

2 files changed

+59
-56
lines changed

2 files changed

+59
-56
lines changed

Sources/ScaleCodec/Integers.swift

Lines changed: 58 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -28,108 +28,111 @@ extension Bool: ScaleCodable {
2828
}
2929
}
3030

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!
3434
}
3535

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)
3838
}
39+
40+
public static var fixedBytesCount: Int = 1
3941
}
4042

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!)
4546
}
4647

47-
public func encode(in encoder: ScaleEncoder) throws {
48+
public func encode() throws -> Data {
4849
let uint = UInt8(bitPattern: self)
49-
encoder.write(Data([uint]))
50+
return Data(repeating: uint, count: 1)
5051
}
52+
53+
public static var fixedBytesCount: Int = 1
5154
}
5255

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 {
5659
$0.load(as: UInt16.self).littleEndian
5760
}
5861
}
5962

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) }
6465
}
66+
67+
public static var fixedBytesCount: Int = 2
6568
}
6669

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 {
7073
$0.load(as: Int16.self).littleEndian
7174
}
7275
}
7376

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) }
7879
}
80+
81+
public static var fixedBytesCount: Int = 2
7982
}
8083

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 {
8487
$0.load(as: UInt32.self).littleEndian
8588
}
8689
}
8790

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) }
9293
}
94+
95+
public static var fixedBytesCount: Int = 4
9396
}
9497

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 {
98101
$0.load(as: Int32.self).littleEndian
99102
}
100103
}
101104

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) }
106107
}
108+
109+
public static var fixedBytesCount: Int = 4
107110
}
108111

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 {
112115
$0.load(as: UInt64.self).littleEndian
113116
}
114117
}
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) }
120121
}
122+
123+
public static var fixedBytesCount: Int = 8
121124
}
122125

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 {
126129
$0.load(as: Int64.self).littleEndian
127130
}
128131
}
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) }
134135
}
136+
137+
public static var fixedBytesCount: Int = 8
135138
}

generate_tuples.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/swift
1+
#!/usr/bin/env swift
22

33
import Foundation
44

0 commit comments

Comments
 (0)