THRIFT-4547: Swift crosstests (#2670)
Co-authored-by: Jiayu Liu <Jimexist@users.noreply.github.com>
diff --git a/lib/swift/Sources/TBinaryProtocol.swift b/lib/swift/Sources/TBinaryProtocol.swift
index 85acce0..766027e 100644
--- a/lib/swift/Sources/TBinaryProtocol.swift
+++ b/lib/swift/Sources/TBinaryProtocol.swift
@@ -191,6 +191,16 @@
return buff[0]
}
+ public func read() throws -> Int8 {
+ var buff = Data()
+ try ProtocolTransportTry(error: TProtocolError(message: "Transport Read Failed")) {
+ buff = try self.transport.readAll(size: 1)
+ }
+ return buff.withUnsafeBytes { pntr in
+ return pntr.load(as: Int8.self)
+ }
+ }
+
public func read() throws -> Int16 {
var buff = Data()
try ProtocolTransportTry(error: TProtocolError(message: "Transport Read Failed")) {
@@ -246,6 +256,20 @@
return data
}
+ public func read() throws -> UUID {
+ let data = try self.transport.readAll(size: 16)
+ let lsb = data[0..<8]
+ let msb = data[8..<16]
+
+ var id = UUID().uuid
+ withUnsafeMutableBytes(of: &id) { pntr in
+ var copyData = msb
+ copyData.append(lsb)
+ copyData.copyBytes(to: pntr)
+ }
+ return UUID(uuid: id)
+ }
+
// Write methods
public func writeMessageBegin(name: String, type messageType: TMessageType, sequenceID: Int32) throws {
@@ -332,6 +356,14 @@
}
}
+ public func write(_ value: Int8) throws {
+ var value = value
+ let buff = Data(bytes: &value, count: MemoryLayout<Int8>.size(ofValue: value))
+ try ProtocolTransportTry(error: TProtocolError(message: "Transport write failed")) {
+ try self.transport.write(data: buff)
+ }
+ }
+
public func write(_ value: Int16) throws {
var buff = Data()
buff.append(Data([UInt8(0xff & (value >> 8))]))
@@ -381,4 +413,18 @@
try self.transport.write(data: data)
}
}
+
+ public func write(_ value: UUID) throws {
+ let data = withUnsafePointer(to: value.uuid) {
+ Data(bytes: $0, count: MemoryLayout.size(ofValue: value.uuid))
+ }
+ let msb = data[0..<8]
+ let lsb = data[8..<16]
+
+ var buff = Data()
+ buff.append(lsb)
+ buff.append(msb)
+
+ try self.transport.write(data: buff)
+ }
}