[Swift] Add allowReadingUnalignedBuffers to most ByteBuffer init methods (#8134)

This commit is contained in:
abandy
2023-11-20 17:52:19 -05:00
committed by GitHub
parent e1c3690a2a
commit 5a937f1ba1

View File

@@ -121,12 +121,17 @@ public struct ByteBuffer {
public let allowReadingUnalignedBuffers: Bool public let allowReadingUnalignedBuffers: Bool
/// Constructor that creates a Flatbuffer object from a UInt8 /// Constructor that creates a Flatbuffer object from a UInt8
/// - Parameter bytes: Array of UInt8 /// - Parameter
public init(bytes: [UInt8]) { /// - bytes: Array of UInt8
/// - allowReadingUnalignedBuffers: allow reading from unaligned buffer
public init(
bytes: [UInt8],
allowReadingUnalignedBuffers allowUnalignedBuffers: Bool = false)
{
var b = bytes var b = bytes
_storage = Storage(count: bytes.count, alignment: alignment) _storage = Storage(count: bytes.count, alignment: alignment)
_writerSize = _storage.capacity _writerSize = _storage.capacity
allowReadingUnalignedBuffers = false allowReadingUnalignedBuffers = allowUnalignedBuffers
b.withUnsafeMutableBytes { bufferPointer in b.withUnsafeMutableBytes { bufferPointer in
self._storage.copy(from: bufferPointer.baseAddress!, count: bytes.count) self._storage.copy(from: bufferPointer.baseAddress!, count: bytes.count)
} }
@@ -134,12 +139,17 @@ public struct ByteBuffer {
#if !os(WASI) #if !os(WASI)
/// Constructor that creates a Flatbuffer from the Swift Data type object /// Constructor that creates a Flatbuffer from the Swift Data type object
/// - Parameter data: Swift data Object /// - Parameter
public init(data: Data) { /// - data: Swift data Object
/// - allowReadingUnalignedBuffers: allow reading from unaligned buffer
public init(
data: Data,
allowReadingUnalignedBuffers allowUnalignedBuffers: Bool = false)
{
var b = data var b = data
_storage = Storage(count: data.count, alignment: alignment) _storage = Storage(count: data.count, alignment: alignment)
_writerSize = _storage.capacity _writerSize = _storage.capacity
allowReadingUnalignedBuffers = false allowReadingUnalignedBuffers = allowUnalignedBuffers
b.withUnsafeMutableBytes { bufferPointer in b.withUnsafeMutableBytes { bufferPointer in
self._storage.copy(from: bufferPointer.baseAddress!, count: data.count) self._storage.copy(from: bufferPointer.baseAddress!, count: data.count)
} }
@@ -147,7 +157,9 @@ public struct ByteBuffer {
#endif #endif
/// Constructor that creates a Flatbuffer instance with a size /// Constructor that creates a Flatbuffer instance with a size
/// - Parameter size: Length of the buffer /// - Parameter:
/// - size: Length of the buffer
/// - allowReadingUnalignedBuffers: allow reading from unaligned buffer
init(initialSize size: Int) { init(initialSize size: Int) {
let size = size.convertToPowerofTwo let size = size.convertToPowerofTwo
_storage = Storage(count: size, alignment: alignment) _storage = Storage(count: size, alignment: alignment)
@@ -160,13 +172,15 @@ public struct ByteBuffer {
/// - Parameters: /// - Parameters:
/// - contiguousBytes: Binary stripe to use as the buffer /// - contiguousBytes: Binary stripe to use as the buffer
/// - count: amount of readable bytes /// - count: amount of readable bytes
/// - allowReadingUnalignedBuffers: allow reading from unaligned buffer
public init<Bytes: ContiguousBytes>( public init<Bytes: ContiguousBytes>(
contiguousBytes: Bytes, contiguousBytes: Bytes,
count: Int) count: Int,
allowReadingUnalignedBuffers allowUnalignedBuffers: Bool = false)
{ {
_storage = Storage(count: count, alignment: alignment) _storage = Storage(count: count, alignment: alignment)
_writerSize = _storage.capacity _writerSize = _storage.capacity
allowReadingUnalignedBuffers = false allowReadingUnalignedBuffers = allowUnalignedBuffers
contiguousBytes.withUnsafeBytes { buf in contiguousBytes.withUnsafeBytes { buf in
_storage.copy(from: buf.baseAddress!, count: buf.count) _storage.copy(from: buf.baseAddress!, count: buf.count)
} }
@@ -174,8 +188,10 @@ public struct ByteBuffer {
#endif #endif
/// Constructor that creates a Flatbuffer from unsafe memory region without copying /// Constructor that creates a Flatbuffer from unsafe memory region without copying
/// - Parameter assumingMemoryBound: The unsafe memory region /// - Parameter:
/// - Parameter capacity: The size of the given memory region /// - assumingMemoryBound: The unsafe memory region
/// - capacity: The size of the given memory region
/// - allowReadingUnalignedBuffers: allow reading from unaligned buffer
public init( public init(
assumingMemoryBound memory: UnsafeMutableRawPointer, assumingMemoryBound memory: UnsafeMutableRawPointer,
capacity: Int, capacity: Int,
@@ -190,11 +206,16 @@ public struct ByteBuffer {
/// - Parameters: /// - Parameters:
/// - memory: Current memory of the buffer /// - memory: Current memory of the buffer
/// - count: count of bytes /// - count: count of bytes
init(memory: UnsafeMutableRawPointer, count: Int) { /// - allowReadingUnalignedBuffers: allow reading from unaligned buffer
init(
memory: UnsafeMutableRawPointer,
count: Int,
allowReadingUnalignedBuffers allowUnalignedBuffers: Bool = false)
{
_storage = Storage(count: count, alignment: alignment) _storage = Storage(count: count, alignment: alignment)
_storage.copy(from: memory, count: count) _storage.copy(from: memory, count: count)
_writerSize = _storage.capacity _writerSize = _storage.capacity
allowReadingUnalignedBuffers = false allowReadingUnalignedBuffers = allowUnalignedBuffers
} }
/// Creates a copy of the existing flatbuffer, by copying it to a different memory. /// Creates a copy of the existing flatbuffer, by copying it to a different memory.
@@ -202,15 +223,17 @@ public struct ByteBuffer {
/// - memory: Current memory of the buffer /// - memory: Current memory of the buffer
/// - count: count of bytes /// - count: count of bytes
/// - removeBytes: Removes a number of bytes from the current size /// - removeBytes: Removes a number of bytes from the current size
/// - allowReadingUnalignedBuffers: allow reading from unaligned buffer
init( init(
memory: UnsafeMutableRawPointer, memory: UnsafeMutableRawPointer,
count: Int, count: Int,
removing removeBytes: Int) removing removeBytes: Int,
allowReadingUnalignedBuffers allowUnalignedBuffers: Bool = false)
{ {
_storage = Storage(count: count, alignment: alignment) _storage = Storage(count: count, alignment: alignment)
_storage.copy(from: memory, count: count) _storage.copy(from: memory, count: count)
_writerSize = removeBytes _writerSize = removeBytes
allowReadingUnalignedBuffers = false allowReadingUnalignedBuffers = allowUnalignedBuffers
} }
/// Fills the buffer with padding by adding to the writersize /// Fills the buffer with padding by adding to the writersize