Skip to content

Commit

Permalink
More code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
FranzBusch committed Aug 30, 2022
1 parent 942998b commit ac921d4
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions Sources/NIOCore/AsyncSequences/NIOAsyncWriter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public protocol NIOAsyncWriterDelegate: Sendable {
/// - ``NIOAsyncWriter/finish(with:)`` is called.
///
/// - Note: This is guaranteed to be called _exactly_ once.
///
/// - Parameter failure: The failure that terminated the ``NIOAsyncWriter``. If the writer was terminated without an
/// error this value is `nil`.
func didTerminate(failure: Failure?)
}

Expand All @@ -53,6 +56,28 @@ public struct NIOAsyncWriterError: Error, Hashable {
@usableFromInline
let _code: _Code

@usableFromInline
var file: String

@usableFromInline
var line: Int

@inlinable
init(_code: _Code, file: String = #fileID, line: Int = #line) {
self._code = _code
self.file = file
self.line = line
}

public static func == (lhs: NIOAsyncWriterError, rhs: NIOAsyncWriterError) -> Bool {
return lhs._code == rhs._code
}

public func hash(into hasher: inout Hasher) {
hasher.combine(self._code)
}

/// Indicates that the ``NIOAsyncWriter`` has already finished and is not accepting any more writes.
public static let alreadyFinished: Self = .init(_code: .alreadyFinished)
}

Expand Down Expand Up @@ -514,11 +539,11 @@ extension NIOAsyncWriter {

if newWritability {
// We became writable again. This means we have to resume all the continuations
// and yield the values
// and yield the values.

// We are taking the whole array of suspended yields and allocate a new empty one
// We are taking the whole array of suspended yields and allocate a new empty one.
// As a performance optimization we could always keep two arrays and switch between
// them but I don't think this is the performance critical part
// them but I don't think this is the performance critical part.
let yields = suspendedYields

self._state = .streaming(
Expand Down Expand Up @@ -607,7 +632,7 @@ extension NIOAsyncWriter {

case .finished:
// We are already finished and still tried to write something
return .throwError(NIOAsyncWriterError.alreadyFinished)
return .throwError(NIOAsyncWriterError(_code: .alreadyFinished))

case .modifying:
preconditionFailure("Invalid state")
Expand Down Expand Up @@ -696,6 +721,7 @@ extension NIOAsyncWriter {
)

} else {
self._state = .modifying
// There is no suspended yield. This can mean that we either already yielded
// or that the call to `yield` is coming afterwards. We need to store
// the ID here. However, if the yield already happened we will never remove the
Expand Down

0 comments on commit ac921d4

Please sign in to comment.