diff --git a/Sources/NIO/ByteBuffer.swift b/Sources/NIO/ByteBuffer.swift index 068e5e2ce..28c42a12b 100644 --- a/Sources/NIO/ByteBuffer.swift +++ b/Sources/NIO/ByteBuffer.swift @@ -330,6 +330,17 @@ public struct ByteBuffer : Equatable { // TODO: Comparable return nil } + @discardableResult + public mutating func set(bytes: UnsafeBufferPointer, at index: Int) -> Int? { + if expandIfNeeded(index: index, size: bytes.count) { + let idx = applyOffset(index) + self.backingData.replaceSubrange(idx.. Int? { if expandIfNeeded(index: index, size: data.count) { let idx = applyOffset(index) @@ -367,6 +378,16 @@ public struct ByteBuffer : Equatable { // TODO: Comparable // TODO: indexOf, bytesBefore, forEachByte, backing byte array access? // TODO: Generics to avoid this? + + @discardableResult + public mutating func write(staticString string: StaticString) -> Int? { + if let bytes = set(staticString: string, at: writerIndex) { + writerIndex += bytes + return bytes + } + return nil + } + @discardableResult public mutating func write(string: String) -> Int? { if let bytes = set(string: string, at: writerIndex) { @@ -376,6 +397,15 @@ public struct ByteBuffer : Equatable { // TODO: Comparable return nil } + @discardableResult + public mutating func set(staticString string: StaticString, at index: Int) -> Int? { + return string.withUTF8Buffer { buffer in + print("set buffer at \(index)") + return self.set(bytes: buffer, at: index) + } + } + + @discardableResult public mutating func set(string: String, at index: Int) -> Int? { let utf8 = string.utf8 let count = utf8.count diff --git a/Sources/NIO/ChannelOutboundInvoker.swift b/Sources/NIO/ChannelOutboundInvoker.swift index 4bb7e3e74..b49fd4545 100644 --- a/Sources/NIO/ChannelOutboundInvoker.swift +++ b/Sources/NIO/ChannelOutboundInvoker.swift @@ -25,7 +25,7 @@ public protocol ChannelOutboundInvoker { func connect(remote: SocketAddress) -> Future @discardableResult func connect(remote: SocketAddress, promise: Promise) -> Future - func write(data: Any) -> Future + @discardableResult func write(data: Any) -> Future @discardableResult func write(data: Any, promise: Promise) -> Future func flush() @@ -53,10 +53,12 @@ public extension ChannelOutboundInvoker { return connect(remote: remote, promise: newVoidPromise()) } + @discardableResult public func write(data: Any) -> Future { return write(data: data, promise: newVoidPromise()) } + @discardableResult public func writeAndFlush(data: Any) -> Future { return writeAndFlush(data: data, promise: newVoidPromise()) } diff --git a/Sources/NIO/ChannelPipeline.swift b/Sources/NIO/ChannelPipeline.swift index bcffe8e2e..80dd17f86 100644 --- a/Sources/NIO/ChannelPipeline.swift +++ b/Sources/NIO/ChannelPipeline.swift @@ -315,6 +315,7 @@ public class ChannelPipeline : ChannelInboundInvoker { } } + @discardableResult func write(data: Any, promise: Promise) -> Future { if eventLoop.inEventLoop { tail!.invokeWrite(data: data, promise: promise) @@ -326,6 +327,7 @@ public class ChannelPipeline : ChannelInboundInvoker { return promise.futureResult } + @discardableResult func writeAndFlush(data: Any, promise: Promise) -> Future { if eventLoop.inEventLoop { tail!.invokeWriteAndFlush(data: data, promise: promise) diff --git a/Tests/NIOTests/ByteBufferTest.swift b/Tests/NIOTests/ByteBufferTest.swift index 0a2e654cb..af4c6a84b 100644 --- a/Tests/NIOTests/ByteBufferTest.swift +++ b/Tests/NIOTests/ByteBufferTest.swift @@ -52,6 +52,30 @@ class ByteBufferTest: XCTestCase { buf.withReadPointer(body: { ptr, size in XCTAssertEqual(12, size) }) } + func testStaticStringReadTests() throws { + var allBytes = 0 + for testString in ["", "Hello world!", "πŸ‘", "πŸ‡¬πŸ‡§πŸ‡ΊπŸ‡ΈπŸ‡ͺπŸ‡Ί"] as [StaticString] { + buf.withReadPointer(body: { ptr, size in + XCTAssertEqual(0, size) + }) + + let bytes = buf.write(staticString: testString) + XCTAssertEqual(testString.utf8CodeUnitCount, bytes) + allBytes += bytes! + + let expected = testString.withUTF8Buffer { buf in + String(bytes: buf, encoding: .utf8) + } + buf.withReadPointer { ptr, size in + let actual = String(bytes: UnsafeBufferPointer(start: ptr, count: size), encoding: .utf8) + XCTAssertEqual(expected, actual) + } + let d = buf.read(length: testString.utf8CodeUnitCount) + XCTAssertNotNil(d) + XCTAssertEqual(expected, String(bytes: d!, encoding: .utf8)) + } + } + func testWriteStringMovesWriterIndex() { var buf = try! allocator.buffer(capacity: 1024) buf.write(string: "hello")