diff --git a/Sources/NIOCore/ByteBuffer-aux.swift b/Sources/NIOCore/ByteBuffer-aux.swift index 60e57dd300..47ef32c1a8 100644 --- a/Sources/NIOCore/ByteBuffer-aux.swift +++ b/Sources/NIOCore/ByteBuffer-aux.swift @@ -463,8 +463,12 @@ extension ByteBuffer { /// - Returns: The number of bytes read. @discardableResult @inlinable - public mutating func readWithUnsafeReadableBytes(_ body: (UnsafeRawBufferPointer) throws -> Int) rethrows -> Int { - let bytesRead = try self.withUnsafeReadableBytes({ try body($0) }) + public mutating func readWithUnsafeReadableBytes( + _ body: (UnsafeRawBufferPointer) throws(ErrorType) -> Int + ) throws(ErrorType) -> Int { + let bytesRead = try self.withUnsafeReadableBytes({ (ptr: UnsafeRawBufferPointer) throws(ErrorType) -> Int in + try body(ptr) + }) self._moveReaderIndex(forwardBy: bytesRead) return bytesRead } @@ -479,10 +483,12 @@ extension ByteBuffer { /// - Returns: The number of bytes read. @discardableResult @inlinable - public mutating func readWithUnsafeMutableReadableBytes( - _ body: (UnsafeMutableRawBufferPointer) throws -> Int - ) rethrows -> Int { - let bytesRead = try self.withUnsafeMutableReadableBytes({ try body($0) }) + public mutating func readWithUnsafeMutableReadableBytes( + _ body: (UnsafeMutableRawBufferPointer) throws(ErrorType) -> Int + ) throws(ErrorType) -> Int { + let bytesRead = try self.withUnsafeMutableReadableBytes({ + (ptr: UnsafeMutableRawBufferPointer) throws(ErrorType) -> Int in try body(ptr) + }) self._moveReaderIndex(forwardBy: bytesRead) return bytesRead } @@ -656,10 +662,12 @@ extension ByteBuffer { /// - body: The closure that will accept the yielded bytes and returns the number of bytes it processed along with some other value. /// - Returns: The value `body` returned in the second tuple component. @inlinable - public mutating func readWithUnsafeMutableReadableBytes( - _ body: (UnsafeMutableRawBufferPointer) throws -> (Int, T) - ) rethrows -> T { - let (bytesRead, ret) = try self.withUnsafeMutableReadableBytes({ try body($0) }) + public mutating func readWithUnsafeMutableReadableBytes( + _ body: (UnsafeMutableRawBufferPointer) throws(ErrorType) -> (Int, T) + ) throws(ErrorType) -> T { + let (bytesRead, ret) = try self.withUnsafeMutableReadableBytes({ + (ptr: UnsafeMutableRawBufferPointer) throws(ErrorType) -> (Int, T) in try body(ptr) + }) self._moveReaderIndex(forwardBy: bytesRead) return ret } @@ -673,10 +681,12 @@ extension ByteBuffer { /// - body: The closure that will accept the yielded bytes and returns the number of bytes it processed along with some other value. /// - Returns: The value `body` returned in the second tuple component. @inlinable - public mutating func readWithUnsafeReadableBytes( - _ body: (UnsafeRawBufferPointer) throws -> (Int, T) - ) rethrows -> T { - let (bytesRead, ret) = try self.withUnsafeReadableBytes({ try body($0) }) + public mutating func readWithUnsafeReadableBytes( + _ body: (UnsafeRawBufferPointer) throws(ErrorType) -> (Int, T) + ) throws(ErrorType) -> T { + let (bytesRead, ret) = try self.withUnsafeReadableBytes({ + (ptr: UnsafeRawBufferPointer) throws(ErrorType) -> (Int, T) in try body(ptr) + }) self._moveReaderIndex(forwardBy: bytesRead) return ret } diff --git a/Sources/NIOCore/ByteBuffer-binaryEncodedLengthPrefix.swift b/Sources/NIOCore/ByteBuffer-binaryEncodedLengthPrefix.swift index cf97570d0f..f18999cd39 100644 --- a/Sources/NIOCore/ByteBuffer-binaryEncodedLengthPrefix.swift +++ b/Sources/NIOCore/ByteBuffer-binaryEncodedLengthPrefix.swift @@ -116,10 +116,10 @@ extension ByteBuffer { /// - Returns: Number of total bytes written. This is the length of the written data + the number of bytes used to write the length before it. @discardableResult @inlinable - public mutating func writeLengthPrefixed( + public mutating func writeLengthPrefixed( strategy: Strategy, - writeData: (_ buffer: inout ByteBuffer) throws -> Int - ) rethrows -> Int { + writeData: (_ buffer: inout ByteBuffer) throws(ErrorType) -> Int + ) throws(ErrorType) -> Int { /// The index at which we write the length let lengthPrefixIndex = self.writerIndex /// The space which we reserve for writing the length diff --git a/Sources/NIOCore/ByteBuffer-core.swift b/Sources/NIOCore/ByteBuffer-core.swift index add0340b77..ce434877e7 100644 --- a/Sources/NIOCore/ByteBuffer-core.swift +++ b/Sources/NIOCore/ByteBuffer-core.swift @@ -682,9 +682,9 @@ public struct ByteBuffer { /// - body: The closure that will accept the yielded bytes. /// - Returns: The value returned by `body`. @inlinable - public mutating func withUnsafeMutableReadableBytes( - _ body: (UnsafeMutableRawBufferPointer) throws -> T - ) rethrows -> T { + public mutating func withUnsafeMutableReadableBytes( + _ body: (UnsafeMutableRawBufferPointer) throws(ErrorType) -> T + ) throws(ErrorType) -> T { self._copyStorageAndRebaseIfNeeded() // this is safe because we always know that readerIndex >= writerIndex let range = Range(uncheckedBounds: (lower: self.readerIndex, upper: self.writerIndex)) @@ -702,9 +702,9 @@ public struct ByteBuffer { /// - body: The closure that will accept the yielded bytes and return the number of bytes written. /// - Returns: The number of bytes written. @inlinable - public mutating func withUnsafeMutableWritableBytes( - _ body: (UnsafeMutableRawBufferPointer) throws -> T - ) rethrows -> T { + public mutating func withUnsafeMutableWritableBytes( + _ body: (UnsafeMutableRawBufferPointer) throws(ErrorType) -> T + ) throws(ErrorType) -> T { self._copyStorageAndRebaseIfNeeded() return try body(.init(rebasing: self._slicedStorageBuffer.dropFirst(self.writerIndex))) } @@ -719,14 +719,16 @@ public struct ByteBuffer { /// - Returns: The number of bytes written. @discardableResult @inlinable - public mutating func writeWithUnsafeMutableBytes( + public mutating func writeWithUnsafeMutableBytes( minimumWritableBytes: Int, - _ body: (UnsafeMutableRawBufferPointer) throws -> Int - ) rethrows -> Int { + _ body: (UnsafeMutableRawBufferPointer) throws(ErrorType) -> Int + ) throws(ErrorType) -> Int { if minimumWritableBytes > 0 { self.reserveCapacity(minimumWritableBytes: minimumWritableBytes) } - let bytesWritten = try self.withUnsafeMutableWritableBytes({ try body($0) }) + let bytesWritten = try self.withUnsafeMutableWritableBytes({ + (ptr: UnsafeMutableRawBufferPointer) throws(ErrorType) -> Int in try body(ptr) + }) self._moveWriterIndex(to: self._writerIndex + _toIndex(bytesWritten)) return bytesWritten } @@ -739,10 +741,13 @@ public struct ByteBuffer { ) @discardableResult @inlinable - public mutating func writeWithUnsafeMutableBytes( - _ body: (UnsafeMutableRawBufferPointer) throws -> Int - ) rethrows -> Int { - try self.writeWithUnsafeMutableBytes(minimumWritableBytes: 0, { try body($0) }) + public mutating func writeWithUnsafeMutableBytes( + _ body: (UnsafeMutableRawBufferPointer) throws(ErrorType) -> Int + ) throws(ErrorType) -> Int { + try self.writeWithUnsafeMutableBytes( + minimumWritableBytes: 0, + { (ptr: UnsafeMutableRawBufferPointer) throws(ErrorType) -> Int in try body(ptr) } + ) } /// This vends a pointer to the storage of the `ByteBuffer`. It's marked as _very unsafe_ because it might contain @@ -750,7 +755,9 @@ public struct ByteBuffer { /// /// - warning: Do not escape the pointer from the closure for later use. @inlinable - public func withVeryUnsafeBytes(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T { + public func withVeryUnsafeBytes( + _ body: (UnsafeRawBufferPointer) throws(ErrorType) -> T + ) throws(ErrorType) -> T { try body(.init(self._slicedStorageBuffer)) } @@ -759,9 +766,9 @@ public struct ByteBuffer { /// /// - warning: Do not escape the pointer from the closure for later use. @inlinable - public mutating func withVeryUnsafeMutableBytes( - _ body: (UnsafeMutableRawBufferPointer) throws -> T - ) rethrows -> T { + public mutating func withVeryUnsafeMutableBytes( + _ body: (UnsafeMutableRawBufferPointer) throws(ErrorType) -> T + ) throws(ErrorType) -> T { self._copyStorageAndRebaseIfNeeded() // this will trigger a CoW if necessary return try body(.init(self._slicedStorageBuffer)) } @@ -774,7 +781,9 @@ public struct ByteBuffer { /// - body: The closure that will accept the yielded bytes. /// - Returns: The value returned by `body`. @inlinable - public func withUnsafeReadableBytes(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T { + public func withUnsafeReadableBytes( + _ body: (UnsafeRawBufferPointer) throws(ErrorType) -> T + ) throws(ErrorType) -> T { // This is safe, writerIndex >= readerIndex let range = Range(uncheckedBounds: (lower: self.readerIndex, upper: self.writerIndex)) return try body(.init(rebasing: self._slicedStorageBuffer[range])) @@ -792,9 +801,9 @@ public struct ByteBuffer { /// - body: The closure that will accept the yielded bytes and the `storageManagement`. /// - Returns: The value returned by `body`. @inlinable - public func withUnsafeReadableBytesWithStorageManagement( - _ body: (UnsafeRawBufferPointer, Unmanaged) throws -> T - ) rethrows -> T { + public func withUnsafeReadableBytesWithStorageManagement( + _ body: (UnsafeRawBufferPointer, Unmanaged) throws(ErrorType) -> T + ) throws(ErrorType) -> T { let storageReference: Unmanaged = Unmanaged.passUnretained(self._storage) // This is safe, writerIndex >= readerIndex let range = Range(uncheckedBounds: (lower: self.readerIndex, upper: self.writerIndex)) @@ -803,9 +812,9 @@ public struct ByteBuffer { /// See `withUnsafeReadableBytesWithStorageManagement` and `withVeryUnsafeBytes`. @inlinable - public func withVeryUnsafeBytesWithStorageManagement( - _ body: (UnsafeRawBufferPointer, Unmanaged) throws -> T - ) rethrows -> T { + public func withVeryUnsafeBytesWithStorageManagement( + _ body: (UnsafeRawBufferPointer, Unmanaged) throws(ErrorType) -> T + ) throws(ErrorType) -> T { let storageReference: Unmanaged = Unmanaged.passUnretained(self._storage) return try body(.init(self._slicedStorageBuffer), storageReference) } @@ -1257,7 +1266,9 @@ extension ByteBuffer { /// - body: The modification operation to execute, with this `ByteBuffer` passed `inout` as an argument. /// - Returns: The return value of `body`. @inlinable - public mutating func modifyIfUniquelyOwned(_ body: (inout ByteBuffer) throws -> T) rethrows -> T? { + public mutating func modifyIfUniquelyOwned( + _ body: (inout ByteBuffer) throws(ErrorType) -> T + ) throws(ErrorType) -> T? { if isKnownUniquelyReferenced(&self._storage) { return try body(&self) } else { diff --git a/Sources/NIOCore/ByteBuffer-views.swift b/Sources/NIOCore/ByteBuffer-views.swift index 8563a301b8..b9ea5eee20 100644 --- a/Sources/NIOCore/ByteBuffer-views.swift +++ b/Sources/NIOCore/ByteBuffer-views.swift @@ -38,8 +38,10 @@ public struct ByteBufferView: RandomAccessCollection, Sendable { } @inlinable - public func withUnsafeBytes(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R { - try self._buffer.withVeryUnsafeBytes { ptr in + public func withUnsafeBytes( + _ body: (UnsafeRawBufferPointer) throws(ErrorType) -> R + ) throws(ErrorType) -> R { + try self._buffer.withVeryUnsafeBytes { (ptr: UnsafeRawBufferPointer) throws(ErrorType) -> R in try body( UnsafeRawBufferPointer( start: ptr.baseAddress!.advanced(by: self._range.lowerBound), @@ -98,8 +100,10 @@ public struct ByteBufferView: RandomAccessCollection, Sendable { } @inlinable - public func withContiguousStorageIfAvailable(_ body: (UnsafeBufferPointer) throws -> R) rethrows -> R? { - try self.withUnsafeBytes { bytes in + public func withContiguousStorageIfAvailable( + _ body: (UnsafeBufferPointer) throws(ErrorType) -> R + ) throws(ErrorType) -> R? { + try self.withUnsafeBytes { (bytes: UnsafeRawBufferPointer) throws(ErrorType) -> R in try body(bytes.bindMemory(to: UInt8.self)) } } diff --git a/Sources/NIOCore/Docs.docc/ByteBuffer-lengthPrefix.md b/Sources/NIOCore/Docs.docc/ByteBuffer-lengthPrefix.md index 57364f2857..79867b12bc 100644 --- a/Sources/NIOCore/Docs.docc/ByteBuffer-lengthPrefix.md +++ b/Sources/NIOCore/Docs.docc/ByteBuffer-lengthPrefix.md @@ -56,10 +56,10 @@ We decided to add the following API to ByteBuffer: /// - strategy: The strategy to use for encoding the length. /// - writeData: A closure that takes a buffer, writes some data to it, and returns the number of bytes written. /// - Returns: Number of total bytes written. This is the length of the written data + the number of bytes used to write the length before it. -public mutating func writeLengthPrefixed( +public mutating func writeLengthPrefixed( strategy: Strategy, - writeData: (_ buffer: inout ByteBuffer) throws -> Int -) rethrows -> Int + writeData: (_ buffer: inout ByteBuffer) throws(ErrorType) -> Int +) throws(ErrorType) -> Int ``` Users could use the function as follows: