From 5d20621e1fae0f6eb13f8d7282f8c79e89608f1d Mon Sep 17 00:00:00 2001 From: Franz Busch Date: Mon, 19 Feb 2024 15:02:56 +0000 Subject: [PATCH] George review --- Sources/NIOCore/EventLoop.swift | 2 +- Sources/NIOCore/EventLoopFuture.swift | 73 +++++---------------------- 2 files changed, 14 insertions(+), 61 deletions(-) diff --git a/Sources/NIOCore/EventLoop.swift b/Sources/NIOCore/EventLoop.swift index b2fe4c8f14..4c9d6c6018 100644 --- a/Sources/NIOCore/EventLoop.swift +++ b/Sources/NIOCore/EventLoop.swift @@ -736,7 +736,7 @@ extension EventLoop { /// - returns: An `EventLoopFuture` identical to the `EventLoopFuture` returned from `task`. @inlinable @preconcurrency - public func flatSubmit(_ task: @escaping @Sendable () -> EventLoopFuture) -> EventLoopFuture { // TODO: This should take a closure that returns fresh + public func flatSubmit(_ task: @escaping @Sendable () -> EventLoopFuture) -> EventLoopFuture { self.submit(task).flatMap { $0 } } diff --git a/Sources/NIOCore/EventLoopFuture.swift b/Sources/NIOCore/EventLoopFuture.swift index 55e1756ceb..4e58738844 100644 --- a/Sources/NIOCore/EventLoopFuture.swift +++ b/Sources/NIOCore/EventLoopFuture.swift @@ -184,17 +184,6 @@ public struct EventLoopPromise { self._resolve(value: .success(value)) } - /// Deliver a successful result to the associated `EventLoopFuture` object. - /// - /// - Note: The call to this method must happen on the same event loop as this promise was created from. - /// - /// - parameters: - /// - eventLoopBoundValue: The successful result of the operation. - @inlinable - public func succeed(eventLoopBoundValue: Value) { - self._resolve(eventLoopBoundResult: .success(eventLoopBoundValue)) - } - /// Deliver an error to the associated `EventLoopFuture` object. /// /// - parameters: @@ -247,27 +236,6 @@ public struct EventLoopPromise { self._resolve(value: result) } - /// Complete the promise with the passed in `Result`. - /// - /// This method is equivalent to invoking: - /// ``` - /// switch result { - /// case .success(let value): - /// promise.succeed(value) - /// case .failure(let error): - /// promise.fail(error) - /// } - /// ``` - /// - /// - Note: The call to this method must happen on the same event loop as this promise was created from. - /// - /// - parameters: - /// - result: The result which will be used to succeed or fail this promise. - @inlinable - public func completeWith(eventLoopBoundResult: Result) { - self._resolve(eventLoopBoundResult: eventLoopBoundResult) - } - /// Fire the associated `EventLoopFuture` on the appropriate event loop. /// /// This method provides the primary difference between the `EventLoopPromise` and most @@ -287,23 +255,6 @@ public struct EventLoopPromise { } } - /// Fire the associated `EventLoopFuture` on the appropriate event loop. - /// - /// This method provides the primary difference between the `EventLoopPromise` and most - /// other `Promise` implementations: specifically, all callbacks fire on the `EventLoop` - /// that was used to create the promise. - /// - /// - Note: The call to this method must happen on the same event loop as this promise was created from. - /// - /// - parameters: - /// - value: The value to fire the future with. - @inlinable - internal func _resolve(eventLoopBoundResult: Result) { - self.futureResult.eventLoop.assertInEventLoop() - - self._setValue(value: eventLoopBoundResult)._run() - } - /// Set the future result and get the associated callbacks. /// /// - parameters: @@ -1435,17 +1386,17 @@ extension EventLoopFuture { processResult(index, .success(())) case .failure(let error): processResult(index, .failure(error)) - } - if case .failure = result { - return // Once the promise is failed, future results do not need to be processed. + return } } else { // We have to map to `Void` here to avoid sharing the potentially non-Sendable // value across event loops. - future - .map { _ in () } - .hop(to: eventLoop) - .whenComplete { result in processResult(index, result) } + future.whenComplete { result in + let voidResult = result.map { _ in } + future.eventLoop.execute { + processResult(index, voidResult) + } + } } } } @@ -1661,10 +1612,12 @@ extension EventLoopFuture { } else { // We have to map to `Void` here to avoid sharing the potentially non-Sendable // value across event loops. - future - .map { _ in () } - .hop(to: eventLoop) - .whenComplete { result in processResult(index, result) } + future.whenComplete { result in + let voidResult = result.map { _ in } + future.eventLoop.execute { + processResult(index, voidResult) + } + } } } }