1
1
/*
2
- * Copyright 2023 Google Inc. All rights reserved.
2
+ * Copyright 2024 Google Inc. All rights reserved.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
14
14
* limitations under the License.
15
15
*/
16
16
17
- #if !os(WASI)
18
17
import Foundation
19
- #else
20
- import SwiftOverlayShims
21
- #endif
22
18
23
19
/// `ByteBuffer` is the interface that stores the data for a `Flatbuffers` object
24
20
/// it allows users to write and read data directly from memory thus the use of its
@@ -121,37 +117,37 @@ public struct ByteBuffer {
121
117
public let allowReadingUnalignedBuffers : Bool
122
118
123
119
/// Constructor that creates a Flatbuffer object from a UInt8
124
- /// - Parameter
120
+ /// - Parameter
125
121
/// - bytes: Array of UInt8
126
122
/// - allowReadingUnalignedBuffers: allow reading from unaligned buffer
127
123
public init (
128
- bytes: [ UInt8 ] ,
124
+ bytes: [ UInt8 ] ,
129
125
allowReadingUnalignedBuffers allowUnalignedBuffers: Bool = false )
130
126
{
131
127
var b = bytes
132
128
_storage = Storage ( count: bytes. count, alignment: alignment)
133
129
_writerSize = _storage. capacity
134
130
allowReadingUnalignedBuffers = allowUnalignedBuffers
135
131
b. withUnsafeMutableBytes { bufferPointer in
136
- self . _storage. copy ( from: bufferPointer. baseAddress!, count: bytes. count)
132
+ _storage. copy ( from: bufferPointer. baseAddress!, count: bytes. count)
137
133
}
138
134
}
139
135
140
136
#if !os(WASI)
141
137
/// Constructor that creates a Flatbuffer from the Swift Data type object
142
- /// - Parameter
138
+ /// - Parameter
143
139
/// - data: Swift data Object
144
140
/// - allowReadingUnalignedBuffers: allow reading from unaligned buffer
145
141
public init (
146
- data: Data ,
142
+ data: Data ,
147
143
allowReadingUnalignedBuffers allowUnalignedBuffers: Bool = false )
148
144
{
149
145
var b = data
150
146
_storage = Storage ( count: data. count, alignment: alignment)
151
147
_writerSize = _storage. capacity
152
148
allowReadingUnalignedBuffers = allowUnalignedBuffers
153
149
b. withUnsafeMutableBytes { bufferPointer in
154
- self . _storage. copy ( from: bufferPointer. baseAddress!, count: data. count)
150
+ _storage. copy ( from: bufferPointer. baseAddress!, count: data. count)
155
151
}
156
152
}
157
153
#endif
@@ -175,7 +171,7 @@ public struct ByteBuffer {
175
171
/// - allowReadingUnalignedBuffers: allow reading from unaligned buffer
176
172
public init < Bytes: ContiguousBytes > (
177
173
contiguousBytes: Bytes ,
178
- count: Int ,
174
+ count: Int ,
179
175
allowReadingUnalignedBuffers allowUnalignedBuffers: Bool = false )
180
176
{
181
177
_storage = Storage ( count: count, alignment: alignment)
@@ -208,7 +204,7 @@ public struct ByteBuffer {
208
204
/// - count: count of bytes
209
205
/// - allowReadingUnalignedBuffers: allow reading from unaligned buffer
210
206
init (
211
- memory: UnsafeMutableRawPointer ,
207
+ memory: UnsafeMutableRawPointer ,
212
208
count: Int ,
213
209
allowReadingUnalignedBuffers allowUnalignedBuffers: Bool = false )
214
210
{
@@ -228,7 +224,7 @@ public struct ByteBuffer {
228
224
memory: UnsafeMutableRawPointer ,
229
225
count: Int ,
230
226
removing removeBytes: Int ,
231
- allowReadingUnalignedBuffers allowUnalignedBuffers: Bool = false )
227
+ allowReadingUnalignedBuffers allowUnalignedBuffers: Bool = false )
232
228
{
233
229
_storage = Storage ( count: count, alignment: alignment)
234
230
_storage. copy ( from: memory, count: count)
@@ -257,7 +253,7 @@ public struct ByteBuffer {
257
253
_storage. memory. advanced ( by: writerIndex &- ptr. count) ,
258
254
UnsafeRawPointer ( ptr. baseAddress!) ,
259
255
ptr. count)
260
- self . _writerSize = self . _writerSize &+ ptr. count
256
+ _writerSize = _writerSize &+ ptr. count
261
257
}
262
258
}
263
259
@@ -271,7 +267,7 @@ public struct ByteBuffer {
271
267
_storage. memory
272
268
. advanced ( by: writerIndex &- ptr. count)
273
269
. copyMemory ( from: ptr. baseAddress!, byteCount: ptr. count)
274
- self . _writerSize = self . _writerSize &+ ptr. count
270
+ _writerSize = _writerSize &+ ptr. count
275
271
}
276
272
}
277
273
@@ -287,7 +283,7 @@ public struct ByteBuffer {
287
283
_storage. memory. advanced ( by: writerIndex &- ptr. count) ,
288
284
UnsafeRawPointer ( ptr. baseAddress!) ,
289
285
ptr. count)
290
- self . _writerSize = self . _writerSize &+ ptr. count
286
+ _writerSize = _writerSize &+ ptr. count
291
287
}
292
288
}
293
289
#endif
@@ -306,7 +302,7 @@ public struct ByteBuffer {
306
302
_storage. memory. advanced ( by: writerIndex &- size) ,
307
303
$0. baseAddress!,
308
304
size)
309
- self . _writerSize = self . _writerSize &+ size
305
+ _writerSize = _writerSize &+ size
310
306
}
311
307
}
312
308
@@ -324,7 +320,7 @@ public struct ByteBuffer {
324
320
_storage. memory. advanced ( by: writerIndex &- len) ,
325
321
$0. baseAddress!,
326
322
len)
327
- self . _writerSize = self . _writerSize &+ len
323
+ _writerSize = _writerSize &+ len
328
324
}
329
325
}
330
326
@@ -429,11 +425,9 @@ public struct ByteBuffer {
429
425
/// - position: the index of the object in the buffer
430
426
@inline ( __always)
431
427
public func read< T> ( def: T . Type , position: Int ) -> T {
432
- #if swift(>=5.7)
433
428
if allowReadingUnalignedBuffers {
434
429
return _storage. memory. advanced ( by: position) . loadUnaligned ( as: T . self)
435
430
}
436
- #endif
437
431
return _storage. memory. advanced ( by: position) . load ( as: T . self)
438
432
}
439
433
@@ -538,7 +532,8 @@ extension ByteBuffer: CustomDebugStringConvertible {
538
532
public var debugDescription : String {
539
533
"""
540
534
buffer located at: \( _storage. memory) , with capacity of \( _storage. capacity)
541
- { writerSize: \( _writerSize) , readerSize: \( reader) , writerIndex: \( writerIndex) }
535
+ { writerSize: \( _writerSize) , readerSize: \( reader) , writerIndex: \(
536
+ writerIndex) }
542
537
"""
543
538
}
544
539
}
0 commit comments