diff --git a/Sources/NIO/ByteBuffer-aux.swift b/Sources/NIO/ByteBuffer-aux.swift index da920232a..2461b511f 100644 --- a/Sources/NIO/ByteBuffer-aux.swift +++ b/Sources/NIO/ByteBuffer-aux.swift @@ -68,8 +68,7 @@ extension ByteBuffer { @discardableResult public mutating func set(string: String, at index: Int, encoding: String.Encoding = .utf8) -> Int? { if encoding == .utf8 { - let bytes = Array(string.utf8) - return self.set(bytes: UnsafeRawBufferPointer(start: bytes, count: bytes.count), at: index) + return self.set(bytes: string.utf8, at: index) } else { if let data = string.data(using: encoding) { return self.set(data: data, at: index) @@ -112,7 +111,7 @@ extension ByteBuffer { } @discardableResult - public mutating func write(bytes: UnsafeRawBufferPointer) -> Int { + public mutating func write(bytes: S) -> Int where S.Element == UInt8 { let written = set(bytes: bytes, at: self.writerIndex) self.moveWriterIndex(forwardBy: written) return written diff --git a/Sources/NIO/ByteBuffer-core.swift b/Sources/NIO/ByteBuffer-core.swift index 8f5d5035a..bf237d687 100644 --- a/Sources/NIO/ByteBuffer-core.swift +++ b/Sources/NIO/ByteBuffer-core.swift @@ -171,16 +171,21 @@ public struct ByteBuffer { deallocator: .custom { _, _ in storageRef.release() }) } - private mutating func set(bytes: UnsafeRawBufferPointer, at index: Index) -> Capacity { - let newEndIndex: Index = index + toIndex(bytes.count) + private mutating func set(bytes: S, at index: Index) -> Capacity where S.Element == UInt8 { + let newEndIndex: Index = index + toIndex(Int(bytes.count)) if !isKnownUniquelyReferenced(&self._storage) { let extraCapacity = newEndIndex > self._slice.upperBound ? newEndIndex - self._slice.upperBound : 0 self.copyStorageAndRebase(extraCapacity: extraCapacity) } self.ensureAvailableCapacity(Capacity(bytes.count), at: index) - self._storage.bytes.advanced(by: Int(self._slice.lowerBound + index)).copyBytes(from: bytes.baseAddress!, count: bytes.count) - return toCapacity(bytes.count) + let base = self._storage.bytes.advanced(by: Int(self._slice.lowerBound + index)).assumingMemoryBound(to: UInt8.self) + var idx = 0 + for b in bytes { + base[idx] = b + idx += 1 + } + return toCapacity(Int(bytes.count)) } // MARK: Public Core API @@ -295,7 +300,7 @@ extension ByteBuffer: CustomStringConvertible { /* change types to the user visible `Int` */ extension ByteBuffer { - public mutating func set(bytes: UnsafeRawBufferPointer, at index: Int) -> Int { + public mutating func set(bytes: S, at index: Int) -> Int where S.Element == UInt8 { return Int(self.set(bytes: bytes, at: toIndex(index))) } diff --git a/Tests/NIOTests/ByteBufferTest+XCTest.swift b/Tests/NIOTests/ByteBufferTest+XCTest.swift index 75007d4e6..0f633562c 100644 --- a/Tests/NIOTests/ByteBufferTest+XCTest.swift +++ b/Tests/NIOTests/ByteBufferTest+XCTest.swift @@ -85,6 +85,8 @@ extension ByteBufferTest { ("testWriteBuffer", testWriteBuffer), ("testMisalignedIntegerRead", testMisalignedIntegerRead), ("testSetAndWriteBytes", testSetAndWriteBytes), + ("testWriteABunchOfCollections", testWriteABunchOfCollections), + ("testSetABunchOfCollections", testSetABunchOfCollections), ] } } diff --git a/Tests/NIOTests/ByteBufferTest.swift b/Tests/NIOTests/ByteBufferTest.swift index 28b49c8b2..e047ddea3 100644 --- a/Tests/NIOTests/ByteBufferTest.swift +++ b/Tests/NIOTests/ByteBufferTest.swift @@ -736,4 +736,33 @@ class ByteBufferTest: XCTestCase { let actualString = String(bytes: actualData, encoding: .utf8) XCTAssertEqual(Array(repeating: str, count: 3).joined(), actualString) } + + func testWriteABunchOfCollections() throws { + let overallData = "0123456789abcdef".data(using: .utf8)! + buf.write(bytes: "0123".utf8) + "4567".withCString { ptr in + ptr.withMemoryRebound(to: UInt8.self, capacity: 4) { ptr in + _ = buf.write(bytes: UnsafeBufferPointer(start: ptr, count: 4)) + } + } + buf.write(bytes: Array("89ab".utf8)) + buf.write(bytes: "cdef".data(using: .utf8)!) + let actual = buf.data(at: 0, length: buf.readableBytes) + XCTAssertEqual(overallData, actual) + } + + func testSetABunchOfCollections() throws { + let overallData = "0123456789abcdef".data(using: .utf8)! + _ = buf.set(bytes: "0123".utf8, at: 0) + "4567".withCString { ptr in + ptr.withMemoryRebound(to: UInt8.self, capacity: 4) { ptr in + _ = buf.set(bytes: UnsafeBufferPointer(start: ptr, count: 4), at: 4) + } + } + _ = buf.set(bytes: Array("89ab".utf8), at: 8) + _ = buf.set(bytes: "cdef".data(using: .utf8)!, at: 12) + let actual = buf.data(at: 0, length: 16) + XCTAssertEqual(overallData, actual) + } + }