-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathasync_taskgroup_is_asyncsequence.swift
99 lines (78 loc) · 2.09 KB
/
async_taskgroup_is_asyncsequence.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// RUN: %target-run-simple-swift( -target %target-swift-5.1-abi-triple -parse-as-library) | %FileCheck %s --dump-input=always
// REQUIRES: executable_test
// REQUIRES: concurrency
// rdar://76038845
// REQUIRES: concurrency_runtime
// UNSUPPORTED: back_deployment_runtime
@available(SwiftStdlib 5.1, *)
func test_taskGroup_is_asyncSequence() async {
print(#function)
let sum = await withTaskGroup(of: Int.self, returning: Int.self) { group in
for n in 1...10 {
group.spawn {
print("add \(n)")
return n
}
}
var sum = 0
for await r in group { // here
print("next: \(r)")
sum += r
}
return sum
}
print("result: \(sum)")
}
@available(SwiftStdlib 5.1, *)
func test_throwingTaskGroup_is_asyncSequence() async throws {
print(#function)
let sum = try await withThrowingTaskGroup(of: Int.self, returning: Int.self) { group in
for n in 1...10 {
group.spawn {
print("add \(n)")
return n
}
}
var sum = 0
for try await r in group { // here
print("next: \(r)")
sum += r
}
return sum
}
print("result: \(sum)")
}
typealias LabelledTuple = (x: Int, y: Int)
@available(SwiftStdlib 5.1, *)
func test_asyncSequence_labelledTuples() async {
print(#function)
let sum = await withTaskGroup(of: LabelledTuple.self, returning: Int.self) { group in
for n in 1...10 {
group.spawn {
print("add (x: \(n), y: 1)")
return (x: n, y: 1)
}
}
var sum = 0
for await (x, y) in group { // here
print("next: (x:\(x), y:\(y))")
sum += x + y
}
return sum
}
print("result: \(sum)")
}
@available(SwiftStdlib 5.1, *)
@main struct Main {
static func main() async {
await test_taskGroup_is_asyncSequence()
// CHECK: test_taskGroup_is_asyncSequence()
// CHECK: result: 55
try! await test_throwingTaskGroup_is_asyncSequence()
// CHECK: test_throwingTaskGroup_is_asyncSequence()
// CHECK: result: 55
await test_asyncSequence_labelledTuples()
// CHECK: test_asyncSequence_labelledTuples()
// CHECK: result: 65
}
}