mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-30 19:00:02 +00:00
[Swift] Implements FlatbuffersVector which confirms to RandomAccessCollection (#8752)
* Implements FlatbuffersVector in swift Implements FlatbuffersVector which confirms to RandomAccessCollection, this would give us semi-native sugary syntax to all the arrays in swift port. This work will also be the foundation of using arrays in swift * Fix failing tests for Swift
This commit is contained in:
@@ -28,9 +28,9 @@ extension Int {
|
||||
var n = UInt32(self)
|
||||
|
||||
#if arch(arm) || arch(i386)
|
||||
let max = UInt32(Int.max)
|
||||
let max = UInt32(Int.max)
|
||||
#else
|
||||
let max = UInt32.max
|
||||
let max = UInt32.max
|
||||
#endif
|
||||
|
||||
n -= 1
|
||||
|
||||
@@ -23,7 +23,7 @@ import Foundation
|
||||
@inline(__always)
|
||||
public func padding(
|
||||
bufSize: UInt,
|
||||
elementSize: UInt
|
||||
) -> UInt {
|
||||
elementSize: UInt) -> UInt
|
||||
{
|
||||
((~bufSize) &+ 1) & (elementSize &- 1)
|
||||
}
|
||||
|
||||
@@ -29,8 +29,8 @@ public struct ByteBuffer {
|
||||
@usableFromInline
|
||||
enum Blob {
|
||||
#if !os(WASI)
|
||||
case data(Data)
|
||||
case bytes(ContiguousBytes)
|
||||
case data(Data)
|
||||
case bytes(ContiguousBytes)
|
||||
#endif
|
||||
|
||||
case byteBuffer(_InternalByteBuffer)
|
||||
@@ -96,16 +96,16 @@ public struct ByteBuffer {
|
||||
@inline(__always)
|
||||
func withUnsafeBytes<T>(
|
||||
_ body: (UnsafeRawBufferPointer) throws
|
||||
-> T
|
||||
) rethrows -> T {
|
||||
-> T) rethrows -> T
|
||||
{
|
||||
switch retainedBlob {
|
||||
case .byteBuffer(let byteBuffer):
|
||||
return try byteBuffer.withUnsafeBytes(body)
|
||||
#if !os(WASI)
|
||||
case .data(let data):
|
||||
return try data.withUnsafeBytes(body)
|
||||
case .bytes(let contiguousBytes):
|
||||
return try contiguousBytes.withUnsafeBytes(body)
|
||||
case .data(let data):
|
||||
return try data.withUnsafeBytes(body)
|
||||
case .bytes(let contiguousBytes):
|
||||
return try contiguousBytes.withUnsafeBytes(body)
|
||||
#endif
|
||||
case .array(let array):
|
||||
return try array.withUnsafeBytes(body)
|
||||
@@ -118,21 +118,21 @@ public struct ByteBuffer {
|
||||
@inline(__always)
|
||||
func withUnsafeRawPointer<T>(
|
||||
_ body: (UnsafeMutableRawPointer) throws
|
||||
-> T
|
||||
) rethrows -> T {
|
||||
-> T) rethrows -> T
|
||||
{
|
||||
switch retainedBlob {
|
||||
case .byteBuffer(let byteBuffer):
|
||||
return try byteBuffer.withUnsafeRawPointer(body)
|
||||
#if !os(WASI)
|
||||
case .data(let data):
|
||||
return
|
||||
try data
|
||||
case .data(let data):
|
||||
return
|
||||
try data
|
||||
.withUnsafeBytes {
|
||||
try body(UnsafeMutableRawPointer(mutating: $0.baseAddress!))
|
||||
}
|
||||
case .bytes(let contiguousBytes):
|
||||
return
|
||||
try contiguousBytes
|
||||
case .bytes(let contiguousBytes):
|
||||
return
|
||||
try contiguousBytes
|
||||
.withUnsafeBytes {
|
||||
try body(UnsafeMutableRawPointer(mutating: $0.baseAddress!))
|
||||
}
|
||||
@@ -140,9 +140,9 @@ public struct ByteBuffer {
|
||||
case .array(let array):
|
||||
return
|
||||
try array
|
||||
.withUnsafeBytes {
|
||||
try body(UnsafeMutableRawPointer(mutating: $0.baseAddress!))
|
||||
}
|
||||
.withUnsafeBytes {
|
||||
try body(UnsafeMutableRawPointer(mutating: $0.baseAddress!))
|
||||
}
|
||||
case .pointer(let ptr):
|
||||
return try body(ptr)
|
||||
}
|
||||
@@ -152,20 +152,20 @@ public struct ByteBuffer {
|
||||
@inline(__always)
|
||||
func readWithUnsafeRawPointer<T>(
|
||||
position: Int,
|
||||
_ body: (UnsafeRawPointer) throws -> T
|
||||
) rethrows -> T {
|
||||
_ body: (UnsafeRawPointer) throws -> T) rethrows -> T
|
||||
{
|
||||
switch retainedBlob {
|
||||
case .byteBuffer(let byteBuffer):
|
||||
return try byteBuffer.readWithUnsafeRawPointer(position: position, body)
|
||||
#if !os(WASI)
|
||||
case .data(let data):
|
||||
return try data.withUnsafeBytes {
|
||||
try body($0.baseAddress!.advanced(by: position))
|
||||
}
|
||||
case .bytes(let contiguousBytes):
|
||||
return try contiguousBytes.withUnsafeBytes {
|
||||
try body($0.baseAddress!.advanced(by: position))
|
||||
}
|
||||
case .data(let data):
|
||||
return try data.withUnsafeBytes {
|
||||
try body($0.baseAddress!.advanced(by: position))
|
||||
}
|
||||
case .bytes(let contiguousBytes):
|
||||
return try contiguousBytes.withUnsafeBytes {
|
||||
try body($0.baseAddress!.advanced(by: position))
|
||||
}
|
||||
#endif
|
||||
case .array(let array):
|
||||
return try array.withUnsafeBytes {
|
||||
@@ -197,7 +197,7 @@ public struct ByteBuffer {
|
||||
blob: .byteBuffer(byteBuffer),
|
||||
capacity: byteBuffer.capacity)
|
||||
_readerIndex = Int(byteBuffer.size)
|
||||
self.capacity = byteBuffer.capacity
|
||||
capacity = byteBuffer.capacity
|
||||
}
|
||||
|
||||
/// Constructor that creates a Flatbuffer from unsafe memory region by copying
|
||||
@@ -209,8 +209,8 @@ public struct ByteBuffer {
|
||||
@inline(__always)
|
||||
public init(
|
||||
copyingMemoryBound memory: UnsafeRawPointer,
|
||||
capacity: Int
|
||||
) {
|
||||
capacity: Int)
|
||||
{
|
||||
_storage = Storage(count: capacity)
|
||||
_storage.copy(from: memory, count: capacity)
|
||||
_readerIndex = capacity
|
||||
@@ -228,29 +228,29 @@ public struct ByteBuffer {
|
||||
}
|
||||
|
||||
#if !os(WASI)
|
||||
/// Constructor that creates a Flatbuffer from the Swift Data type object
|
||||
/// - Parameter
|
||||
/// - data: Swift data Object
|
||||
@inline(__always)
|
||||
public init(data: Data) {
|
||||
_storage = Storage(blob: .data(data), capacity: data.count)
|
||||
_readerIndex = data.count
|
||||
capacity = data.count
|
||||
}
|
||||
/// Constructor that creates a Flatbuffer from the Swift Data type object
|
||||
/// - Parameter
|
||||
/// - data: Swift data Object
|
||||
@inline(__always)
|
||||
public init(data: Data) {
|
||||
_storage = Storage(blob: .data(data), capacity: data.count)
|
||||
_readerIndex = data.count
|
||||
capacity = data.count
|
||||
}
|
||||
|
||||
/// Constructor that creates a Flatbuffer object from a ContiguousBytes
|
||||
/// - Parameters:
|
||||
/// - contiguousBytes: Binary stripe to use as the buffer
|
||||
/// - count: amount of readable bytes
|
||||
@inline(__always)
|
||||
public init<Bytes: ContiguousBytes>(
|
||||
contiguousBytes: Bytes,
|
||||
count: Int
|
||||
) {
|
||||
_storage = Storage(blob: .bytes(contiguousBytes), capacity: count)
|
||||
_readerIndex = count
|
||||
self.capacity = count
|
||||
}
|
||||
/// Constructor that creates a Flatbuffer object from a ContiguousBytes
|
||||
/// - Parameters:
|
||||
/// - contiguousBytes: Binary stripe to use as the buffer
|
||||
/// - count: amount of readable bytes
|
||||
@inline(__always)
|
||||
public init<Bytes: ContiguousBytes>(
|
||||
contiguousBytes: Bytes,
|
||||
count: Int)
|
||||
{
|
||||
_storage = Storage(blob: .bytes(contiguousBytes), capacity: count)
|
||||
_readerIndex = count
|
||||
capacity = count
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Constructor that creates a Flatbuffer from unsafe memory region without copying
|
||||
@@ -262,8 +262,8 @@ public struct ByteBuffer {
|
||||
@inline(__always)
|
||||
public init(
|
||||
assumingMemoryBound memory: UnsafeMutableRawPointer,
|
||||
capacity: Int
|
||||
) {
|
||||
capacity: Int)
|
||||
{
|
||||
_storage = Storage(
|
||||
blob: .pointer(memory),
|
||||
capacity: capacity)
|
||||
@@ -280,8 +280,8 @@ public struct ByteBuffer {
|
||||
init(
|
||||
blob: Storage.Blob,
|
||||
count: Int,
|
||||
removing removeBytes: Int
|
||||
) {
|
||||
removing removeBytes: Int)
|
||||
{
|
||||
_storage = Storage(blob: blob, capacity: count)
|
||||
_readerIndex = removeBytes
|
||||
capacity = count
|
||||
@@ -332,8 +332,8 @@ public struct ByteBuffer {
|
||||
@inline(__always)
|
||||
public func readSlice<T>(
|
||||
index: Int,
|
||||
count: Int
|
||||
) -> [T] {
|
||||
count: Int) -> [T]
|
||||
{
|
||||
assert(
|
||||
index + count <= capacity,
|
||||
"Reading out of bounds is illegal")
|
||||
@@ -355,8 +355,8 @@ public struct ByteBuffer {
|
||||
public func withUnsafePointerToSlice<T>(
|
||||
index: Int,
|
||||
count: Int,
|
||||
body: (UnsafeRawBufferPointer) throws -> T
|
||||
) rethrows -> T {
|
||||
body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T
|
||||
{
|
||||
assert(
|
||||
index + count <= capacity,
|
||||
"Reading out of bounds is illegal")
|
||||
@@ -366,46 +366,46 @@ public struct ByteBuffer {
|
||||
}
|
||||
|
||||
#if !os(WASI)
|
||||
/// Reads a string from the buffer and encodes it to a swift string
|
||||
/// - Parameters:
|
||||
/// - index: index of the string in the buffer
|
||||
/// - count: length of the string
|
||||
/// - type: Encoding of the string
|
||||
@inline(__always)
|
||||
public func readString(
|
||||
at index: Int,
|
||||
count: Int,
|
||||
type: String.Encoding = .utf8
|
||||
) -> String? {
|
||||
assert(
|
||||
index + count <= capacity,
|
||||
"Reading out of bounds is illegal")
|
||||
return _storage.readWithUnsafeRawPointer(position: index) {
|
||||
let buf = UnsafeBufferPointer(
|
||||
start: $0.bindMemory(to: UInt8.self, capacity: count),
|
||||
count: count)
|
||||
return String(
|
||||
bytes: buf,
|
||||
encoding: type)
|
||||
}
|
||||
/// Reads a string from the buffer and encodes it to a swift string
|
||||
/// - Parameters:
|
||||
/// - index: index of the string in the buffer
|
||||
/// - count: length of the string
|
||||
/// - type: Encoding of the string
|
||||
@inline(__always)
|
||||
public func readString(
|
||||
at index: Int,
|
||||
count: Int,
|
||||
type: String.Encoding = .utf8) -> String?
|
||||
{
|
||||
assert(
|
||||
index + count <= capacity,
|
||||
"Reading out of bounds is illegal")
|
||||
return _storage.readWithUnsafeRawPointer(position: index) {
|
||||
let buf = UnsafeBufferPointer(
|
||||
start: $0.bindMemory(to: UInt8.self, capacity: count),
|
||||
count: count)
|
||||
return String(
|
||||
bytes: buf,
|
||||
encoding: type)
|
||||
}
|
||||
}
|
||||
#else
|
||||
/// Reads a string from the buffer and encodes it to a swift string
|
||||
/// - Parameters:
|
||||
/// - index: index of the string in the buffer
|
||||
/// - count: length of the string
|
||||
@inline(__always)
|
||||
public func readString(
|
||||
at index: Int,
|
||||
count: Int
|
||||
) -> String? {
|
||||
assert(
|
||||
index + count <= capacity,
|
||||
"Reading out of bounds is illegal")
|
||||
return _storage.readWithUnsafeRawPointer(position: index) {
|
||||
String(cString: $0.bindMemory(to: UInt8.self, capacity: count))
|
||||
}
|
||||
/// Reads a string from the buffer and encodes it to a swift string
|
||||
/// - Parameters:
|
||||
/// - index: index of the string in the buffer
|
||||
/// - count: length of the string
|
||||
@inline(__always)
|
||||
public func readString(
|
||||
at index: Int,
|
||||
count: Int) -> String?
|
||||
{
|
||||
assert(
|
||||
index + count <= capacity,
|
||||
"Reading out of bounds is illegal")
|
||||
return _storage.readWithUnsafeRawPointer(position: index) {
|
||||
String(cString: $0.bindMemory(to: UInt8.self, capacity: count))
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Creates a new Flatbuffer object that's duplicated from the current one
|
||||
@@ -437,8 +437,8 @@ public struct ByteBuffer {
|
||||
@inline(__always)
|
||||
public func withUnsafeBytes<T>(
|
||||
body: (UnsafeRawBufferPointer) throws
|
||||
-> T
|
||||
) rethrows -> T {
|
||||
-> T) rethrows -> T
|
||||
{
|
||||
try _storage.withUnsafeBytes(body)
|
||||
}
|
||||
|
||||
@@ -446,8 +446,8 @@ public struct ByteBuffer {
|
||||
@inline(__always)
|
||||
func withUnsafeMutableRawPointer<T>(
|
||||
body: (UnsafeMutableRawPointer) throws
|
||||
-> T
|
||||
) rethrows -> T {
|
||||
-> T) rethrows -> T
|
||||
{
|
||||
try _storage.withUnsafeRawPointer(body)
|
||||
}
|
||||
|
||||
@@ -455,8 +455,8 @@ public struct ByteBuffer {
|
||||
@inline(__always)
|
||||
func readWithUnsafeRawPointer<T>(
|
||||
position: Int,
|
||||
_ body: (UnsafeRawPointer) throws -> T
|
||||
) rethrows -> T {
|
||||
_ body: (UnsafeRawPointer) throws -> T) rethrows -> T
|
||||
{
|
||||
try _storage.readWithUnsafeRawPointer(position: position, body)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
import Foundation
|
||||
|
||||
#if canImport(Common)
|
||||
import Common
|
||||
import Common
|
||||
#endif
|
||||
|
||||
/// Type aliases
|
||||
@@ -28,7 +28,7 @@ public typealias VOffset = UInt16
|
||||
/// Maximum size for a buffer
|
||||
public let FlatBufferMaxSize =
|
||||
UInt32
|
||||
.max << ((MemoryLayout<SOffset>.size * 8 - 1) - 1)
|
||||
.max << ((MemoryLayout<SOffset>.size * 8 - 1) - 1)
|
||||
|
||||
/// Protocol that All Scalars should conform to
|
||||
///
|
||||
@@ -36,28 +36,28 @@ public let FlatBufferMaxSize =
|
||||
|
||||
extension Scalar where Self: FixedWidthInteger {}
|
||||
|
||||
extension Double: Verifiable {}
|
||||
extension Double: Verifiable, FlatbuffersVectorInitializable {}
|
||||
|
||||
extension Float32: Verifiable {}
|
||||
extension Float32: Verifiable, FlatbuffersVectorInitializable {}
|
||||
|
||||
extension Bool: Verifiable {}
|
||||
extension Bool: Verifiable, FlatbuffersVectorInitializable {}
|
||||
|
||||
extension Int: Verifiable {}
|
||||
extension Int: Verifiable, FlatbuffersVectorInitializable {}
|
||||
|
||||
extension Int8: Verifiable {}
|
||||
extension Int8: Verifiable, FlatbuffersVectorInitializable {}
|
||||
|
||||
extension Int16: Verifiable {}
|
||||
extension Int16: Verifiable, FlatbuffersVectorInitializable {}
|
||||
|
||||
extension Int32: Verifiable {}
|
||||
extension Int32: Verifiable, FlatbuffersVectorInitializable {}
|
||||
|
||||
extension Int64: Verifiable {}
|
||||
extension Int64: Verifiable, FlatbuffersVectorInitializable {}
|
||||
|
||||
extension UInt8: Verifiable {}
|
||||
extension UInt8: Verifiable, FlatbuffersVectorInitializable {}
|
||||
|
||||
extension UInt16: Verifiable {}
|
||||
extension UInt16: Verifiable, FlatbuffersVectorInitializable {}
|
||||
|
||||
extension UInt32: Verifiable {}
|
||||
extension UInt32: Verifiable, FlatbuffersVectorInitializable {}
|
||||
|
||||
extension UInt64: Verifiable {}
|
||||
extension UInt64: Verifiable, FlatbuffersVectorInitializable {}
|
||||
|
||||
public func FlatBuffersVersion_25_9_23() {}
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
import Foundation
|
||||
|
||||
#if canImport(Common)
|
||||
import Common
|
||||
import Common
|
||||
#endif
|
||||
|
||||
/// Enum is a protocol that all flatbuffers enums should conform to
|
||||
@@ -28,8 +28,13 @@ public protocol Enum {
|
||||
associatedtype T: Scalar & Verifiable
|
||||
/// Size of the current associatedtype in the enum
|
||||
static var byteSize: Int { get }
|
||||
/// Provides a static min value in case we fail to decode
|
||||
/// in vectors
|
||||
static var min: Self { get }
|
||||
/// The current value the enum hosts
|
||||
var value: T { get }
|
||||
|
||||
init?(rawValue: T)
|
||||
}
|
||||
|
||||
extension Enum where Self: Verifiable {
|
||||
@@ -44,8 +49,8 @@ extension Enum where Self: Verifiable {
|
||||
public static func verify<T>(
|
||||
_ verifier: inout Verifier,
|
||||
at position: Int,
|
||||
of type: T.Type
|
||||
) throws where T: Verifiable {
|
||||
of type: T.Type) throws where T: Verifiable
|
||||
{
|
||||
try verifier.inBuffer(position: position, of: type.self)
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
import Foundation
|
||||
|
||||
#if canImport(Common)
|
||||
import Common
|
||||
import Common
|
||||
#endif
|
||||
|
||||
/// ``FlatBufferBuilder`` builds a `FlatBuffer` through manipulating its internal state.
|
||||
@@ -63,21 +63,21 @@ public struct FlatBufferBuilder {
|
||||
public var capacity: Int { _bb.capacity }
|
||||
|
||||
#if !os(WASI)
|
||||
/// Data representation of the buffer
|
||||
///
|
||||
/// Should only be used after ``finish(offset:addPrefix:)`` is called
|
||||
public var data: Data {
|
||||
assert(finished, "Data shouldn't be called before finish()")
|
||||
return _bb.withUnsafeSlicedBytes { ptr in
|
||||
var data = Data()
|
||||
data.append(
|
||||
ptr.baseAddress!.bindMemory(
|
||||
to: UInt8.self,
|
||||
capacity: ptr.count),
|
||||
count: ptr.count)
|
||||
return data
|
||||
}
|
||||
/// Data representation of the buffer
|
||||
///
|
||||
/// Should only be used after ``finish(offset:addPrefix:)`` is called
|
||||
public var data: Data {
|
||||
assert(finished, "Data shouldn't be called before finish()")
|
||||
return _bb.withUnsafeSlicedBytes { ptr in
|
||||
var data = Data()
|
||||
data.append(
|
||||
ptr.baseAddress!.bindMemory(
|
||||
to: UInt8.self,
|
||||
capacity: ptr.count),
|
||||
count: ptr.count)
|
||||
return data
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Returns the underlying bytes in the ``ByteBuffer``
|
||||
@@ -132,8 +132,8 @@ public struct FlatBufferBuilder {
|
||||
/// however the builder can be force by passing true for `serializeDefaults`
|
||||
public init(
|
||||
initialSize: Int32 = 1024,
|
||||
serializeDefaults force: Bool = false
|
||||
) {
|
||||
serializeDefaults force: Bool = false)
|
||||
{
|
||||
assert(initialSize > 0, "Size should be greater than zero!")
|
||||
guard isLitteEndian else {
|
||||
fatalError(
|
||||
@@ -200,8 +200,8 @@ public struct FlatBufferBuilder {
|
||||
mutating public func finish(
|
||||
offset: Offset,
|
||||
fileId: String,
|
||||
addPrefix prefix: Bool = false
|
||||
) {
|
||||
addPrefix prefix: Bool = false)
|
||||
{
|
||||
let size = MemoryLayout<UOffset>.size
|
||||
preAlign(
|
||||
len: size &+ (prefix ? size : 0) &+ FileIdLength,
|
||||
@@ -230,8 +230,8 @@ public struct FlatBufferBuilder {
|
||||
/// include the size of the current buffer.
|
||||
mutating public func finish(
|
||||
offset: Offset,
|
||||
addPrefix prefix: Bool = false
|
||||
) {
|
||||
addPrefix prefix: Bool = false)
|
||||
{
|
||||
notNested()
|
||||
let size = MemoryLayout<UOffset>.size
|
||||
preAlign(len: size &+ (prefix ? size : 0), alignment: _minAlignment)
|
||||
@@ -470,8 +470,8 @@ public struct FlatBufferBuilder {
|
||||
@inline(__always)
|
||||
mutating public func createVector<T: Scalar>(
|
||||
_ elements: [T],
|
||||
size: Int
|
||||
) -> Offset {
|
||||
size: Int) -> Offset
|
||||
{
|
||||
let size = size
|
||||
startVector(size, elementSize: MemoryLayout<T>.size)
|
||||
_bb.push(elements: elements)
|
||||
@@ -479,20 +479,20 @@ public struct FlatBufferBuilder {
|
||||
}
|
||||
|
||||
#if swift(>=5.0) && !os(WASI)
|
||||
@inline(__always)
|
||||
/// Creates a vector of bytes in the buffer.
|
||||
///
|
||||
/// Allows creating a vector from `Data` without copying to a `[UInt8]`
|
||||
///
|
||||
/// - Parameter bytes: bytes to be written into the buffer
|
||||
/// - Returns: ``Offset`` of the vector
|
||||
mutating public func createVector(bytes: ContiguousBytes) -> Offset {
|
||||
bytes.withUnsafeBytes {
|
||||
startVector($0.count, elementSize: MemoryLayout<UInt8>.size)
|
||||
_bb.push(bytes: $0)
|
||||
return endVector(len: $0.count)
|
||||
}
|
||||
@inline(__always)
|
||||
/// Creates a vector of bytes in the buffer.
|
||||
///
|
||||
/// Allows creating a vector from `Data` without copying to a `[UInt8]`
|
||||
///
|
||||
/// - Parameter bytes: bytes to be written into the buffer
|
||||
/// - Returns: ``Offset`` of the vector
|
||||
mutating public func createVector(bytes: ContiguousBytes) -> Offset {
|
||||
bytes.withUnsafeBytes {
|
||||
startVector($0.count, elementSize: MemoryLayout<UInt8>.size)
|
||||
_bb.push(bytes: $0)
|
||||
return endVector(len: $0.count)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Creates a vector of type ``Enum`` into the ``ByteBuffer``
|
||||
@@ -530,8 +530,8 @@ public struct FlatBufferBuilder {
|
||||
@inline(__always)
|
||||
mutating public func createVector<T: Enum>(
|
||||
_ elements: [T],
|
||||
size: Int
|
||||
) -> Offset {
|
||||
size: Int) -> Offset
|
||||
{
|
||||
let size = size
|
||||
startVector(size, elementSize: T.byteSize)
|
||||
for index in stride(from: elements.count, to: 0, by: -1) {
|
||||
@@ -576,8 +576,8 @@ public struct FlatBufferBuilder {
|
||||
@inline(__always)
|
||||
mutating public func createVector(
|
||||
ofOffsets offsets: [Offset],
|
||||
len: Int
|
||||
) -> Offset {
|
||||
len: Int) -> Offset
|
||||
{
|
||||
startVector(len, elementSize: MemoryLayout<Offset>.size)
|
||||
for index in stride(from: offsets.count, to: 0, by: -1) {
|
||||
push(element: offsets[index &- 1])
|
||||
@@ -653,8 +653,8 @@ public struct FlatBufferBuilder {
|
||||
@inline(__always)
|
||||
@discardableResult
|
||||
mutating public func create<T: NativeStruct>(
|
||||
struct s: T, position: VOffset
|
||||
) -> Offset {
|
||||
struct s: T, position: VOffset) -> Offset
|
||||
{
|
||||
let offset = create(struct: s)
|
||||
_vtableStorage.add(
|
||||
loc: (offset: _bb.size, position: VOffset(position)))
|
||||
@@ -678,8 +678,8 @@ public struct FlatBufferBuilder {
|
||||
@inline(__always)
|
||||
@discardableResult
|
||||
mutating public func create<T: NativeStruct>(
|
||||
struct s: T
|
||||
) -> Offset {
|
||||
struct s: T) -> Offset
|
||||
{
|
||||
let size = MemoryLayout<T>.size
|
||||
preAlign(len: size, alignment: MemoryLayout<T>.alignment)
|
||||
_bb.push(struct: s, size: size)
|
||||
@@ -794,8 +794,8 @@ public struct FlatBufferBuilder {
|
||||
mutating public func add<T: Scalar>(
|
||||
element: T,
|
||||
def: T,
|
||||
at position: VOffset
|
||||
) {
|
||||
at position: VOffset)
|
||||
{
|
||||
if element == def && !serializeDefaults { return }
|
||||
track(offset: push(element: element), at: position)
|
||||
}
|
||||
|
||||
@@ -28,8 +28,17 @@ public protocol FlatbuffersInitializable {
|
||||
init(_ bb: ByteBuffer, o: Int32)
|
||||
}
|
||||
|
||||
/// FlatbufferObject structures all the Flatbuffers objects
|
||||
public protocol FlatBufferObject: FlatbuffersInitializable {
|
||||
/// FlatbufferTabke structures all the Flatbuffers tables
|
||||
public protocol FlatBufferTable: FlatbuffersInitializable,
|
||||
FlatbuffersVectorInitializable
|
||||
{
|
||||
var __buffer: ByteBuffer! { get }
|
||||
}
|
||||
|
||||
/// FlatbufferStruct structures all the Flatbuffers structs
|
||||
public protocol FlatBufferStruct: FlatbuffersInitializable,
|
||||
FlatbuffersVectorInitializable
|
||||
{
|
||||
var __buffer: ByteBuffer! { get }
|
||||
}
|
||||
|
||||
|
||||
@@ -63,13 +63,13 @@ public enum FlatbuffersErrors: Error, Equatable {
|
||||
|
||||
#if !os(WASI)
|
||||
|
||||
extension FlatbuffersErrors {
|
||||
public static func == (
|
||||
lhs: FlatbuffersErrors,
|
||||
rhs: FlatbuffersErrors
|
||||
) -> Bool {
|
||||
lhs.localizedDescription == rhs.localizedDescription
|
||||
}
|
||||
extension FlatbuffersErrors {
|
||||
public static func == (
|
||||
lhs: FlatbuffersErrors,
|
||||
rhs: FlatbuffersErrors) -> Bool
|
||||
{
|
||||
lhs.localizedDescription == rhs.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -28,20 +28,20 @@ public protocol FlatBufferGRPCMessage {
|
||||
@inline(__always)
|
||||
func withUnsafeReadableBytes<T>(
|
||||
_ body: (UnsafeRawBufferPointer) throws
|
||||
-> T
|
||||
) rethrows -> T
|
||||
-> T) rethrows -> T
|
||||
}
|
||||
|
||||
/// Message is a wrapper around Buffers to to able to send Flatbuffers `Buffers` through the
|
||||
/// GRPC library
|
||||
public struct Message<T: FlatBufferObject>: FlatBufferGRPCMessage {
|
||||
public struct Message<T: FlatBufferTable>: FlatBufferGRPCMessage {
|
||||
internal var buffer: ByteBuffer
|
||||
|
||||
/// 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))
|
||||
o: Int32(buffer.read(def: UOffset.self, position: buffer.reader)) &+
|
||||
Int32(buffer.reader))
|
||||
}
|
||||
|
||||
public var size: Int { Int(buffer.size) }
|
||||
@@ -66,8 +66,8 @@ public struct Message<T: FlatBufferObject>: FlatBufferGRPCMessage {
|
||||
@inline(__always)
|
||||
public func withUnsafeReadableBytes<Data>(
|
||||
_ body: (UnsafeRawBufferPointer) throws
|
||||
-> Data
|
||||
) rethrows -> Data {
|
||||
-> Data) rethrows -> Data
|
||||
{
|
||||
return try buffer.readWithUnsafeRawPointer(position: buffer.reader) {
|
||||
try body(UnsafeRawBufferPointer(start: $0, count: size))
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
import Foundation
|
||||
|
||||
#if canImport(Common)
|
||||
import Common
|
||||
import Common
|
||||
#endif
|
||||
|
||||
/// Mutable is a protocol that allows us to mutate Scalar values within a ``ByteBuffer``
|
||||
|
||||
@@ -27,7 +27,8 @@ extension NativeObject {
|
||||
/// - Parameter type: Type of the Flatbuffer object
|
||||
/// - Returns: returns the encoded sized ByteBuffer
|
||||
public func serialize<T: ObjectAPIPacker>(type: T.Type) -> ByteBuffer
|
||||
where T.T == Self {
|
||||
where T.T == Self
|
||||
{
|
||||
var builder = FlatBufferBuilder(initialSize: 1024)
|
||||
return serialize(builder: &builder, type: type.self)
|
||||
}
|
||||
@@ -42,8 +43,8 @@ extension NativeObject {
|
||||
/// It can be considered less expensive in terms of memory allocation
|
||||
public func serialize<T: ObjectAPIPacker>(
|
||||
builder: inout FlatBufferBuilder,
|
||||
type: T.Type
|
||||
) -> ByteBuffer where T.T == Self {
|
||||
type: T.Type) -> ByteBuffer where T.T == Self
|
||||
{
|
||||
var s = self
|
||||
let root = type.pack(&builder, obj: &s)
|
||||
builder.finish(offset: root)
|
||||
|
||||
@@ -26,11 +26,11 @@ import Foundation
|
||||
///
|
||||
/// ``getPrefixedSizeCheckedRoot(byteBuffer:options:)`` would skip the first Bytes in
|
||||
/// the ``ByteBuffer`` and verifies the buffer by calling ``getCheckedRoot(byteBuffer:options:)``
|
||||
public func getPrefixedSizeCheckedRoot<T: FlatBufferObject & Verifiable>(
|
||||
public func getPrefixedSizeCheckedRoot<T: FlatBufferTable & Verifiable>(
|
||||
byteBuffer: inout ByteBuffer,
|
||||
fileId: String? = nil,
|
||||
options: VerifierOptions = .init()
|
||||
) throws -> T {
|
||||
options: VerifierOptions = .init()) throws -> T
|
||||
{
|
||||
byteBuffer.skipPrefix()
|
||||
return try getCheckedRoot(
|
||||
byteBuffer: &byteBuffer,
|
||||
@@ -48,11 +48,11 @@ public func getPrefixedSizeCheckedRoot<T: FlatBufferObject & Verifiable>(
|
||||
///
|
||||
/// ``getPrefixedSizeCheckedRoot(byteBuffer:options:)`` would skip the first Bytes in
|
||||
/// the ``ByteBuffer`` and verifies the buffer by calling ``getCheckedRoot(byteBuffer:options:)``
|
||||
public func getCheckedPrefixedSizeRoot<T: FlatBufferObject & Verifiable>(
|
||||
public func getCheckedPrefixedSizeRoot<T: FlatBufferTable & Verifiable>(
|
||||
byteBuffer: inout ByteBuffer,
|
||||
fileId: String? = nil,
|
||||
options: VerifierOptions = .init()
|
||||
) throws -> T {
|
||||
options: VerifierOptions = .init()) throws -> T
|
||||
{
|
||||
let prefix = byteBuffer.skipPrefix()
|
||||
if prefix != byteBuffer.size {
|
||||
throw FlatbuffersErrors.prefixedSizeNotEqualToBufferSize
|
||||
@@ -70,9 +70,8 @@ public func getCheckedPrefixedSizeRoot<T: FlatBufferObject & Verifiable>(
|
||||
///
|
||||
/// ``getPrefixedSizeCheckedRoot(byteBuffer:options:)`` would skip the first Bytes in
|
||||
/// the ``ByteBuffer`` and then calls ``getRoot(byteBuffer:)``
|
||||
public func getPrefixedSizeRoot<T: FlatBufferObject>(
|
||||
byteBuffer: inout ByteBuffer
|
||||
)
|
||||
public func getPrefixedSizeRoot<T: FlatBufferTable>(
|
||||
byteBuffer: inout ByteBuffer)
|
||||
-> T
|
||||
{
|
||||
byteBuffer.skipPrefix()
|
||||
@@ -90,11 +89,11 @@ public func getPrefixedSizeRoot<T: FlatBufferObject>(
|
||||
/// ``getCheckedRoot(byteBuffer:options:)`` Takes in a ``ByteBuffer`` and verifies
|
||||
/// that by creating a ``Verifier`` and checkes if all the `Bytes` and correctly aligned
|
||||
/// and within the ``ByteBuffer`` range.
|
||||
public func getCheckedRoot<T: FlatBufferObject & Verifiable>(
|
||||
public func getCheckedRoot<T: FlatBufferTable & Verifiable>(
|
||||
byteBuffer: inout ByteBuffer,
|
||||
fileId: String? = nil,
|
||||
options: VerifierOptions = .init()
|
||||
) throws -> T {
|
||||
options: VerifierOptions = .init()) throws -> T
|
||||
{
|
||||
var verifier = try Verifier(buffer: &byteBuffer, options: options)
|
||||
if let fileId = fileId {
|
||||
try verifier.verify(id: fileId)
|
||||
@@ -109,7 +108,7 @@ public func getCheckedRoot<T: FlatBufferObject & Verifiable>(
|
||||
/// Returns a `NON-Checked` flatbuffers object
|
||||
/// - Parameter byteBuffer: Buffer that contains data
|
||||
/// - Returns: Returns a Flatbuffers object
|
||||
public func getRoot<T: FlatBufferObject>(byteBuffer: inout ByteBuffer) -> T {
|
||||
public func getRoot<T: FlatBufferTable>(byteBuffer: inout ByteBuffer) -> T {
|
||||
T.init(
|
||||
byteBuffer,
|
||||
o: Int32(byteBuffer.read(def: UOffset.self, position: byteBuffer.reader))
|
||||
|
||||
@@ -28,8 +28,8 @@ extension String: Verifiable {
|
||||
public static func verify<T>(
|
||||
_ verifier: inout Verifier,
|
||||
at position: Int,
|
||||
of type: T.Type
|
||||
) throws where T: Verifiable {
|
||||
of type: T.Type) throws where T: Verifiable
|
||||
{
|
||||
|
||||
let range = try String.verifyRange(&verifier, at: position, of: UInt8.self)
|
||||
/// Safe &+ since we already check for overflow in verify range
|
||||
@@ -75,16 +75,16 @@ extension String: ObjectAPIPacker {
|
||||
|
||||
public static func pack(
|
||||
_ builder: inout FlatBufferBuilder,
|
||||
obj: inout String?
|
||||
) -> Offset {
|
||||
obj: inout String?) -> Offset
|
||||
{
|
||||
guard var obj = obj else { return Offset() }
|
||||
return pack(&builder, obj: &obj)
|
||||
}
|
||||
|
||||
public static func pack(
|
||||
_ builder: inout FlatBufferBuilder,
|
||||
obj: inout String
|
||||
) -> Offset {
|
||||
obj: inout String) -> Offset
|
||||
{
|
||||
builder.create(string: obj)
|
||||
}
|
||||
|
||||
@@ -97,14 +97,15 @@ extension String: ObjectAPIPacker {
|
||||
extension String: NativeObject {
|
||||
|
||||
public func serialize<T: ObjectAPIPacker>(type: T.Type) -> ByteBuffer
|
||||
where T.T == Self {
|
||||
where T.T == Self
|
||||
{
|
||||
fatalError("serialize should never be called from string directly")
|
||||
}
|
||||
|
||||
public func serialize<T: ObjectAPIPacker>(
|
||||
builder: inout FlatBufferBuilder,
|
||||
type: T.Type
|
||||
) -> ByteBuffer where T.T == Self {
|
||||
type: T.Type) -> ByteBuffer where T.T == Self
|
||||
{
|
||||
fatalError("serialize should never be called from string directly")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
import Foundation
|
||||
|
||||
#if canImport(Common)
|
||||
import Common
|
||||
import Common
|
||||
#endif
|
||||
|
||||
/// Struct is a representation of a mutable `Flatbuffers` struct
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
import Foundation
|
||||
|
||||
#if canImport(Common)
|
||||
import Common
|
||||
import Common
|
||||
#endif
|
||||
|
||||
/// `Table` is a Flatbuffers object that can read,
|
||||
@@ -71,15 +71,7 @@ public struct Table {
|
||||
/// String reads from the buffer with respect to position of the current table.
|
||||
/// - Parameter offset: Offset of the string
|
||||
public func string(at offset: Int32) -> String? {
|
||||
directString(at: offset &+ position)
|
||||
}
|
||||
|
||||
/// Direct string reads from the buffer disregarding the position of the table.
|
||||
/// It would be preferable to use string unless the current position of the table
|
||||
/// is not needed
|
||||
/// - Parameter offset: Offset of the string
|
||||
public func directString(at offset: Int32) -> String? {
|
||||
var offset = offset
|
||||
var offset = offset &+ position
|
||||
offset &+= bb.read(def: Int32.self, position: Int(offset))
|
||||
let count = bb.read(def: Int32.self, position: Int(offset))
|
||||
let position = Int(offset) &+ MemoryLayout<Int32>.size
|
||||
@@ -91,24 +83,7 @@ public struct Table {
|
||||
/// - type: Type of Element that needs to be read from the buffer
|
||||
/// - o: Offset of the Element
|
||||
public func readBuffer<T>(of type: T.Type, at o: Int32) -> T {
|
||||
directRead(of: T.self, offset: o &+ position)
|
||||
}
|
||||
|
||||
/// Reads from the buffer disregarding the position of the table.
|
||||
/// It would be used when reading from an
|
||||
/// ```
|
||||
/// let offset = __t.offset(10)
|
||||
/// //Only used when the we already know what is the
|
||||
/// // position in the table since __t.vector(at:)
|
||||
/// // returns the index with respect to the position
|
||||
/// __t.directRead(of: Byte.self,
|
||||
/// offset: __t.vector(at: offset) + index * 1)
|
||||
/// ```
|
||||
/// - Parameters:
|
||||
/// - type: Type of Element that needs to be read from the buffer
|
||||
/// - o: Offset of the Element
|
||||
public func directRead<T>(of type: T.Type, offset o: Int32) -> T {
|
||||
bb.read(def: T.self, position: Int(o))
|
||||
bb.read(def: T.self, position: Int(o &+ position))
|
||||
}
|
||||
|
||||
/// Returns that current `Union` object at a specific offset
|
||||
@@ -137,6 +112,35 @@ public struct Table {
|
||||
return bb.readSlice(index: Int(vector(at: o)), count: Int(vector(count: o)))
|
||||
}
|
||||
|
||||
public func vector<T>(at off: Int32, byteSize: Int) -> FlatbufferVector<T> {
|
||||
let off = offset(off)
|
||||
return FlatbufferVector(
|
||||
bb: bb,
|
||||
startPosition: vector(at: off),
|
||||
count: Int(count(offset: off)),
|
||||
byteSize: byteSize)
|
||||
}
|
||||
|
||||
public func unionVector(
|
||||
at off: Int32,
|
||||
byteSize: Int) -> UnionFlatbufferVector
|
||||
{
|
||||
let off = offset(off)
|
||||
return UnionFlatbufferVector(
|
||||
bb: bb,
|
||||
startPosition: vector(at: off),
|
||||
count: Int(count(offset: off)),
|
||||
byteSize: byteSize)
|
||||
}
|
||||
|
||||
private func count(offset: Int32) -> Int32 {
|
||||
if offset == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
return vector(count: offset)
|
||||
}
|
||||
|
||||
/// Returns the underlying pointer to a vector within the buffer
|
||||
/// This should only be used by `Scalars`
|
||||
/// - Parameter off: Readable offset
|
||||
@@ -144,14 +148,15 @@ public struct Table {
|
||||
@inline(__always)
|
||||
public func withUnsafePointerToSlice<T>(
|
||||
at off: Int32,
|
||||
body: (UnsafeRawBufferPointer) throws -> T
|
||||
) rethrows -> T? {
|
||||
body: (UnsafeRawBufferPointer, Int) throws -> T) rethrows -> T?
|
||||
{
|
||||
let o = offset(off)
|
||||
guard o != 0 else { return nil }
|
||||
let count = Int(vector(count: o))
|
||||
return try bb.withUnsafePointerToSlice(
|
||||
index: Int(vector(at: o)),
|
||||
count: Int(vector(count: o)),
|
||||
body: body)
|
||||
count: count,
|
||||
body: { try body($0, count) })
|
||||
}
|
||||
|
||||
/// Vector count gets the count of Elements within the array
|
||||
@@ -192,8 +197,8 @@ public struct Table {
|
||||
static public func offset(
|
||||
_ o: Int32,
|
||||
vOffset: Int32,
|
||||
fbb: inout FlatBufferBuilder
|
||||
) -> Int32 {
|
||||
fbb: inout FlatBufferBuilder) -> Int32
|
||||
{
|
||||
let vTable = Int32(fbb.capacity) &- o
|
||||
return vTable
|
||||
&+ Int32(
|
||||
@@ -216,8 +221,8 @@ public struct Table {
|
||||
static public func compare(
|
||||
_ off1: Int32,
|
||||
_ off2: Int32,
|
||||
fbb: inout FlatBufferBuilder
|
||||
) -> Int32 {
|
||||
fbb: inout FlatBufferBuilder) -> Int32
|
||||
{
|
||||
let memorySize = Int32(MemoryLayout<Int32>.size)
|
||||
let _off1 = off1 &+ fbb.read(def: Int32.self, position: Int(off1))
|
||||
let _off2 = off2 &+ fbb.read(def: Int32.self, position: Int(off2))
|
||||
@@ -246,8 +251,8 @@ public struct Table {
|
||||
static public func compare(
|
||||
_ off1: Int32,
|
||||
_ key: [Byte],
|
||||
fbb: inout FlatBufferBuilder
|
||||
) -> Int32 {
|
||||
fbb: inout FlatBufferBuilder) -> Int32
|
||||
{
|
||||
let memorySize = Int32(MemoryLayout<Int32>.size)
|
||||
let _off1 = off1 &+ fbb.read(def: Int32.self, position: Int(off1))
|
||||
let len1 = fbb.read(def: Int32.self, position: Int(_off1))
|
||||
@@ -274,8 +279,8 @@ public struct Table {
|
||||
static public func offset(
|
||||
_ o: Int32,
|
||||
vOffset: Int32,
|
||||
fbb: ByteBuffer
|
||||
) -> Int32 {
|
||||
fbb: ByteBuffer) -> Int32
|
||||
{
|
||||
let vTable = Int32(fbb.capacity) &- o
|
||||
return vTable
|
||||
&+ Int32(
|
||||
@@ -298,8 +303,8 @@ public struct Table {
|
||||
static public func compare(
|
||||
_ off1: Int32,
|
||||
_ off2: Int32,
|
||||
fbb: ByteBuffer
|
||||
) -> Int32 {
|
||||
fbb: ByteBuffer) -> Int32
|
||||
{
|
||||
let memorySize = Int32(MemoryLayout<Int32>.size)
|
||||
let _off1 = off1 &+ fbb.read(def: Int32.self, position: Int(off1))
|
||||
let _off2 = off2 &+ fbb.read(def: Int32.self, position: Int(off2))
|
||||
@@ -328,8 +333,8 @@ public struct Table {
|
||||
static public func compare(
|
||||
_ off1: Int32,
|
||||
_ key: [Byte],
|
||||
fbb: ByteBuffer
|
||||
) -> Int32 {
|
||||
fbb: ByteBuffer) -> Int32
|
||||
{
|
||||
let memorySize = Int32(MemoryLayout<Int32>.size)
|
||||
let _off1 = off1 &+ fbb.read(def: Int32.self, position: Int(off1))
|
||||
let len1 = fbb.read(def: Int32.self, position: Int(_off1))
|
||||
|
||||
@@ -45,8 +45,8 @@ public struct TableVerifier {
|
||||
position: Int,
|
||||
vtable: Int,
|
||||
vtableLength: Int,
|
||||
verifier: inout Verifier
|
||||
) {
|
||||
verifier: inout Verifier)
|
||||
{
|
||||
_position = position
|
||||
_vtable = vtable
|
||||
_vtableLength = vtableLength
|
||||
@@ -84,8 +84,8 @@ public struct TableVerifier {
|
||||
field: VOffset,
|
||||
fieldName: String,
|
||||
required: Bool,
|
||||
type: T.Type
|
||||
) throws where T: Verifiable {
|
||||
type: T.Type) throws where T: Verifiable
|
||||
{
|
||||
let derefValue = try dereference(field)
|
||||
|
||||
if let value = derefValue {
|
||||
@@ -115,9 +115,9 @@ public struct TableVerifier {
|
||||
unionKeyName: String,
|
||||
fieldName: String,
|
||||
required: Bool,
|
||||
completion: @escaping (inout Verifier, T, Int) throws -> Void
|
||||
) throws
|
||||
where T: UnionEnum {
|
||||
completion: @escaping (inout Verifier, T, Int) throws -> Void) throws
|
||||
where T: UnionEnum
|
||||
{
|
||||
let keyPos = try dereference(key)
|
||||
let valPos = try dereference(field)
|
||||
|
||||
@@ -131,7 +131,7 @@ public struct TableVerifier {
|
||||
}
|
||||
|
||||
if let _key = keyPos,
|
||||
let _val = valPos
|
||||
let _val = valPos
|
||||
{
|
||||
/// verifiying that the key is within the buffer
|
||||
try T.T.verify(&_verifier, at: _key, of: T.T.self)
|
||||
@@ -172,14 +172,14 @@ public struct TableVerifier {
|
||||
unionKeyName: String,
|
||||
fieldName: String,
|
||||
required: Bool,
|
||||
completion: @escaping (inout Verifier, T, Int) throws -> Void
|
||||
) throws
|
||||
where T: UnionEnum {
|
||||
completion: @escaping (inout Verifier, T, Int) throws -> Void) throws
|
||||
where T: UnionEnum
|
||||
{
|
||||
let keyVectorPosition = try dereference(key)
|
||||
let offsetVectorPosition = try dereference(field)
|
||||
|
||||
if let keyPos = keyVectorPosition,
|
||||
let valPos = offsetVectorPosition
|
||||
let valPos = offsetVectorPosition
|
||||
{
|
||||
try UnionVector<T>.verify(
|
||||
&_verifier,
|
||||
|
||||
77
swift/Sources/FlatBuffers/Vectors/FlatbufferVector.swift
Normal file
77
swift/Sources/FlatBuffers/Vectors/FlatbufferVector.swift
Normal file
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright 2024 Google Inc. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import Foundation
|
||||
|
||||
public struct FlatbufferVector<
|
||||
Element: FlatbuffersVectorInitializable
|
||||
>: RandomAccessCollection {
|
||||
public typealias Element = Element
|
||||
public typealias Index = Int
|
||||
|
||||
private let bb: ByteBuffer
|
||||
private let byteSize: Int
|
||||
private let startPosition: Int32
|
||||
public let count: Int
|
||||
|
||||
init(
|
||||
bb: ByteBuffer,
|
||||
startPosition: Int32,
|
||||
count: Int,
|
||||
byteSize: Int)
|
||||
{
|
||||
self.bb = bb
|
||||
self.byteSize = byteSize
|
||||
self.count = count
|
||||
self.startPosition = startPosition
|
||||
}
|
||||
|
||||
public var startIndex: Int {
|
||||
0
|
||||
}
|
||||
|
||||
public var endIndex: Int {
|
||||
count
|
||||
}
|
||||
|
||||
public var isEmpty: Bool {
|
||||
count == 0
|
||||
}
|
||||
|
||||
|
||||
public subscript(position: Int) -> Element {
|
||||
guard position < count else {
|
||||
fatalError(
|
||||
"Trying to read element at index \(position) in a vector of size \(count)")
|
||||
}
|
||||
let index = startPosition &+ Int32(position &* byteSize)
|
||||
return Element.readFrom(byteBuffer: bb, index: Int(index))
|
||||
}
|
||||
|
||||
public func index(after i: Int) -> Int {
|
||||
guard i < count else { return count }
|
||||
return i &+ 1
|
||||
}
|
||||
}
|
||||
|
||||
extension FlatbufferVector: Encodable where Element: Encodable {
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.unkeyedContainer()
|
||||
for element in self {
|
||||
try container.encode(element)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright 2024 Google Inc. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import Foundation
|
||||
|
||||
#if canImport(Common)
|
||||
import Common
|
||||
#endif
|
||||
|
||||
public protocol FlatbuffersVectorInitializable {
|
||||
static func readFrom(byteBuffer: ByteBuffer, index: Int) -> Self
|
||||
}
|
||||
|
||||
extension Scalar where Self: FlatbuffersVectorInitializable {
|
||||
public static func readFrom(byteBuffer: ByteBuffer, index: Int) -> Self {
|
||||
byteBuffer.read(def: Self.self, position: index)
|
||||
}
|
||||
}
|
||||
|
||||
extension NativeStruct where Self: FlatbuffersVectorInitializable {
|
||||
public static func readFrom(byteBuffer: ByteBuffer, index: Int) -> Self {
|
||||
byteBuffer.read(def: Self.self, position: index)
|
||||
}
|
||||
}
|
||||
|
||||
extension FlatBufferStruct {
|
||||
public static func readFrom(byteBuffer: ByteBuffer, index: Int) -> Self {
|
||||
Self.init(byteBuffer, o: Int32(index))
|
||||
}
|
||||
}
|
||||
|
||||
extension FlatBufferTable {
|
||||
public static func readFrom(byteBuffer: ByteBuffer, index: Int) -> Self {
|
||||
return Self.init(
|
||||
byteBuffer,
|
||||
o: Int32(index) &+ byteBuffer.read(def: Int32.self, position: index))
|
||||
}
|
||||
}
|
||||
|
||||
extension Optional: FlatbuffersVectorInitializable where Wrapped == String {
|
||||
public static func readFrom(byteBuffer: ByteBuffer, index: Int) -> Self {
|
||||
var index = Int32(index)
|
||||
index &+= byteBuffer.read(def: Int32.self, position: Int(index))
|
||||
let count = byteBuffer.read(def: Int32.self, position: Int(index))
|
||||
let position = Int(index) &+ MemoryLayout<Int32>.size
|
||||
return byteBuffer.readString(at: position, count: Int(count))
|
||||
}
|
||||
}
|
||||
|
||||
extension Enum where Self: FlatbuffersVectorInitializable {
|
||||
public static func readFrom(byteBuffer: ByteBuffer, index: Int) -> Self {
|
||||
Self
|
||||
.init(rawValue: byteBuffer.read(def: Self.T.self, position: index)) ??
|
||||
Self.min
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright 2024 Google Inc. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import Foundation
|
||||
|
||||
public struct UnionFlatbufferVector {
|
||||
|
||||
private let bb: ByteBuffer
|
||||
private let byteSize: Int
|
||||
private let startPosition: Int32
|
||||
public let count: Int
|
||||
|
||||
init(
|
||||
bb: ByteBuffer,
|
||||
startPosition: Int32,
|
||||
count: Int,
|
||||
byteSize: Int)
|
||||
{
|
||||
self.bb = bb
|
||||
self.byteSize = byteSize
|
||||
self.count = count
|
||||
self.startPosition = startPosition
|
||||
}
|
||||
|
||||
public var startIndex: Int {
|
||||
0
|
||||
}
|
||||
|
||||
public var endIndex: Int {
|
||||
count
|
||||
}
|
||||
|
||||
public var isEmpty: Bool {
|
||||
count == 0
|
||||
}
|
||||
|
||||
public subscript(
|
||||
position: Int,
|
||||
Type: FlatbuffersVectorInitializable
|
||||
.Type) -> FlatbuffersVectorInitializable
|
||||
{
|
||||
guard position < count else {
|
||||
fatalError(
|
||||
"Trying to read element at index \(position) in a vector of size \(count)")
|
||||
}
|
||||
let index = startPosition &+ Int32(position &* byteSize)
|
||||
return Type.readFrom(byteBuffer: bb, index: Int(index))
|
||||
}
|
||||
}
|
||||
@@ -41,8 +41,8 @@ public struct VerifierOptions {
|
||||
maxDepth: UOffset = 64,
|
||||
maxTableCount: UOffset = 1_000_000,
|
||||
maxApparentSize: UOffset = 1 << 31,
|
||||
ignoreMissingNullTerminators: Bool = false
|
||||
) {
|
||||
ignoreMissingNullTerminators: Bool = false)
|
||||
{
|
||||
_maxDepth = maxDepth
|
||||
_maxTableCount = maxTableCount
|
||||
_maxApparentSize = maxApparentSize
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
import Foundation
|
||||
|
||||
#if canImport(Common)
|
||||
import Common
|
||||
import Common
|
||||
#endif
|
||||
|
||||
/// Verifiable is a protocol all swift flatbuffers object should conform to,
|
||||
@@ -51,8 +51,8 @@ extension Verifiable {
|
||||
@discardableResult
|
||||
public static func verifyRange<T>(
|
||||
_ verifier: inout Verifier,
|
||||
at position: Int, of type: T.Type
|
||||
) throws -> (start: Int, count: Int) {
|
||||
at position: Int, of type: T.Type) throws -> (start: Int, count: Int)
|
||||
{
|
||||
let len: UOffset = try verifier.getValue(at: position)
|
||||
let intLen = Int(len)
|
||||
let start = Int(clamping: (position &+ MemoryLayout<Int32>.size).magnitude)
|
||||
@@ -74,8 +74,8 @@ extension Verifiable where Self: Scalar {
|
||||
public static func verify<T>(
|
||||
_ verifier: inout Verifier,
|
||||
at position: Int,
|
||||
of type: T.Type
|
||||
) throws where T: Verifiable {
|
||||
of type: T.Type) throws where T: Verifiable
|
||||
{
|
||||
try verifier.inBuffer(position: position, of: type.self)
|
||||
}
|
||||
}
|
||||
@@ -96,8 +96,8 @@ public enum ForwardOffset<U>: Verifiable where U: Verifiable {
|
||||
public static func verify<T>(
|
||||
_ verifier: inout Verifier,
|
||||
at position: Int,
|
||||
of type: T.Type
|
||||
) throws where T: Verifiable {
|
||||
of type: T.Type) throws where T: Verifiable
|
||||
{
|
||||
let offset: UOffset = try verifier.getValue(at: position)
|
||||
let nextOffset = Int(clamping: (Int(offset) &+ position).magnitude)
|
||||
try U.verify(&verifier, at: nextOffset, of: U.self)
|
||||
@@ -120,8 +120,8 @@ public enum Vector<U, S>: Verifiable where U: Verifiable, S: Verifiable {
|
||||
public static func verify<T>(
|
||||
_ verifier: inout Verifier,
|
||||
at position: Int,
|
||||
of type: T.Type
|
||||
) throws where T: Verifiable {
|
||||
of type: T.Type) throws where T: Verifiable
|
||||
{
|
||||
/// checks if the next verification type S is equal to U of type forwardOffset
|
||||
/// This had to be done since I couldnt find a solution for duplicate call functions
|
||||
/// A fix will be appreciated
|
||||
@@ -170,8 +170,8 @@ public enum UnionVector<S> where S: UnionEnum {
|
||||
fieldPosition: Int,
|
||||
unionKeyName: String,
|
||||
fieldName: String,
|
||||
completion: @escaping Completion
|
||||
) throws {
|
||||
completion: @escaping Completion) throws
|
||||
{
|
||||
/// Get offset for union key vectors and offset vectors
|
||||
let keyOffset: UOffset = try verifier.getValue(at: keyPosition)
|
||||
let fieldOffset: UOffset = try verifier.getValue(at: fieldPosition)
|
||||
|
||||
@@ -53,8 +53,8 @@ public struct Verifier {
|
||||
public init(
|
||||
buffer: inout ByteBuffer,
|
||||
options: VerifierOptions = .init(),
|
||||
checkAlignment: Bool = true
|
||||
) throws {
|
||||
checkAlignment: Bool = true) throws
|
||||
{
|
||||
guard buffer.capacity < FlatBufferMaxSize else {
|
||||
throw FlatbuffersErrors.exceedsMaxSizeAllowed
|
||||
}
|
||||
@@ -187,19 +187,19 @@ public struct Verifier {
|
||||
if offset > 0 {
|
||||
reportedOverflow =
|
||||
_int32Position
|
||||
.subtractingReportingOverflow(offset.magnitude)
|
||||
.subtractingReportingOverflow(offset.magnitude)
|
||||
} else {
|
||||
reportedOverflow =
|
||||
_int32Position
|
||||
.addingReportingOverflow(offset.magnitude)
|
||||
.addingReportingOverflow(offset.magnitude)
|
||||
}
|
||||
|
||||
/// since `subtractingReportingOverflow` & `addingReportingOverflow` returns true,
|
||||
/// if there is overflow we return failure
|
||||
if reportedOverflow.overflow
|
||||
|| reportedOverflow.partialValue
|
||||
> _buffer
|
||||
.capacity
|
||||
> _buffer
|
||||
.capacity
|
||||
{
|
||||
throw FlatbuffersErrors.signedOffsetOutOfBounds(
|
||||
offset: Int(offset),
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
import Foundation
|
||||
|
||||
#if canImport(Common)
|
||||
import Common
|
||||
import Common
|
||||
#endif
|
||||
|
||||
/// `ByteBuffer` is the interface that stores the data for a `Flatbuffers` object
|
||||
@@ -55,8 +55,8 @@ struct _InternalByteBuffer {
|
||||
capacity: Int,
|
||||
writerSize: Int,
|
||||
currentWritingIndex: Int,
|
||||
alignment: Int
|
||||
) {
|
||||
alignment: Int)
|
||||
{
|
||||
let newData = UnsafeMutableRawPointer.allocate(
|
||||
byteCount: capacity,
|
||||
alignment: alignment)
|
||||
@@ -144,18 +144,18 @@ struct _InternalByteBuffer {
|
||||
/// Adds a `ContiguousBytes` to buffer memory
|
||||
/// - Parameter value: bytes to copy
|
||||
#if swift(>=5.0) && !os(WASI)
|
||||
@inline(__always)
|
||||
@usableFromInline
|
||||
mutating func push(bytes: ContiguousBytes) {
|
||||
bytes.withUnsafeBytes { ptr in
|
||||
ensureSpace(size: ptr.count)
|
||||
memcpy(
|
||||
_storage.memory.advanced(by: writerIndex &- ptr.count),
|
||||
ptr.baseAddress!,
|
||||
ptr.count)
|
||||
_writerSize = _writerSize &+ ptr.count
|
||||
}
|
||||
@inline(__always)
|
||||
@usableFromInline
|
||||
mutating func push(bytes: ContiguousBytes) {
|
||||
bytes.withUnsafeBytes { ptr in
|
||||
ensureSpace(size: ptr.count)
|
||||
memcpy(
|
||||
_storage.memory.advanced(by: writerIndex &- ptr.count),
|
||||
ptr.baseAddress!,
|
||||
ptr.count)
|
||||
_writerSize = _writerSize &+ ptr.count
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Adds an object of type NativeStruct into the buffer
|
||||
@@ -200,7 +200,8 @@ struct _InternalByteBuffer {
|
||||
mutating func push(string str: String, len: Int) {
|
||||
ensureSpace(size: len)
|
||||
if str.utf8
|
||||
.withContiguousStorageIfAvailable({ self.push(bytes: $0, len: len) }) != nil
|
||||
.withContiguousStorageIfAvailable({ self.push(bytes: $0, len: len) }) !=
|
||||
nil
|
||||
{
|
||||
} else {
|
||||
let utf8View = str.utf8
|
||||
@@ -218,8 +219,8 @@ struct _InternalByteBuffer {
|
||||
@inline(__always)
|
||||
mutating func push(
|
||||
bytes: UnsafeBufferPointer<String.UTF8View.Element>,
|
||||
len: Int
|
||||
) -> Bool {
|
||||
len: Int) -> Bool
|
||||
{
|
||||
memcpy(
|
||||
_storage.memory.advanced(by: writerIndex &- len),
|
||||
bytes.baseAddress!,
|
||||
@@ -325,8 +326,8 @@ struct _InternalByteBuffer {
|
||||
@inline(__always)
|
||||
func withUnsafeBytes<T>(
|
||||
_ body: (UnsafeRawBufferPointer) throws
|
||||
-> T
|
||||
) rethrows -> T {
|
||||
-> T) rethrows -> T
|
||||
{
|
||||
try body(
|
||||
UnsafeRawBufferPointer(
|
||||
start: _storage.memory,
|
||||
@@ -337,8 +338,8 @@ struct _InternalByteBuffer {
|
||||
@inline(__always)
|
||||
func withUnsafeSlicedBytes<T>(
|
||||
_ body: (UnsafeRawBufferPointer) throws
|
||||
-> T
|
||||
) rethrows -> T {
|
||||
-> T) rethrows -> T
|
||||
{
|
||||
try body(
|
||||
UnsafeRawBufferPointer(
|
||||
start: _storage.memory.advanced(by: writerIndex),
|
||||
@@ -349,8 +350,8 @@ struct _InternalByteBuffer {
|
||||
@inline(__always)
|
||||
func withUnsafeRawPointer<T>(
|
||||
_ body: (UnsafeMutableRawPointer) throws
|
||||
-> T
|
||||
) rethrows -> T {
|
||||
-> T) rethrows -> T
|
||||
{
|
||||
try body(_storage.memory)
|
||||
}
|
||||
|
||||
@@ -358,8 +359,8 @@ struct _InternalByteBuffer {
|
||||
@inline(__always)
|
||||
func readWithUnsafeRawPointer<T>(
|
||||
position: Int,
|
||||
_ body: (UnsafeRawPointer) throws -> T
|
||||
) rethrows -> T {
|
||||
_ body: (UnsafeRawPointer) throws -> T) rethrows -> T
|
||||
{
|
||||
try body(_storage.memory.advanced(by: position))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,8 +29,8 @@ public struct ByteBuffer {
|
||||
@usableFromInline
|
||||
enum Blob {
|
||||
#if !os(WASI)
|
||||
case data(Data)
|
||||
case bytes(ContiguousBytes)
|
||||
case data(Data)
|
||||
case bytes(ContiguousBytes)
|
||||
#endif
|
||||
|
||||
case byteBuffer(_InternalByteBuffer)
|
||||
@@ -96,16 +96,16 @@ public struct ByteBuffer {
|
||||
@inline(__always)
|
||||
func withUnsafeBytes<T>(
|
||||
_ body: (UnsafeRawBufferPointer) throws
|
||||
-> T
|
||||
) rethrows -> T {
|
||||
-> T) rethrows -> T
|
||||
{
|
||||
switch retainedBlob {
|
||||
case .byteBuffer(let byteBuffer):
|
||||
return try byteBuffer.withUnsafeBytes(body)
|
||||
#if !os(WASI)
|
||||
case .data(let data):
|
||||
return try data.withUnsafeBytes(body)
|
||||
case .bytes(let contiguousBytes):
|
||||
return try contiguousBytes.withUnsafeBytes(body)
|
||||
case .data(let data):
|
||||
return try data.withUnsafeBytes(body)
|
||||
case .bytes(let contiguousBytes):
|
||||
return try contiguousBytes.withUnsafeBytes(body)
|
||||
#endif
|
||||
case .array(let array):
|
||||
return try array.withUnsafeBytes(body)
|
||||
@@ -118,21 +118,21 @@ public struct ByteBuffer {
|
||||
@inline(__always)
|
||||
func withUnsafeRawPointer<T>(
|
||||
_ body: (UnsafeMutableRawPointer) throws
|
||||
-> T
|
||||
) rethrows -> T {
|
||||
-> T) rethrows -> T
|
||||
{
|
||||
switch retainedBlob {
|
||||
case .byteBuffer(let byteBuffer):
|
||||
return try byteBuffer.withUnsafeRawPointer(body)
|
||||
#if !os(WASI)
|
||||
case .data(let data):
|
||||
return
|
||||
try data
|
||||
case .data(let data):
|
||||
return
|
||||
try data
|
||||
.withUnsafeBytes {
|
||||
try body(UnsafeMutableRawPointer(mutating: $0.baseAddress!))
|
||||
}
|
||||
case .bytes(let contiguousBytes):
|
||||
return
|
||||
try contiguousBytes
|
||||
case .bytes(let contiguousBytes):
|
||||
return
|
||||
try contiguousBytes
|
||||
.withUnsafeBytes {
|
||||
try body(UnsafeMutableRawPointer(mutating: $0.baseAddress!))
|
||||
}
|
||||
@@ -140,9 +140,9 @@ public struct ByteBuffer {
|
||||
case .array(let array):
|
||||
return
|
||||
try array
|
||||
.withUnsafeBytes {
|
||||
try body(UnsafeMutableRawPointer(mutating: $0.baseAddress!))
|
||||
}
|
||||
.withUnsafeBytes {
|
||||
try body(UnsafeMutableRawPointer(mutating: $0.baseAddress!))
|
||||
}
|
||||
case .pointer(let ptr):
|
||||
return try body(ptr)
|
||||
}
|
||||
@@ -152,20 +152,20 @@ public struct ByteBuffer {
|
||||
@inline(__always)
|
||||
func readWithUnsafeRawPointer<T>(
|
||||
position: Int,
|
||||
_ body: (UnsafeRawPointer) throws -> T
|
||||
) rethrows -> T {
|
||||
_ body: (UnsafeRawPointer) throws -> T) rethrows -> T
|
||||
{
|
||||
switch retainedBlob {
|
||||
case .byteBuffer(let byteBuffer):
|
||||
return try byteBuffer.readWithUnsafeRawPointer(position: position, body)
|
||||
#if !os(WASI)
|
||||
case .data(let data):
|
||||
return try data.withUnsafeBytes {
|
||||
try body($0.baseAddress!.advanced(by: position))
|
||||
}
|
||||
case .bytes(let contiguousBytes):
|
||||
return try contiguousBytes.withUnsafeBytes {
|
||||
try body($0.baseAddress!.advanced(by: position))
|
||||
}
|
||||
case .data(let data):
|
||||
return try data.withUnsafeBytes {
|
||||
try body($0.baseAddress!.advanced(by: position))
|
||||
}
|
||||
case .bytes(let contiguousBytes):
|
||||
return try contiguousBytes.withUnsafeBytes {
|
||||
try body($0.baseAddress!.advanced(by: position))
|
||||
}
|
||||
#endif
|
||||
case .array(let array):
|
||||
return try array.withUnsafeBytes {
|
||||
@@ -207,8 +207,8 @@ public struct ByteBuffer {
|
||||
@inline(__always)
|
||||
public init(
|
||||
copyingMemoryBound memory: UnsafeRawPointer,
|
||||
capacity: Int
|
||||
) {
|
||||
capacity: Int)
|
||||
{
|
||||
_storage = Storage(count: capacity)
|
||||
_storage.copy(from: memory, count: capacity)
|
||||
_readerIndex = capacity
|
||||
@@ -226,29 +226,29 @@ public struct ByteBuffer {
|
||||
}
|
||||
|
||||
#if !os(WASI)
|
||||
/// Constructor that creates a Flatbuffer from the Swift Data type object
|
||||
/// - Parameter
|
||||
/// - data: Swift data Object
|
||||
@inline(__always)
|
||||
public init(data: Data) {
|
||||
_storage = Storage(blob: .data(data), capacity: data.count)
|
||||
_readerIndex = data.count
|
||||
capacity = data.count
|
||||
}
|
||||
/// Constructor that creates a Flatbuffer from the Swift Data type object
|
||||
/// - Parameter
|
||||
/// - data: Swift data Object
|
||||
@inline(__always)
|
||||
public init(data: Data) {
|
||||
_storage = Storage(blob: .data(data), capacity: data.count)
|
||||
_readerIndex = data.count
|
||||
capacity = data.count
|
||||
}
|
||||
|
||||
/// Constructor that creates a Flatbuffer object from a ContiguousBytes
|
||||
/// - Parameters:
|
||||
/// - contiguousBytes: Binary stripe to use as the buffer
|
||||
/// - count: amount of readable bytes
|
||||
@inline(__always)
|
||||
public init<Bytes: ContiguousBytes>(
|
||||
contiguousBytes: Bytes,
|
||||
count: Int
|
||||
) {
|
||||
_storage = Storage(blob: .bytes(contiguousBytes), capacity: count)
|
||||
_readerIndex = count
|
||||
capacity = count
|
||||
}
|
||||
/// Constructor that creates a Flatbuffer object from a ContiguousBytes
|
||||
/// - Parameters:
|
||||
/// - contiguousBytes: Binary stripe to use as the buffer
|
||||
/// - count: amount of readable bytes
|
||||
@inline(__always)
|
||||
public init<Bytes: ContiguousBytes>(
|
||||
contiguousBytes: Bytes,
|
||||
count: Int)
|
||||
{
|
||||
_storage = Storage(blob: .bytes(contiguousBytes), capacity: count)
|
||||
_readerIndex = count
|
||||
capacity = count
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Constructor that creates a Flatbuffer from unsafe memory region without copying
|
||||
@@ -260,8 +260,8 @@ public struct ByteBuffer {
|
||||
@inline(__always)
|
||||
public init(
|
||||
assumingMemoryBound memory: UnsafeMutableRawPointer,
|
||||
capacity: Int
|
||||
) {
|
||||
capacity: Int)
|
||||
{
|
||||
_storage = Storage(
|
||||
blob: .pointer(memory),
|
||||
capacity: capacity)
|
||||
@@ -278,8 +278,8 @@ public struct ByteBuffer {
|
||||
init(
|
||||
blob: Storage.Blob,
|
||||
count: Int,
|
||||
removing removeBytes: Int
|
||||
) {
|
||||
removing removeBytes: Int)
|
||||
{
|
||||
_storage = Storage(blob: blob, capacity: count)
|
||||
_readerIndex = removeBytes
|
||||
capacity = count
|
||||
@@ -371,8 +371,8 @@ public struct ByteBuffer {
|
||||
t3: T3.Type,
|
||||
t4: T4.Type,
|
||||
position: Int,
|
||||
byteWidth: UInt8
|
||||
) -> T {
|
||||
byteWidth: UInt8) -> T
|
||||
{
|
||||
switch byteWidth {
|
||||
case 1:
|
||||
numericCast(read(def: T1.self, position: position))
|
||||
@@ -392,8 +392,8 @@ public struct ByteBuffer {
|
||||
@inline(__always)
|
||||
public func readSlice<T>(
|
||||
index: Int,
|
||||
count: Int
|
||||
) -> [T] {
|
||||
count: Int) -> [T]
|
||||
{
|
||||
assert(
|
||||
index + count <= capacity,
|
||||
"Reading out of bounds is illegal")
|
||||
@@ -410,8 +410,8 @@ public struct ByteBuffer {
|
||||
public func readString(
|
||||
at index: Int,
|
||||
count: Int,
|
||||
type: String.Encoding
|
||||
) -> String? {
|
||||
type: String.Encoding) -> String?
|
||||
{
|
||||
assert(
|
||||
index + count <= capacity,
|
||||
"Reading out of bounds is illegal")
|
||||
@@ -432,8 +432,8 @@ public struct ByteBuffer {
|
||||
@inline(__always)
|
||||
public func readString(
|
||||
at index: Int,
|
||||
count: Int
|
||||
) -> String? {
|
||||
count: Int) -> String?
|
||||
{
|
||||
assert(
|
||||
index + count <= capacity,
|
||||
"Reading out of bounds is illegal")
|
||||
@@ -451,8 +451,8 @@ public struct ByteBuffer {
|
||||
public func withUnsafePointerToSlice<T>(
|
||||
index: Int,
|
||||
count: Int,
|
||||
body: (UnsafeRawBufferPointer) throws -> T
|
||||
) rethrows -> T {
|
||||
body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T
|
||||
{
|
||||
assert(
|
||||
index + count <= capacity,
|
||||
"Reading out of bounds is illegal")
|
||||
@@ -465,8 +465,8 @@ public struct ByteBuffer {
|
||||
@inline(__always)
|
||||
public func withUnsafeBytes<T>(
|
||||
body: (UnsafeRawBufferPointer) throws
|
||||
-> T
|
||||
) rethrows -> T {
|
||||
-> T) rethrows -> T
|
||||
{
|
||||
try _storage.withUnsafeBytes(body)
|
||||
}
|
||||
|
||||
@@ -474,8 +474,8 @@ public struct ByteBuffer {
|
||||
@inline(__always)
|
||||
func withUnsafeMutableRawPointer<T>(
|
||||
body: (UnsafeMutableRawPointer) throws
|
||||
-> T
|
||||
) rethrows -> T {
|
||||
-> T) rethrows -> T
|
||||
{
|
||||
try _storage.withUnsafeRawPointer(body)
|
||||
}
|
||||
|
||||
@@ -483,8 +483,8 @@ public struct ByteBuffer {
|
||||
@inline(__always)
|
||||
func readWithUnsafeRawPointer<T>(
|
||||
position: Int,
|
||||
_ body: (UnsafeRawPointer) throws -> T
|
||||
) rethrows -> T {
|
||||
_ body: (UnsafeRawPointer) throws -> T) rethrows -> T
|
||||
{
|
||||
try _storage.readWithUnsafeRawPointer(position: position, body)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,8 +46,7 @@ public enum FlexBufferType: UInt64 {
|
||||
@available(
|
||||
*,
|
||||
deprecated,
|
||||
message: "use FBT_VECTOR or FBT_VECTOR_KEY instead."
|
||||
)
|
||||
message: "use FBT_VECTOR or FBT_VECTOR_KEY instead.")
|
||||
case vectorString = 15
|
||||
|
||||
/// Typed tuples (no type table, no size field).
|
||||
|
||||
@@ -31,8 +31,8 @@ public struct FixedTypedVector: FlexBufferVector {
|
||||
offset: Int,
|
||||
byteWidth: UInt8,
|
||||
type: FlexBufferType,
|
||||
count: Int
|
||||
) {
|
||||
count: Int)
|
||||
{
|
||||
self.byteBuffer = byteBuffer
|
||||
self.offset = offset
|
||||
self.byteWidth = byteWidth
|
||||
|
||||
@@ -39,14 +39,13 @@ public protocol FlexBufferContiguousBytes {
|
||||
var count: Int { get }
|
||||
|
||||
func withUnsafeRawBufferPointer<Result>(
|
||||
_ body: (UnsafeRawBufferPointer) throws -> Result
|
||||
) rethrows -> Result
|
||||
_ body: (UnsafeRawBufferPointer) throws -> Result) rethrows -> Result
|
||||
}
|
||||
|
||||
extension FlexBufferContiguousBytes {
|
||||
public func withUnsafeRawBufferPointer<Result>(
|
||||
_ body: (UnsafeRawBufferPointer) throws -> Result
|
||||
) rethrows -> Result {
|
||||
_ body: (UnsafeRawBufferPointer) throws -> Result) rethrows -> Result
|
||||
{
|
||||
try byteBuffer.withUnsafePointerToSlice(
|
||||
index: offset,
|
||||
count: count,
|
||||
|
||||
@@ -58,8 +58,8 @@ public struct Reference {
|
||||
byteBuffer: ByteBuffer,
|
||||
offset: Int,
|
||||
parentWidth: UInt8,
|
||||
packedType: UInt8
|
||||
) {
|
||||
packedType: UInt8)
|
||||
{
|
||||
guard let type = FlexBufferType(rawValue: UInt64(packedType >> 2)) else {
|
||||
return nil
|
||||
}
|
||||
@@ -76,8 +76,8 @@ public struct Reference {
|
||||
offset: Int,
|
||||
parentWidth: UInt8,
|
||||
byteWidth: UInt8,
|
||||
type: FlexBufferType
|
||||
) {
|
||||
type: FlexBufferType)
|
||||
{
|
||||
self.byteBuffer = byteBuffer
|
||||
self.offset = offset
|
||||
self.parentWidth = parentWidth
|
||||
@@ -241,8 +241,7 @@ public struct Reference {
|
||||
@inline(__always)
|
||||
public func withUnsafeRawPointer<Result>(
|
||||
_ completion: (UnsafeRawPointer) throws
|
||||
-> Result
|
||||
)
|
||||
-> Result)
|
||||
rethrows -> Result?
|
||||
{
|
||||
return try byteBuffer.readWithUnsafeRawPointer(
|
||||
|
||||
@@ -30,8 +30,8 @@ public struct TypedVector: FlexBufferVector {
|
||||
byteBuffer: ByteBuffer,
|
||||
offset: Int,
|
||||
byteWidth: UInt8,
|
||||
type: FlexBufferType
|
||||
) {
|
||||
type: FlexBufferType)
|
||||
{
|
||||
self.byteBuffer = byteBuffer
|
||||
self.offset = offset
|
||||
self.byteWidth = byteWidth
|
||||
@@ -54,8 +54,8 @@ public struct TypedVector: FlexBufferVector {
|
||||
static func mapKeys(
|
||||
byteBuffer: ByteBuffer,
|
||||
offset: Int,
|
||||
byteWidth: UInt8
|
||||
) -> TypedVector {
|
||||
byteWidth: UInt8) -> TypedVector
|
||||
{
|
||||
let prefixedFields = 3
|
||||
let keysOffset = offset &- (numericCast(byteWidth) &* prefixedFields)
|
||||
|
||||
@@ -88,8 +88,8 @@ extension TypedVector {
|
||||
byteWidth)
|
||||
|
||||
return byteBuffer.readWithUnsafeRawPointer(
|
||||
position: indirectoffset
|
||||
) { bufPointer in
|
||||
position: indirectoffset)
|
||||
{ bufPointer in
|
||||
target.withCString { strPointer in
|
||||
Int(strcmp(bufPointer, strPointer))
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
import Foundation
|
||||
|
||||
#if canImport(Common)
|
||||
import Common
|
||||
import Common
|
||||
#endif
|
||||
|
||||
extension UInt64 {
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
import Foundation
|
||||
|
||||
#if canImport(Common)
|
||||
import Common
|
||||
import Common
|
||||
#endif
|
||||
|
||||
public struct Value: Equatable {
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
import Foundation
|
||||
|
||||
#if canImport(Common)
|
||||
import Common
|
||||
import Common
|
||||
#endif
|
||||
|
||||
@inline(__always)
|
||||
@@ -128,13 +128,14 @@ func toFixedTypedVectorElementType(type: FlexBufferType)
|
||||
type.rawValue
|
||||
&- FlexBufferType.vectorInt2
|
||||
.rawValue)
|
||||
let len: Int = numericCast(fixedType.dividedReportingOverflow(by: 3).partialValue &+ 2)
|
||||
let len: Int = numericCast(
|
||||
fixedType.dividedReportingOverflow(by: 3)
|
||||
.partialValue &+ 2)
|
||||
return (
|
||||
FlexBufferType(
|
||||
rawValue: (fixedType.quotientAndRemainder(dividingBy: 3).remainder)
|
||||
&+ FlexBufferType.int.rawValue),
|
||||
len
|
||||
)
|
||||
len)
|
||||
}
|
||||
|
||||
// MARK: - Reader functions
|
||||
@@ -142,8 +143,8 @@ func toFixedTypedVectorElementType(type: FlexBufferType)
|
||||
@inline(__always)
|
||||
func binarySearch(
|
||||
vector: TypedVector,
|
||||
target: String
|
||||
) -> Int? {
|
||||
target: String) -> Int?
|
||||
{
|
||||
var left = 0
|
||||
var right = vector.count
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
import Foundation
|
||||
|
||||
#if canImport(Common)
|
||||
import Common
|
||||
import Common
|
||||
#endif
|
||||
|
||||
private let twentyFourBytes: Int = 24
|
||||
@@ -74,21 +74,21 @@ public struct FlexBuffersWriter {
|
||||
}
|
||||
|
||||
#if !os(WASI)
|
||||
/// Data representation of the buffer
|
||||
///
|
||||
/// Should only be used after ``finish(offset:addPrefix:)`` is called
|
||||
public var data: Data {
|
||||
assert(finished, "Data shouldn't be called before finish()")
|
||||
return _bb.withUnsafeSlicedBytes { ptr in
|
||||
var data = Data()
|
||||
data.append(
|
||||
ptr.baseAddress!.bindMemory(
|
||||
to: UInt8.self,
|
||||
capacity: ptr.count),
|
||||
count: ptr.count)
|
||||
return data
|
||||
}
|
||||
/// Data representation of the buffer
|
||||
///
|
||||
/// Should only be used after ``finish(offset:addPrefix:)`` is called
|
||||
public var data: Data {
|
||||
assert(finished, "Data shouldn't be called before finish()")
|
||||
return _bb.withUnsafeSlicedBytes { ptr in
|
||||
var data = Data()
|
||||
data.append(
|
||||
ptr.baseAddress!.bindMemory(
|
||||
to: UInt8.self,
|
||||
capacity: ptr.count),
|
||||
count: ptr.count)
|
||||
return data
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Resets the internal state. Automatically called before building a new flexbuffer.
|
||||
@@ -139,8 +139,8 @@ public struct FlexBuffersWriter {
|
||||
public mutating func endVector(
|
||||
start: Int,
|
||||
typed: Bool = false,
|
||||
fixed: Bool = false
|
||||
) -> UInt64 {
|
||||
fixed: Bool = false) -> UInt64
|
||||
{
|
||||
let vec = createVector(
|
||||
start: start,
|
||||
count: stack.count &- start,
|
||||
@@ -162,7 +162,8 @@ public struct FlexBuffersWriter {
|
||||
@discardableResult
|
||||
@inline(__always)
|
||||
public mutating func create<T>(vector: [T], key: borrowing String) -> Int
|
||||
where T: Scalar {
|
||||
where T: Scalar
|
||||
{
|
||||
add(key: key)
|
||||
return create(vector: vector, fixed: false)
|
||||
}
|
||||
@@ -178,8 +179,8 @@ public struct FlexBuffersWriter {
|
||||
@inline(__always)
|
||||
public mutating func createFixed<T>(
|
||||
vector: [T],
|
||||
key: borrowing String
|
||||
) -> Int where T: Scalar {
|
||||
key: borrowing String) -> Int where T: Scalar
|
||||
{
|
||||
assert(vector.count >= 2 && vector.count <= 4)
|
||||
add(key: key)
|
||||
return create(vector: vector, fixed: true)
|
||||
@@ -301,8 +302,8 @@ public struct FlexBuffersWriter {
|
||||
@inline(__always)
|
||||
public mutating func add(
|
||||
uint64 value: borrowing UInt64,
|
||||
key: borrowing String
|
||||
) {
|
||||
key: borrowing String)
|
||||
{
|
||||
add(key: key)
|
||||
add(uint64: value)
|
||||
}
|
||||
@@ -315,8 +316,8 @@ public struct FlexBuffersWriter {
|
||||
@inline(__always)
|
||||
public mutating func indirect(
|
||||
uint64 val: borrowing UInt64,
|
||||
key: borrowing String
|
||||
) {
|
||||
key: borrowing String)
|
||||
{
|
||||
add(key: key)
|
||||
indirect(uint64: val)
|
||||
}
|
||||
@@ -324,8 +325,8 @@ public struct FlexBuffersWriter {
|
||||
@inline(__always)
|
||||
public mutating func indirect(
|
||||
uint val: borrowing UInt,
|
||||
key: borrowing String
|
||||
) {
|
||||
key: borrowing String)
|
||||
{
|
||||
add(key: key)
|
||||
indirect(uint64: numericCast(val))
|
||||
}
|
||||
@@ -384,8 +385,8 @@ public struct FlexBuffersWriter {
|
||||
@inline(__always)
|
||||
public mutating func add(
|
||||
int64 value: borrowing Int64,
|
||||
key: borrowing String
|
||||
) {
|
||||
key: borrowing String)
|
||||
{
|
||||
add(key: key)
|
||||
add(int64: value)
|
||||
}
|
||||
@@ -398,8 +399,8 @@ public struct FlexBuffersWriter {
|
||||
@inline(__always)
|
||||
public mutating func indirect(
|
||||
int64 val: borrowing Int64,
|
||||
key: borrowing String
|
||||
) {
|
||||
key: borrowing String)
|
||||
{
|
||||
add(key: key)
|
||||
indirect(int64: val)
|
||||
}
|
||||
@@ -407,8 +408,8 @@ public struct FlexBuffersWriter {
|
||||
@inline(__always)
|
||||
public mutating func indirect(
|
||||
int val: borrowing Int,
|
||||
key: borrowing String
|
||||
) {
|
||||
key: borrowing String)
|
||||
{
|
||||
add(key: key)
|
||||
indirect(int64: numericCast(val))
|
||||
}
|
||||
@@ -423,8 +424,8 @@ public struct FlexBuffersWriter {
|
||||
@inline(__always)
|
||||
public mutating func add(
|
||||
float32 value: borrowing Float32,
|
||||
key: borrowing String
|
||||
) {
|
||||
key: borrowing String)
|
||||
{
|
||||
add(key: key)
|
||||
add(float32: value)
|
||||
}
|
||||
@@ -437,8 +438,8 @@ public struct FlexBuffersWriter {
|
||||
@inline(__always)
|
||||
public mutating func indirect(
|
||||
float32 val: borrowing Float32,
|
||||
key: borrowing String
|
||||
) {
|
||||
key: borrowing String)
|
||||
{
|
||||
add(key: key)
|
||||
indirect(float32: val)
|
||||
}
|
||||
@@ -451,8 +452,8 @@ public struct FlexBuffersWriter {
|
||||
@inline(__always)
|
||||
public mutating func add(
|
||||
double value: borrowing Double,
|
||||
key: borrowing String
|
||||
) {
|
||||
key: borrowing String)
|
||||
{
|
||||
add(key: key)
|
||||
add(double: value)
|
||||
}
|
||||
@@ -465,8 +466,8 @@ public struct FlexBuffersWriter {
|
||||
@inline(__always)
|
||||
public mutating func indirect(
|
||||
double val: borrowing Double,
|
||||
key: borrowing String
|
||||
) {
|
||||
key: borrowing String)
|
||||
{
|
||||
add(key: key)
|
||||
indirect(double: val)
|
||||
}
|
||||
@@ -488,8 +489,8 @@ public struct FlexBuffersWriter {
|
||||
@inline(__always)
|
||||
public mutating func add<T>(
|
||||
blob: borrowing T,
|
||||
length l: Int
|
||||
) -> UInt where T: ContiguousBytes {
|
||||
length l: Int) -> UInt where T: ContiguousBytes
|
||||
{
|
||||
storeBlob(blob, len: l, type: .blob)
|
||||
}
|
||||
|
||||
@@ -498,8 +499,8 @@ public struct FlexBuffersWriter {
|
||||
public mutating func add<T>(
|
||||
blob: borrowing T,
|
||||
key: borrowing String,
|
||||
length l: Int
|
||||
) -> UInt where T: ContiguousBytes {
|
||||
length l: Int) -> UInt where T: ContiguousBytes
|
||||
{
|
||||
add(key: key)
|
||||
return storeBlob(blob, len: l, type: .blob)
|
||||
}
|
||||
@@ -540,8 +541,8 @@ public struct FlexBuffersWriter {
|
||||
mutating func pushIndirect<T>(
|
||||
value: T,
|
||||
type: FlexBufferType,
|
||||
bitWidth: BitWidth
|
||||
) {
|
||||
bitWidth: BitWidth)
|
||||
{
|
||||
let byteWidth = align(width: bitWidth)
|
||||
let iloc = writerIndex
|
||||
_bb.ensureSpace(size: byteWidth)
|
||||
@@ -618,8 +619,8 @@ public struct FlexBuffersWriter {
|
||||
mutating func storeBlob<T>(
|
||||
_ bytes: T,
|
||||
len: Int,
|
||||
type: FlexBufferType
|
||||
) -> UInt where T: ContiguousBytes {
|
||||
type: FlexBufferType) -> UInt where T: ContiguousBytes
|
||||
{
|
||||
return bytes.withUnsafeBytes {
|
||||
storeBlob(pointer: $0.baseAddress!, len: len, type: type)
|
||||
}
|
||||
@@ -631,8 +632,8 @@ public struct FlexBuffersWriter {
|
||||
pointer: borrowing UnsafeRawPointer,
|
||||
len: Int,
|
||||
trailing: Int = 0,
|
||||
type: FlexBufferType
|
||||
) -> UInt {
|
||||
type: FlexBufferType) -> UInt
|
||||
{
|
||||
_bb.ensureSpace(size: len &+ trailing)
|
||||
let bitWidth = widthU(numericCast(len))
|
||||
|
||||
@@ -655,7 +656,8 @@ public struct FlexBuffersWriter {
|
||||
@discardableResult
|
||||
@usableFromInline
|
||||
mutating func create<T>(vector: [T], fixed: Bool) -> Int
|
||||
where T: Scalar {
|
||||
where T: Scalar
|
||||
{
|
||||
let length: UInt64 = numericCast(vector.count)
|
||||
let vectorType = getScalarType(type: T.self)
|
||||
let byteWidth = MemoryLayout<T>.size
|
||||
@@ -691,8 +693,8 @@ public struct FlexBuffersWriter {
|
||||
step: Int,
|
||||
typed: Bool,
|
||||
fixed: Bool,
|
||||
keys: Value? = nil
|
||||
) -> Value {
|
||||
keys: Value? = nil) -> Value
|
||||
{
|
||||
assert(
|
||||
!fixed || typed,
|
||||
"Typed false and fixed true is a combination not supported currently")
|
||||
@@ -857,8 +859,8 @@ extension FlexBuffersWriter {
|
||||
@discardableResult
|
||||
public mutating func vector(
|
||||
key: String,
|
||||
_ closure: FlexBuffersWriterBuilder
|
||||
) -> UInt64 {
|
||||
_ closure: FlexBuffersWriterBuilder) -> UInt64
|
||||
{
|
||||
let start = startVector(key: key)
|
||||
closure(&self)
|
||||
return endVector(start: start)
|
||||
@@ -879,8 +881,8 @@ extension FlexBuffersWriter {
|
||||
@discardableResult
|
||||
public mutating func map(
|
||||
key: String,
|
||||
_ closure: FlexBuffersWriterBuilder
|
||||
) -> UInt64 {
|
||||
_ closure: FlexBuffersWriterBuilder) -> UInt64
|
||||
{
|
||||
let start = startMap(key: key)
|
||||
closure(&self)
|
||||
return endMap(start: start)
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
import Foundation
|
||||
|
||||
#if canImport(Common)
|
||||
import Common
|
||||
import Common
|
||||
#endif
|
||||
|
||||
/// `ByteBuffer` is the interface that stores the data for a `Flatbuffers` object
|
||||
@@ -55,8 +55,8 @@ struct _InternalByteBuffer {
|
||||
func reallocate(
|
||||
capacity: Int,
|
||||
writerSize: Int,
|
||||
alignment: Int
|
||||
) {
|
||||
alignment: Int)
|
||||
{
|
||||
let newData = UnsafeMutableRawPointer.allocate(
|
||||
byteCount: capacity,
|
||||
alignment: alignment)
|
||||
@@ -135,18 +135,17 @@ struct _InternalByteBuffer {
|
||||
_storage.reallocate(
|
||||
capacity: capacity,
|
||||
writerSize: writerIndex,
|
||||
alignment: alignment
|
||||
)
|
||||
alignment: alignment)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
mutating func addPadding(bytes: Int) {
|
||||
writerIndex =
|
||||
writerIndex
|
||||
&+ numericCast(
|
||||
padding(
|
||||
bufSize: numericCast(writerIndex),
|
||||
elementSize: numericCast(bytes)))
|
||||
&+ numericCast(
|
||||
padding(
|
||||
bufSize: numericCast(writerIndex),
|
||||
elementSize: numericCast(bytes)))
|
||||
ensureSpace(size: writerIndex)
|
||||
}
|
||||
|
||||
@@ -174,8 +173,8 @@ struct _InternalByteBuffer {
|
||||
@inline(__always)
|
||||
func withUnsafeBytes<T>(
|
||||
_ body: (UnsafeRawBufferPointer) throws
|
||||
-> T
|
||||
) rethrows -> T {
|
||||
-> T) rethrows -> T
|
||||
{
|
||||
try body(
|
||||
UnsafeRawBufferPointer(
|
||||
start: _storage.memory,
|
||||
@@ -186,8 +185,8 @@ struct _InternalByteBuffer {
|
||||
@inline(__always)
|
||||
func withUnsafeSlicedBytes<T>(
|
||||
_ body: (UnsafeRawBufferPointer) throws
|
||||
-> T
|
||||
) rethrows -> T {
|
||||
-> T) rethrows -> T
|
||||
{
|
||||
try body(
|
||||
UnsafeRawBufferPointer(
|
||||
start: _storage.memory,
|
||||
@@ -198,8 +197,8 @@ struct _InternalByteBuffer {
|
||||
@inline(__always)
|
||||
func withUnsafeRawPointer<T>(
|
||||
_ body: (UnsafeMutableRawPointer) throws
|
||||
-> T
|
||||
) rethrows -> T {
|
||||
-> T) rethrows -> T
|
||||
{
|
||||
try body(_storage.memory)
|
||||
}
|
||||
|
||||
@@ -207,8 +206,8 @@ struct _InternalByteBuffer {
|
||||
@inline(__always)
|
||||
func readWithUnsafeRawPointer<T>(
|
||||
position: Int,
|
||||
_ body: (UnsafeRawPointer) throws -> T
|
||||
) rethrows -> T {
|
||||
_ body: (UnsafeRawPointer) throws -> T) rethrows -> T
|
||||
{
|
||||
try body(_storage.memory.advanced(by: position))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user