From 98c4af8109a867febdcbeadb04d2bb3b3f2d54e1 Mon Sep 17 00:00:00 2001 From: George Barnett Date: Fri, 18 Dec 2020 16:16:06 +0000 Subject: [PATCH] Make a few EventLoopFuture functions inlinable (#1708) Motivation: Some of the generic EventLoopFuture functions weren't inlinable. Modifications: - Make them @inlinable - Fix typo in name of 'DispathQueue+WithFuture.swift' Result: More specialization. Co-authored-by: Cory Benfield --- ...e.swift => DispatchQueue+WithFuture.swift} | 0 Sources/NIO/EventLoop.swift | 4 +++- Sources/NIO/EventLoopFuture.swift | 19 ++++++++++++++----- 3 files changed, 17 insertions(+), 6 deletions(-) rename Sources/NIO/{DispathQueue+WithFuture.swift => DispatchQueue+WithFuture.swift} (100%) diff --git a/Sources/NIO/DispathQueue+WithFuture.swift b/Sources/NIO/DispatchQueue+WithFuture.swift similarity index 100% rename from Sources/NIO/DispathQueue+WithFuture.swift rename to Sources/NIO/DispatchQueue+WithFuture.swift diff --git a/Sources/NIO/EventLoop.swift b/Sources/NIO/EventLoop.swift index 530e13e66..3776575d0 100644 --- a/Sources/NIO/EventLoop.swift +++ b/Sources/NIO/EventLoop.swift @@ -515,7 +515,7 @@ extension EventLoop { /// /// - parameters: /// - task: The asynchronous task to run. As with everything that runs on the `EventLoop`, it must not block. - /// - returns: An `EventLoopFuture` identical to the `EventLooopFuture` returned from `task`. + /// - returns: An `EventLoopFuture` identical to the `EventLoopFuture` returned from `task`. @inlinable public func flatSubmit(_ task: @escaping () -> EventLoopFuture) -> EventLoopFuture { return self.submit(task).flatMap { $0 } @@ -530,6 +530,7 @@ extension EventLoop { /// /// - note: You can only cancel a task before it has started executing. @discardableResult + @inlinable public func flatScheduleTask(deadline: NIODeadline, file: StaticString = #file, line: UInt = #line, @@ -550,6 +551,7 @@ extension EventLoop { /// /// - note: You can only cancel a task before it has started executing. @discardableResult + @inlinable public func flatScheduleTask(in delay: TimeAmount, file: StaticString = #file, line: UInt = #line, diff --git a/Sources/NIO/EventLoopFuture.swift b/Sources/NIO/EventLoopFuture.swift index 63d0b5a1b..de5e8c863 100644 --- a/Sources/NIO/EventLoopFuture.swift +++ b/Sources/NIO/EventLoopFuture.swift @@ -113,7 +113,11 @@ internal struct CallbackList { } /// Internal error for operations that return results that were not replaced -private struct OperationPlaceholderError: Error { } +@usableFromInline +internal struct OperationPlaceholderError: Error { + @usableFromInline + internal init() {} +} /// A promise to provide a result later. /// @@ -891,6 +895,7 @@ extension EventLoopFuture { /// /// - returns: The value of the `EventLoopFuture` when it completes. /// - throws: The error value of the `EventLoopFuture` if it errors. + @inlinable public func wait(file: StaticString = #file, line: UInt = #line) throws -> Value { if !(self.eventLoop is EmbeddedEventLoop) { let explainer: () -> String = { """ @@ -1176,6 +1181,7 @@ extension EventLoopFuture { /// - futures: An array of homogenous `EventLoopFuture`s to wait for. /// - on: The `EventLoop` on which the new `EventLoopFuture` callbacks will execute on. /// - Returns: A new `EventLoopFuture` that succeeds after all futures complete. + @inlinable public static func andAllComplete(_ futures: [EventLoopFuture], on eventLoop: EventLoop) -> EventLoopFuture { let promise = eventLoop.makePromise(of: Void.self) @@ -1201,6 +1207,7 @@ extension EventLoopFuture { /// - futures: An array of homogenous `EventLoopFuture`s to gather results from. /// - on: The `EventLoop` on which the new `EventLoopFuture` callbacks will fire. /// - Returns: A new `EventLoopFuture` with all the results of the provided futures. + @inlinable public static func whenAllComplete(_ futures: [EventLoopFuture], on eventLoop: EventLoop) -> EventLoopFuture<[Result]> { let promise = eventLoop.makePromise(of: Void.self) @@ -1233,10 +1240,11 @@ extension EventLoopFuture { /// they complete. The `onResult` will receive the index of the future that fulfilled the provided `Result`. /// /// Once all the futures have completed, the provided promise will succeed. - private static func _reduceCompletions0(_ promise: EventLoopPromise, - _ futures: [EventLoopFuture], - _ eventLoop: EventLoop, - onResult: @escaping (Int, Result) -> Void) { + @inlinable + internal static func _reduceCompletions0(_ promise: EventLoopPromise, + _ futures: [EventLoopFuture], + _ eventLoop: EventLoop, + onResult: @escaping (Int, Result) -> Void) { eventLoop.assertInEventLoop() var remainingCount = futures.count @@ -1317,6 +1325,7 @@ extension EventLoopFuture { /// - parameters: /// - callback: the callback that is called when the `EventLoopFuture` is fulfilled. /// - returns: the current `EventLoopFuture` + @inlinable public func always(_ callback: @escaping (Result) -> Void) -> EventLoopFuture { self.whenComplete { result in callback(result) } return self