Skip to content

Commit

Permalink
Wrap concurrency code with @available(macOS etc) (apple#1809)
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler authored Apr 19, 2021
1 parent f7918a8 commit f6936ae
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions Sources/NIOAsyncAwaitDemo/AsyncChannelIO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import NIOHTTP1

#if compiler(>=5.5) // we cannot write this on one line with `&&` because Swift 5.0 doesn't like it...
#if compiler(>=5.5) && $AsyncAwait
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
struct AsyncChannelIO<Request, Response> {
let channel: Channel

Expand Down
10 changes: 8 additions & 2 deletions Sources/NIOAsyncAwaitDemo/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ defer {
try! group.syncShutdownGracefully()
}

@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
func makeHTTPChannel(host: String, port: Int) async throws -> AsyncChannelIO<HTTPRequestHead, NIOHTTPClientResponseFull> {
let channel = try await ClientBootstrap(group: group).connect(host: host, port: port).get()
try await channel.pipeline.addHTTPClientHandlers().get()
Expand All @@ -35,6 +36,7 @@ func makeHTTPChannel(host: String, port: Int) async throws -> AsyncChannelIO<HTT
return try await AsyncChannelIO<HTTPRequestHead, NIOHTTPClientResponseFull>(channel).start()
}

@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
func main() async {
do {
let channel = try await makeHTTPChannel(host: "httpbin.org", port: 80)
Expand Down Expand Up @@ -63,8 +65,12 @@ func main() async {

let dg = DispatchGroup()
dg.enter()
let task = detach {
await main()
if #available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) {
detach {
await main()
dg.leave()
}
} else {
dg.leave()
}
dg.wait()
Expand Down
5 changes: 5 additions & 0 deletions Sources/_NIOConcurrency/AsyncAwaitSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import NIO
#if compiler(>=5.5) && $AsyncAwait
import _Concurrency

@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
extension EventLoopFuture {
/// Get the value/error from an `EventLoopFuture` in an `async` context.
///
Expand All @@ -37,6 +38,7 @@ extension EventLoopFuture {
}
}

@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
extension EventLoopPromise {
/// Complete a future with the result (or error) of the `async` function `body`.
///
Expand All @@ -56,6 +58,7 @@ extension EventLoopPromise {
}
}

@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
extension Channel {
/// Shortcut for calling `write` and `flush`.
///
Expand All @@ -78,6 +81,7 @@ extension Channel {
}
}

@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
extension ChannelOutboundInvoker {
/// Register on an `EventLoop` and so have all its IO handled.
///
Expand Down Expand Up @@ -130,6 +134,7 @@ extension ChannelOutboundInvoker {
}
}

@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
extension ChannelPipeline {
public func addHandler(_ handler: ChannelHandler,
name: String? = nil,
Expand Down

0 comments on commit f6936ae

Please sign in to comment.