@@ -89,48 +89,22 @@ extension Task {
89
89
///
90
90
/// - Parameters:
91
91
/// - keyPath: key path to the `TaskLocalKey` to be used for lookup
92
- /// - value:
93
- /// - body:
92
+ /// - value: value to bind the task local to for the scope of `operation`
93
+ /// - operation: the operation to run with the task local value bound
94
94
/// - Returns: the value returned by the `body` function.
95
95
public static func withLocal< Key, BodyResult> (
96
96
_ keyPath: KeyPath < TaskLocalValues , Key > ,
97
97
boundTo value: Key . Value ,
98
- body : @escaping ( ) async -> BodyResult
99
- ) async -> BodyResult where Key: TaskLocalKey {
98
+ operation : ( ) async throws -> BodyResult
99
+ ) async rethrows -> BodyResult where Key: TaskLocalKey {
100
100
let task = Builtin . getCurrentAsyncTask ( )
101
101
102
102
_taskLocalValuePush ( task, keyType: Key . self, value: value)
103
-
104
- defer {
105
- _taskLocalValuePop ( task)
106
- }
103
+ defer { _taskLocalValuePop ( task) }
107
104
108
- return await body ( )
105
+ return try await operation ( )
109
106
}
110
107
111
- /// Bind the task local key to the given value for the scope of the `body` function.
112
- /// Any child tasks spawned within this scope will inherit the binding.
113
- ///
114
- /// - Parameters:
115
- /// - key:
116
- /// - value:
117
- /// - body:
118
- /// - Returns: the value returned by the `body` function, or throws.
119
- public static func withLocal< Key, BodyResult> (
120
- _ keyPath: KeyPath < TaskLocalValues , Key > ,
121
- boundTo value: Key . Value ,
122
- body: @escaping ( ) async throws -> BodyResult
123
- ) async throws -> BodyResult where Key: TaskLocalKey {
124
- let task = Builtin . getCurrentAsyncTask ( )
125
-
126
- _taskLocalValuePush ( task, keyType: Key . self, value: value)
127
-
128
- defer {
129
- _taskLocalValuePop ( task)
130
- }
131
-
132
- return try ! await body ( )
133
- }
134
108
}
135
109
136
110
// ==== ------------------------------------------------------------------------
0 commit comments