mirror of
https://github.com/apple/swift-nio
synced 2026-11-28 05:14:23 +00:00
Rename the waitForFinalResult() methods and provide an ELF convenience (#2483)
# Motivation The name `waitForFinalResult()` is bad in a couple of ways. First, we normally use `wait` for blocking operations. Secondly, `finalResult` is also just strange because the user only wants to know the _result_ and doesn't even know that there might be intermediates. # Modification This PR renames the `waitForFinalResult()` method to be a property on `NIOProtocolNegotiationResult`. Additionally, it provides a conditional extension on `ELF` to avoid a `get()`. # Result Better naming for our async interfaces
This commit is contained in:
1 parent
b25bc5838f
commit
df49d6bde4
2 files changed
+54
-31
No files matched your search
@@ -354,26 +354,49 @@ public struct NIOProtocolNegotiationResult<NegotiationResult: Sendable> {
|
||||
case deferredResult(EventLoopFuture<NIOProtocolNegotiationResult<NegotiationResult>>)
|
||||
}
|
||||
|
||||
private let result: Result
|
||||
private let _result: Result
|
||||
|
||||
public init(result: NegotiationResult) {
|
||||
self.result = .finished(result)
|
||||
}
|
||||
|
||||
public init(deferredResult: EventLoopFuture<NIOProtocolNegotiationResult<NegotiationResult>>) {
|
||||
self.result = .deferredResult(deferredResult)
|
||||
}
|
||||
|
||||
/// Waits for the final protocol negotiation result and returns the value.
|
||||
/// The final result of protocol negotiation.
|
||||
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
|
||||
public func waitForFinalResult() async throws -> NegotiationResult {
|
||||
switch self.result {
|
||||
case .finished(let negotiationResult):
|
||||
return negotiationResult
|
||||
case .deferredResult(let eventLoopFuture):
|
||||
return try await eventLoopFuture.flatMap { $0.result.resolve(on: eventLoopFuture.eventLoop) }.get()
|
||||
var result: NegotiationResult {
|
||||
get async throws {
|
||||
switch self._result {
|
||||
case .finished(let negotiationResult):
|
||||
return negotiationResult
|
||||
case .deferredResult(let eventLoopFuture):
|
||||
return try await eventLoopFuture.flatMap { $0._result.resolve(on: eventLoopFuture.eventLoop) }.get()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Intializes a new ``NIOProtocolNegotiationResult`` with a final result.
|
||||
///
|
||||
/// - Parameter result: The final result of protocol negotiation.
|
||||
public init(result: NegotiationResult) {
|
||||
self._result = .finished(result)
|
||||
}
|
||||
|
||||
/// Intializes a new ``NIOProtocolNegotiationResult`` with a deferred result.
|
||||
///
|
||||
/// - Parameter deferredResult: The deferred result.
|
||||
public init(deferredResult: EventLoopFuture<NIOProtocolNegotiationResult<NegotiationResult>>) {
|
||||
self._result = .deferredResult(deferredResult)
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
|
||||
@available(*, deprecated, renamed: "getResult")
|
||||
public func waitForFinalResult() async throws -> NegotiationResult {
|
||||
try await self.result
|
||||
}
|
||||
}
|
||||
|
||||
extension EventLoopFuture {
|
||||
/// Get the result/error from the protocol negotiation.
|
||||
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
|
||||
@_spi(AsyncChannel)
|
||||
public func getResult<NegotiationResult: Sendable>() async throws -> NegotiationResult where Value == NIOProtocolNegotiationResult<NegotiationResult> {
|
||||
try await self.get().result
|
||||
}
|
||||
}
|
||||
|
||||
@_spi(AsyncChannel)
|
||||
@@ -389,7 +412,7 @@ extension NIOProtocolNegotiationResult.Result {
|
||||
|
||||
case .deferredResult(let future):
|
||||
return future.flatMap { result in
|
||||
return resolve(on: eventLoop, result: result.result)
|
||||
return resolve(on: eventLoop, result: result._result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -282,7 +282,7 @@ final class AsyncChannelBootstrapTests: XCTestCase {
|
||||
try await withThrowingTaskGroup(of: Void.self) { group in
|
||||
for try await negotiationResult in channel.inboundStream {
|
||||
group.addTask {
|
||||
switch try await negotiationResult.get().waitForFinalResult() {
|
||||
switch try await negotiationResult.getResult() {
|
||||
case .string(let channel):
|
||||
for try await value in channel.inboundStream {
|
||||
continuation.yield(.string(value))
|
||||
@@ -302,7 +302,7 @@ final class AsyncChannelBootstrapTests: XCTestCase {
|
||||
port: channel.channel.localAddress!.port!,
|
||||
proposedALPN: .string
|
||||
)
|
||||
let stringNegotiationResult = try await stringNegotiationResultFuture.get().waitForFinalResult()
|
||||
let stringNegotiationResult = try await stringNegotiationResultFuture.getResult()
|
||||
switch stringNegotiationResult {
|
||||
case .string(let stringChannel):
|
||||
// This is the actual content
|
||||
@@ -317,7 +317,7 @@ final class AsyncChannelBootstrapTests: XCTestCase {
|
||||
port: channel.channel.localAddress!.port!,
|
||||
proposedALPN: .byte
|
||||
)
|
||||
let byteNegotiationResult = try await byteNegotiationResultFuture.get().waitForFinalResult()
|
||||
let byteNegotiationResult = try await byteNegotiationResultFuture.getResult()
|
||||
switch byteNegotiationResult {
|
||||
case .string:
|
||||
preconditionFailure()
|
||||
@@ -356,7 +356,7 @@ final class AsyncChannelBootstrapTests: XCTestCase {
|
||||
try await withThrowingTaskGroup(of: Void.self) { group in
|
||||
for try await negotiationResult in channel.inboundStream {
|
||||
group.addTask {
|
||||
switch try await negotiationResult.get().waitForFinalResult() {
|
||||
switch try await negotiationResult.getResult() {
|
||||
case .string(let channel):
|
||||
for try await value in channel.inboundStream {
|
||||
continuation.yield(.string(value))
|
||||
@@ -377,7 +377,7 @@ final class AsyncChannelBootstrapTests: XCTestCase {
|
||||
proposedOuterALPN: .string,
|
||||
proposedInnerALPN: .string
|
||||
)
|
||||
switch try await stringStringNegotiationResult.get().waitForFinalResult() {
|
||||
switch try await stringStringNegotiationResult.getResult() {
|
||||
case .string(let stringChannel):
|
||||
// This is the actual content
|
||||
try await stringChannel.outboundWriter.write("hello")
|
||||
@@ -392,7 +392,7 @@ final class AsyncChannelBootstrapTests: XCTestCase {
|
||||
proposedOuterALPN: .byte,
|
||||
proposedInnerALPN: .string
|
||||
)
|
||||
switch try await byteStringNegotiationResult.get().waitForFinalResult() {
|
||||
switch try await byteStringNegotiationResult.getResult() {
|
||||
case .string(let stringChannel):
|
||||
// This is the actual content
|
||||
try await stringChannel.outboundWriter.write("hello")
|
||||
@@ -407,7 +407,7 @@ final class AsyncChannelBootstrapTests: XCTestCase {
|
||||
proposedOuterALPN: .byte,
|
||||
proposedInnerALPN: .byte
|
||||
)
|
||||
switch try await byteByteNegotiationResult.get().waitForFinalResult() {
|
||||
switch try await byteByteNegotiationResult.getResult() {
|
||||
case .string:
|
||||
preconditionFailure()
|
||||
case .byte(let byteChannel):
|
||||
@@ -422,7 +422,7 @@ final class AsyncChannelBootstrapTests: XCTestCase {
|
||||
proposedOuterALPN: .string,
|
||||
proposedInnerALPN: .byte
|
||||
)
|
||||
switch try await stringByteNegotiationResult.get().waitForFinalResult() {
|
||||
switch try await stringByteNegotiationResult.getResult() {
|
||||
case .string:
|
||||
preconditionFailure()
|
||||
case .byte(let byteChannel):
|
||||
@@ -485,7 +485,7 @@ final class AsyncChannelBootstrapTests: XCTestCase {
|
||||
try await withThrowingTaskGroup(of: Void.self) { group in
|
||||
for try await negotiationResult in channel.inboundStream {
|
||||
group.addTask {
|
||||
switch try await negotiationResult.get().waitForFinalResult() {
|
||||
switch try await negotiationResult.getResult() {
|
||||
case .string(let channel):
|
||||
for try await value in channel.inboundStream {
|
||||
continuation.yield(.string(value))
|
||||
@@ -506,7 +506,7 @@ final class AsyncChannelBootstrapTests: XCTestCase {
|
||||
proposedALPN: .unknown
|
||||
)
|
||||
await XCTAssertThrowsError(
|
||||
try await failedProtocolNegotiation.get().waitForFinalResult()
|
||||
try await failedProtocolNegotiation.getResult()
|
||||
)
|
||||
|
||||
// Let's check that we can still open a new connection
|
||||
@@ -515,7 +515,7 @@ final class AsyncChannelBootstrapTests: XCTestCase {
|
||||
port: channel.channel.localAddress!.port!,
|
||||
proposedALPN: .string
|
||||
)
|
||||
switch try await stringNegotiationResult.get().waitForFinalResult() {
|
||||
switch try await stringNegotiationResult.getResult() {
|
||||
case .string(let stringChannel):
|
||||
// This is the actual content
|
||||
try await stringChannel.outboundWriter.write("hello")
|
||||
@@ -599,7 +599,7 @@ final class AsyncChannelBootstrapTests: XCTestCase {
|
||||
let firstNegotiationResult = try await group.next()
|
||||
let secondNegotiationResult = try await group.next()
|
||||
|
||||
switch (try await firstNegotiationResult?.get().waitForFinalResult(), try await secondNegotiationResult?.get().waitForFinalResult()) {
|
||||
switch (try await firstNegotiationResult?.getResult(), try await secondNegotiationResult?.getResult()) {
|
||||
case (.string(let firstChannel), .string(let secondChannel)):
|
||||
var firstInboundIterator = firstChannel.inboundStream.makeAsyncIterator()
|
||||
var secondInboundIterator = secondChannel.inboundStream.makeAsyncIterator()
|
||||
@@ -682,7 +682,7 @@ final class AsyncChannelBootstrapTests: XCTestCase {
|
||||
|
||||
try toChannel.writeBytes(.init(string: "alpn:string\nHello\n"))
|
||||
let negotiationResult = try await group.next()
|
||||
switch try await negotiationResult?.get().waitForFinalResult() {
|
||||
switch try await negotiationResult?.getResult() {
|
||||
case .string(let channel):
|
||||
var inboundIterator = channel.inboundStream.makeAsyncIterator()
|
||||
do {
|
||||
@@ -753,7 +753,7 @@ final class AsyncChannelBootstrapTests: XCTestCase {
|
||||
let firstNegotiationResult = try await group.next()
|
||||
let secondNegotiationResult = try await group.next()
|
||||
|
||||
switch (try await firstNegotiationResult?.get().waitForFinalResult(), try await secondNegotiationResult?.get().waitForFinalResult()) {
|
||||
switch (try await firstNegotiationResult?.getResult(), try await secondNegotiationResult?.getResult()) {
|
||||
case (.string(let firstChannel), .string(let secondChannel)):
|
||||
var firstInboundIterator = firstChannel.inboundStream.makeAsyncIterator()
|
||||
var secondInboundIterator = secondChannel.inboundStream.makeAsyncIterator()
|
||||
|
||||
Reference in new issue
Block a user