Skip to content

Commit f837e29

Browse files
authored
Add DiscardingTaskGroup and Stream polyfills for cluster support (#94)
1 parent 1b69101 commit f837e29

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the RediStack open source project
4+
//
5+
// Copyright (c) 2023 RediStack project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of RediStack project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
/// We use this protocol internally to abstract over TaskGroups. On Linux we can always use a `DiscardingTaskGroup`,
16+
/// but to support Swift 5.8 on macOS we need to fallback to the original TaskGroup.
17+
protocol DiscardingTaskGroupProtocol {
18+
mutating func addTask(priority: TaskPriority?, operation: @escaping @Sendable () async -> Void)
19+
}
20+
21+
extension DiscardingTaskGroupProtocol {
22+
mutating func addTask(_ operation: @escaping @Sendable () async -> Void) {
23+
self.addTask(priority: nil, operation: operation)
24+
}
25+
}
26+
27+
extension TaskGroup: DiscardingTaskGroupProtocol where ChildTaskResult == Void {}
28+
29+
#if swift(>=5.9) || (swift(>=5.8) && os(Linux))
30+
@available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *)
31+
extension DiscardingTaskGroup: DiscardingTaskGroupProtocol {}
32+
#endif
33+
34+
#if swift(<5.9)
35+
// This should be removed once we support Swift 5.9+ only
36+
extension AsyncStream {
37+
static func makeStream(
38+
of elementType: Element.Type = Element.self,
39+
bufferingPolicy limit: Continuation.BufferingPolicy = .unbounded
40+
) -> (stream: AsyncStream<Element>, continuation: AsyncStream<Element>.Continuation) {
41+
var continuation: AsyncStream<Element>.Continuation!
42+
let stream = AsyncStream<Element>(bufferingPolicy: limit) { continuation = $0 }
43+
return (stream: stream, continuation: continuation!)
44+
}
45+
}
46+
#endif

0 commit comments

Comments
 (0)