@@ -15,8 +15,8 @@ import Swift
15
15
// ==== Task.init ------------------------------------------------
16
16
17
17
% for (METHOD_VARIANTS, ALL_AVAILABILITY, PARAMS) in [
18
- % (
19
- % [ # METHOD_VARIANT
18
+ % # ==== Without task executor, but available ever since 5.1
19
+ % ( [ # METHOD_VARIANT
20
20
% '',
21
21
% 'THROWING',
22
22
% ],
@@ -29,9 +29,7 @@ import Swift
29
29
% 'priority: TaskPriority? = nil',
30
30
% '@_inheritActorContext @_implicitSelfCapture operation: sending @escaping @isolated(any) () async throws -> Success',
31
31
% ]),
32
- % # ====
33
- % (
34
- % [ # METHOD_VARIANT
32
+ % ([ # METHOD_VARIANT
35
33
% 'DETACHED',
36
34
% 'DETACHED THROWING',
37
35
% ],
@@ -44,46 +42,83 @@ import Swift
44
42
% 'priority: TaskPriority? = nil',
45
43
% 'operation: sending @escaping @isolated(any) () async throws -> Success',
46
44
% ]),
45
+ % # ==== With task executor, but available only since 6.0
46
+ % ([ # METHOD_VARIANT
47
+ % '',
48
+ % 'THROWING',
49
+ % ],
50
+ % [ # ALL_AVAILABILITY
51
+ % '@_alwaysEmitIntoClient',
52
+ % '@available(SwiftStdlib 6.0, *)',
53
+ % ],
54
+ % [ # PARAMS
55
+ % 'name: String?',
56
+ % 'executorPreference taskExecutor: (any TaskExecutor)? = nil',
57
+ % 'priority: TaskPriority? = nil',
58
+ % '@_inheritActorContext @_implicitSelfCapture operation: sending @escaping @isolated(any) () async throws -> Success',
59
+ % ]),
60
+ % ([ # METHOD_VARIANT
61
+ % 'DETACHED',
62
+ % 'DETACHED THROWING',
63
+ % ],
64
+ % [
65
+ % '@_alwaysEmitIntoClient',
66
+ % '@available(SwiftStdlib 6.0, *)',
67
+ % ],
68
+ % [ # PARAMS
69
+ % 'name: String?',
70
+ % 'executorPreference taskExecutor: (any TaskExecutor)? = nil',
71
+ % 'priority: TaskPriority? = nil',
72
+ % 'operation: sending @escaping @isolated(any) () async throws -> Success',
73
+ % ]),
47
74
% ]:
48
75
% for METHOD_VARIANT in METHOD_VARIANTS:
49
76
50
77
% IS_DETACHED = 'DETACHED' in METHOD_VARIANT
51
78
% IS_THROWING = 'THROWING' in METHOD_VARIANT
79
+ % HAS_TASK_PRIORITY = any('priority:' in param for param in PARAMS)
80
+ % HAS_TASK_NAME = any('name:' in param for param in PARAMS)
81
+ % HAS_TASK_EXECUTOR = any('taskExecutor:' in param for param in PARAMS)
82
+ %
52
83
% if IS_THROWING:
53
84
% FAILURE_TYPE = 'Error'
54
85
% else:
55
86
% FAILURE_TYPE = 'Never'
56
87
% end
57
88
58
- %
59
- % def adjust_params_for_kind(params):
60
- % res = []
61
- % for p in params:
62
- % np = p
63
- % if not IS_THROWING:
64
- % np = np.replace("throws", "")
65
- % res.append(np)
66
- % return res
67
- %
68
- %
69
- % HAS_TASK_PRIORITY = any('priority:' in param for param in PARAMS)
70
- % HAS_TASK_NAME = any('name:' in param for param in PARAMS)
71
- % HAS_TASK_EXECUTOR = any('taskExecutor:' in param for param in PARAMS)
89
+ %{
72
90
73
- % if IS_DETACHED:
74
- % ARROW_RETURN_TYPE = f'-> Task<Success, {FAILURE_TYPE}>'
75
- % else:
76
- % ARROW_RETURN_TYPE = '' # init does not spell out return type
77
- % end
91
+ def adjust_params_for_kind(params):
92
+ res = []
93
+ for p in params:
94
+ np = p
95
+ if not IS_THROWING:
96
+ np = np.replace("throws", "")
97
+ res.append(np)
98
+ return res
78
99
79
- % if IS_DETACHED:
80
- % THE_FUNC = 'public static func detached'
81
- % else:
82
- % THE_FUNC = 'public init'
83
- % end
100
+ def adjust_availability(avails):
101
+ res = []
102
+ for av in avails:
103
+ adjusted = av
104
+ if HAS_TASK_EXECUTOR:
105
+ adjusted = adjusted.replace("SwiftStdlib 5.1", "SwiftStdlib 6.0")
106
+ res.append(adjusted)
107
+ return res
108
+
109
+ if IS_DETACHED:
110
+ ARROW_RETURN_TYPE = f'-> Task<Success, {FAILURE_TYPE}>'
111
+ else:
112
+ ARROW_RETURN_TYPE = '' # init does not spell out return type
113
+
114
+ if IS_DETACHED:
115
+ THE_FUNC = 'public static func detached'
116
+ else:
117
+ THE_FUNC = 'public init'
118
+
119
+ }%
84
120
85
121
% # ====================================================================================================================
86
- @available(SwiftStdlib 5.1, *)
87
122
extension Task where Failure == ${FAILURE_TYPE} {
88
123
89
124
% # --------------------------------------------------------------------------------------------------------------------
@@ -101,7 +136,7 @@ extension Task where Failure == ${FAILURE_TYPE} {
101
136
#elseif $Embedded
102
137
@discardableResult
103
138
@_alwaysEmitIntoClient
104
- @available(SwiftStdlib 5.1, *)
139
+ ${" \n".join(adjust_availability([' @available(SwiftStdlib 5.1, *)']))}
105
140
${THE_FUNC}(
106
141
${",\n ".join(adjust_params_for_kind(PARAMS))}
107
142
) ${ARROW_RETURN_TYPE}{
@@ -159,16 +194,18 @@ extension Task where Failure == ${FAILURE_TYPE} {
159
194
% if HAS_TASK_NAME:
160
195
/// - name: Human readable name of the task.
161
196
% end
162
- % if HAS_TASK_NAME:
163
- /// - taskExecutor: the preferred task executor for this task,
164
- /// and any child tasks created by it. Explicitly passing `nil` is
165
- /// interpreted as "no preference".
197
+ % if HAS_TASK_EXECUTOR:
198
+ /// - taskExecutor:
199
+ /// - taskExecutor: The task executor that the child task should be started on and keep using.
200
+ /// Explicitly passing `nil` as the executor preference is equivalent to no preference,
201
+ /// and effectively means to inherit the outer context's executor preference.
202
+ /// You can also pass the ``globalConcurrentExecutor`` global executor explicitly.
166
203
% end
167
204
/// - priority: The priority of the task.
168
205
/// - operation: The operation to perform.
169
206
///
170
207
/// - Returns: A reference to the task.
171
- ${"\n ".join(ALL_AVAILABILITY)}
208
+ ${"\n ".join(adjust_availability( ALL_AVAILABILITY) )}
172
209
@discardableResult
173
210
${THE_FUNC}(
174
211
${",\n ".join(adjust_params_for_kind(PARAMS))}
0 commit comments