@@ -40,11 +40,11 @@ public struct ByteBuffer {
40
40
41
41
/// This storage doesn't own the memory, therefore, we won't deallocate on deinit.
42
42
private let isOwned : Bool
43
+ /// Capacity of UInt8 the buffer can hold
44
+ private let capacity : Int
43
45
/// Retained blob of data that requires the storage to retain a pointer to.
44
46
@usableFromInline
45
47
var retainedBlob : Blob
46
- /// Capacity of UInt8 the buffer can hold
47
- var capacity : Int
48
48
49
49
@usableFromInline
50
50
init ( count: Int ) {
@@ -179,11 +179,11 @@ public struct ByteBuffer {
179
179
/// The size of the elements written to the buffer + their paddings
180
180
private var _readerIndex : Int = 0
181
181
/// Reader is the position of the current Writer Index (capacity - size)
182
- public var reader : Int { _storage . capacity &- _readerIndex }
182
+ public var reader : Int { capacity &- _readerIndex }
183
183
/// Current size of the buffer
184
184
public var size : UOffset { UOffset ( _readerIndex) }
185
185
/// Current capacity for the buffer
186
- public var capacity : Int { _storage . capacity }
186
+ public let capacity : Int
187
187
188
188
/// Constructor that creates a Flatbuffer object from an InternalByteBuffer
189
189
/// - Parameter
@@ -194,6 +194,7 @@ public struct ByteBuffer {
194
194
blob: . byteBuffer( byteBuffer) ,
195
195
capacity: byteBuffer. capacity)
196
196
_readerIndex = Int ( byteBuffer. size)
197
+ self . capacity = byteBuffer. capacity
197
198
}
198
199
199
200
/// Constructor that creates a Flatbuffer from unsafe memory region by copying
@@ -209,7 +210,8 @@ public struct ByteBuffer {
209
210
{
210
211
_storage = Storage ( count: capacity)
211
212
_storage. copy ( from: memory, count: capacity)
212
- _readerIndex = _storage. capacity
213
+ _readerIndex = capacity
214
+ self . capacity = capacity
213
215
}
214
216
215
217
/// Constructor that creates a Flatbuffer object from a UInt8
@@ -218,7 +220,8 @@ public struct ByteBuffer {
218
220
@inline ( __always)
219
221
public init ( bytes: [ UInt8 ] ) {
220
222
_storage = Storage ( blob: . array( bytes) , capacity: bytes. count)
221
- _readerIndex = _storage. capacity
223
+ _readerIndex = bytes. count
224
+ capacity = bytes. count
222
225
}
223
226
224
227
#if !os(WASI)
@@ -228,7 +231,8 @@ public struct ByteBuffer {
228
231
@inline ( __always)
229
232
public init ( data: Data ) {
230
233
_storage = Storage ( blob: . data( data) , capacity: data. count)
231
- _readerIndex = _storage. capacity
234
+ _readerIndex = data. count
235
+ capacity = data. count
232
236
}
233
237
234
238
/// Constructor that creates a Flatbuffer object from a ContiguousBytes
@@ -241,7 +245,8 @@ public struct ByteBuffer {
241
245
count: Int )
242
246
{
243
247
_storage = Storage ( blob: . bytes( contiguousBytes) , capacity: count)
244
- _readerIndex = _storage. capacity
248
+ _readerIndex = count
249
+ self . capacity = count
245
250
}
246
251
#endif
247
252
@@ -259,7 +264,8 @@ public struct ByteBuffer {
259
264
_storage = Storage (
260
265
blob: . pointer( memory) ,
261
266
capacity: capacity)
262
- _readerIndex = _storage. capacity
267
+ _readerIndex = capacity
268
+ self . capacity = capacity
263
269
}
264
270
265
271
/// Creates a copy of the existing flatbuffer, by copying it to a different memory.
@@ -275,6 +281,7 @@ public struct ByteBuffer {
275
281
{
276
282
_storage = Storage ( blob: blob, capacity: count)
277
283
_readerIndex = removeBytes
284
+ capacity = count
278
285
}
279
286
280
287
/// Write stores an object into the buffer directly or indirectly.
@@ -289,11 +296,11 @@ public struct ByteBuffer {
289
296
func write< T> ( value: T , index: Int , direct: Bool = false ) {
290
297
var index = index
291
298
if !direct {
292
- index = _storage . capacity &- index
299
+ index = capacity &- index
293
300
}
294
- assert ( index < _storage . capacity, " Write index is out of writing bound " )
301
+ assert ( index < capacity, " Write index is out of writing bound " )
295
302
assert ( index >= 0 , " Writer index should be above zero " )
296
- withUnsafePointer ( to: value) { ptr in
303
+ _ = withUnsafePointer ( to: value) { ptr in
297
304
_storage. withUnsafeRawPointer {
298
305
memcpy (
299
306
$0. advanced ( by: index) ,
@@ -325,7 +332,7 @@ public struct ByteBuffer {
325
332
count: Int ) -> [ T ]
326
333
{
327
334
assert (
328
- index + count <= _storage . capacity,
335
+ index + count <= capacity,
329
336
" Reading out of bounds is illegal " )
330
337
331
338
return _storage. readWithUnsafeRawPointer ( position: index) {
@@ -348,7 +355,7 @@ public struct ByteBuffer {
348
355
body: ( UnsafeRawBufferPointer ) throws -> T ) rethrows -> T
349
356
{
350
357
assert (
351
- index + count <= _storage . capacity,
358
+ index + count <= capacity,
352
359
" Reading out of bounds is illegal " )
353
360
return try _storage. readWithUnsafeRawPointer ( position: index) {
354
361
try body ( UnsafeRawBufferPointer ( start: $0, count: count) )
@@ -368,7 +375,7 @@ public struct ByteBuffer {
368
375
type: String . Encoding = . utf8) -> String ?
369
376
{
370
377
assert (
371
- index + count <= _storage . capacity,
378
+ index + count <= capacity,
372
379
" Reading out of bounds is illegal " )
373
380
return _storage. readWithUnsafeRawPointer ( position: index) {
374
381
let buf = UnsafeBufferPointer (
@@ -390,7 +397,7 @@ public struct ByteBuffer {
390
397
count: Int ) -> String ?
391
398
{
392
399
assert (
393
- index + count <= _storage . capacity,
400
+ index + count <= capacity,
394
401
" Reading out of bounds is illegal " )
395
402
return _storage. readWithUnsafeRawPointer ( position: index) {
396
403
String ( cString: $0. bindMemory ( to: UInt8 . self, capacity: count) )
@@ -404,11 +411,11 @@ public struct ByteBuffer {
404
411
public func duplicate( removing removeBytes: Int = 0 ) -> ByteBuffer {
405
412
assert ( removeBytes > 0 , " Can NOT remove negative bytes " )
406
413
assert (
407
- removeBytes < _storage . capacity,
414
+ removeBytes < capacity,
408
415
" Can NOT remove more bytes than the ones allocated " )
409
416
return ByteBuffer (
410
417
blob: _storage. retainedBlob,
411
- count: _storage . capacity,
418
+ count: capacity,
412
419
removing: _readerIndex &- removeBytes)
413
420
}
414
421
@@ -456,7 +463,7 @@ extension ByteBuffer: CustomDebugStringConvertible {
456
463
public var debugDescription : String {
457
464
"""
458
465
buffer located at: \( _storage. retainedBlob) ,
459
- with capacity of \( _storage . capacity) ,
466
+ with capacity of \( capacity) ,
460
467
{ writtenSize: \( _readerIndex) , readerSize: \( reader) ,
461
468
size: \( size) }
462
469
"""
0 commit comments