mirror of
https://github.com/google/flatbuffers.git
synced 2026-07-04 12:41:09 +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:
@@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user