mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-28 05:48:06 +00:00
[Swift] Migrate to swift 6.0 and Implements support gRPC v2 (#8983)
* Migrate to swift 6.0 & swift-gRPC 2.0 The following migrates to swift 6.0, and also migrate to swift-grpc 2.0 that uses swift-nio under the hood to provide nicer API and async await Adds sendable to enum & update @_implementationOnly imports to use internal imports * Address PR comments regarding misspelling & proper method naming.
This commit is contained in:
@@ -16,50 +16,52 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
/// FlatBufferGRPCMessage protocol that should allow us to invoke
|
||||
/// initializers directly from the GRPC generated code
|
||||
public protocol FlatBufferGRPCMessage {
|
||||
/// Size of readable bytes in the buffer
|
||||
var size: Int { get }
|
||||
|
||||
init(byteBuffer: ByteBuffer)
|
||||
|
||||
@discardableResult
|
||||
@inline(__always)
|
||||
func withUnsafeReadableBytes<T>(
|
||||
_ body: (UnsafeRawBufferPointer) throws
|
||||
-> T) rethrows -> T
|
||||
enum FlatbuffersGRPCError: Error {
|
||||
case finishedNotCalledOnBuilder
|
||||
}
|
||||
|
||||
/// Message is a wrapper around Buffers to to able to send Flatbuffers `Buffers` through the
|
||||
/// GRPC library
|
||||
public struct Message<T: FlatBufferTable>: FlatBufferGRPCMessage {
|
||||
internal var buffer: ByteBuffer
|
||||
public protocol GRPCVerifiableMessage<Message> {
|
||||
associatedtype Message
|
||||
|
||||
/// Returns the an object of type T that would be read from the buffer
|
||||
public var object: T {
|
||||
T.init(
|
||||
buffer,
|
||||
o: Int32(buffer.read(def: UOffset.self, position: buffer.reader)) &+
|
||||
Int32(buffer.reader))
|
||||
}
|
||||
init(pointer: UnsafeRawBufferPointer)
|
||||
init(byteBuffer: ByteBuffer)
|
||||
|
||||
func decode() throws -> Message
|
||||
func withUnsafeReadableBytes<Data>(
|
||||
_ body: (UnsafeRawBufferPointer) throws
|
||||
-> Data) rethrows -> Data
|
||||
}
|
||||
|
||||
public struct GRPCMessage<
|
||||
Table: FlatBufferVerifiableTable
|
||||
>: Sendable, GRPCVerifiableMessage {
|
||||
public typealias Message = Table
|
||||
|
||||
private let buffer: ByteBuffer
|
||||
|
||||
public var size: Int { Int(buffer.size) }
|
||||
|
||||
/// Initializes the message with the type Flatbuffer.Bytebuffer that is transmitted over
|
||||
/// GRPC
|
||||
/// - Parameter byteBuffer: Flatbuffer ByteBuffer object
|
||||
public init(pointer: UnsafeRawBufferPointer) {
|
||||
buffer = ByteBuffer(
|
||||
copyingMemoryBound: pointer.baseAddress!,
|
||||
capacity: pointer.count)
|
||||
}
|
||||
|
||||
public init(builder: inout FlatBufferBuilder) throws {
|
||||
guard builder.finished else {
|
||||
throw FlatbuffersGRPCError.finishedNotCalledOnBuilder
|
||||
}
|
||||
|
||||
buffer = builder.sizedBuffer
|
||||
}
|
||||
|
||||
public init(byteBuffer: ByteBuffer) {
|
||||
buffer = byteBuffer
|
||||
}
|
||||
|
||||
/// Initializes the message by copying the buffer to the message to be sent.
|
||||
/// from the builder
|
||||
/// - Parameter builder: FlatbufferBuilder that has the bytes created in
|
||||
/// - Note: Use `builder.finish(offset)` before passing the builder without prefixing anything to it
|
||||
public init(builder: inout FlatBufferBuilder) {
|
||||
buffer = builder.sizedBuffer
|
||||
builder.clear()
|
||||
public func decode() throws -> Table {
|
||||
var buf = buffer
|
||||
return try getCheckedRoot(byteBuffer: &buf)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
@@ -73,3 +75,30 @@ public struct Message<T: FlatBufferTable>: FlatBufferGRPCMessage {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
|
||||
public struct FlatBuffersMessageSerializer<
|
||||
Message: GRPCVerifiableMessage
|
||||
>: Sendable {
|
||||
public init() {}
|
||||
|
||||
public func serialize<Data>(
|
||||
message: Message,
|
||||
_ completion: (UnsafeRawBufferPointer) throws -> Data) throws -> Data
|
||||
{
|
||||
return try message.withUnsafeReadableBytes {
|
||||
try completion($0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
|
||||
public struct FlatBuffersMessageDeserializer<
|
||||
Message: GRPCVerifiableMessage
|
||||
>: Sendable {
|
||||
public init() {}
|
||||
|
||||
public func deserialize(pointer: UnsafeRawBufferPointer) throws -> Message {
|
||||
Message.init(pointer: pointer)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user