diff --git a/swift/Sources/FlatBuffers/ByteBuffer.swift b/swift/Sources/FlatBuffers/ByteBuffer.swift index 92778f41151..7005461f80e 100644 --- a/swift/Sources/FlatBuffers/ByteBuffer.swift +++ b/swift/Sources/FlatBuffers/ByteBuffer.swift @@ -445,7 +445,9 @@ public struct ByteBuffer { /// - position: the index of the object in the buffer @inline(__always) public func read(def: T.Type, position: Int) -> T { - _storage.memory.advanced(by: position).assumingMemoryBound(to: T.self) + _storage.memory + .advanced(by: position) + .bindMemory(to: T.self, capacity: 1) .pointee } @@ -462,7 +464,7 @@ public struct ByteBuffer { index + count <= _storage.capacity, "Reading out of bounds is illegal") let start = _storage.memory.advanced(by: index) - .assumingMemoryBound(to: T.self) + .bindMemory(to: T.self, capacity: count) let array = UnsafeBufferPointer(start: start, count: count) return Array(array) } @@ -483,7 +485,7 @@ public struct ByteBuffer { index + count <= _storage.capacity, "Reading out of bounds is illegal") let start = _storage.memory.advanced(by: index) - .assumingMemoryBound(to: UInt8.self) + .bindMemory(to: UInt8.self, capacity: count) let bufprt = UnsafeBufferPointer(start: start, count: count) return String(bytes: Array(bufprt), encoding: type) } @@ -501,7 +503,7 @@ public struct ByteBuffer { index + count <= _storage.capacity, "Reading out of bounds is illegal") let start = _storage.memory.advanced(by: index) - .assumingMemoryBound(to: UInt8.self) + .bindMemory(to: UInt8.self, capacity: count) let bufprt = UnsafeBufferPointer(start: start, count: count) return String(cString: bufprt.baseAddress!) } diff --git a/swift/Sources/FlatBuffers/FlatBufferBuilder.swift b/swift/Sources/FlatBuffers/FlatBufferBuilder.swift index 95e9accdc9f..d914636314e 100644 --- a/swift/Sources/FlatBuffers/FlatBufferBuilder.swift +++ b/swift/Sources/FlatBuffers/FlatBufferBuilder.swift @@ -72,7 +72,10 @@ public struct FlatBufferBuilder { /// Note: This should be used with caution. public var fullSizedByteArray: [UInt8] { let ptr = UnsafeBufferPointer( - start: _bb.memory.assumingMemoryBound(to: UInt8.self), + start: _bb.memory.bindMemory( + to: UInt8.self, + capacity: _bb.capacity + ), count: _bb.capacity) return Array(ptr) } @@ -283,7 +286,7 @@ public struct FlatBufferBuilder { var isAlreadyAdded: Int? let vt2 = _bb.memory.advanced(by: _bb.writerIndex) - let len2 = vt2.load(fromByteOffset: 0, as: Int16.self) + let len2 = vt2.bindMemory(to: Int16.self, capacity: 1).pointee for index in stride(from: 0, to: _vtables.count, by: 1) { let position = _bb.capacity &- Int(_vtables[index])