Skip to content

Commit e66f37d

Browse files
committed
feat: VecU8
1 parent 4100ad8 commit e66f37d

File tree

5 files changed

+50
-10
lines changed

5 files changed

+50
-10
lines changed
Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
import Foundation
22

3-
struct VecU8: BorshCodable, Codable, Equatable, Hashable {
4-
let length: UInt16
5-
let data: Data
6-
init(from reader: inout BinaryReader) throws {
7-
length = try UInt16(from: &reader)
3+
public struct VecU8<T: FixedWidthInteger & Codable>: BorshCodable, Codable, Equatable, Hashable {
4+
public let length: T
5+
public let data: Data
6+
7+
public init(from reader: inout BinaryReader) throws {
8+
length = try T(from: &reader)
89
data = try Data(reader.read(count: Int(length)))
910
}
1011

11-
func serialize(to data: inout Data) throws {
12-
try length.serialize(to: &data)
13-
try data.serialize(to: &data)
12+
public init(length: T, data: Data) {
13+
self.length = length
14+
self.data = data
15+
}
16+
17+
public func serialize(to writer: inout Data) throws {
18+
try length.serialize(to: &writer)
19+
try data.serialize(to: &writer)
1420
}
1521
}

Sources/SolanaSwift/Programs/TokenPrograms/Token2022Program/ExtensionReader/Token2022ExtensionState.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ public protocol Token2022ExtensionState: BorshCodable, Codable, Equatable, Hasha
44
var length: UInt16 { get }
55
}
66

7-
extension VecU8: Token2022ExtensionState {}
7+
extension VecU8<UInt16>: Token2022ExtensionState {}

Sources/SolanaSwift/Programs/TokenPrograms/Token2022Program/Token2022MintState.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public struct Token2022MintState: TokenMintState {
1212
public var extensions: [AnyToken2022ExtensionState]
1313

1414
public func getParsedExtension<T: Token2022ExtensionState>(ofType _: T.Type) -> T? {
15-
assert(T.self != VecU8.self)
15+
assert(T.self != VecU8<UInt16>.self)
1616
return extensions.first(where: { $0.state is T })?.state as? T
1717
}
1818
}

Tests/SolanaSwiftUnitTests/Other/BufferLayout/BufferLayoutDecodingTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@ class BufferLayoutDecodingTests: XCTestCase {
2424
XCTAssertEqual(try Data(from: &binaryReader).bytes, expectedBytes)
2525
}
2626

27+
// MARK: - VecU8
28+
29+
func testDecodingVecU8() throws {
30+
let string = "GQCn7dKsGcVAJhtFRDDBcRgD8i3I/WDk4Z2y"
31+
let data = Data(base64Encoded: string)!
32+
var binaryReader = BinaryReader(bytes: data.bytes)
33+
let vecU8 = try VecU8<UInt16>(from: &binaryReader)
34+
35+
XCTAssertEqual(vecU8.length, 25)
36+
XCTAssertEqual(vecU8.data.bytes, [
37+
167, 237, 210, 172, 25, 197,
38+
64, 38, 27, 69, 68, 48, 193,
39+
113, 24, 3, 242, 45, 200, 253,
40+
96, 228, 225, 157, 178,
41+
])
42+
}
43+
2744
// MARK: - Mint
2845

2946
func testDecodingMint() throws {

Tests/SolanaSwiftUnitTests/Other/BufferLayout/BufferLayoutEncodingTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@ final class BufferLayoutEncodingTests: XCTestCase {
2424
)
2525
}
2626

27+
// MARK: - VecU8
28+
29+
func testEncodingVecU8() throws {
30+
let length: UInt16 = 25
31+
let data = Data([
32+
167, 237, 210, 172, 25, 197,
33+
64, 38, 27, 69, 68, 48, 193,
34+
113, 24, 3, 242, 45, 200, 253,
35+
96, 228, 225, 157, 178,
36+
])
37+
38+
var result = Data()
39+
try VecU8(length: length, data: data).serialize(to: &result)
40+
41+
XCTAssertEqual(result.base64EncodedString(), "GQCn7dKsGcVAJhtFRDDBcRgD8i3I/WDk4Z2y")
42+
}
43+
2744
// MARK: - Account info
2845

2946
func testEncodingAccountInfo() throws {

0 commit comments

Comments
 (0)