13
13
//
14
14
//===----------------------------------------------------------------------===//
15
15
16
- #if swift(>=5.5) && canImport(_Concurrency)
17
- public typealias _ServiceContext_Sendable = Swift . Sendable
18
- #else
19
- public typealias _ServiceContext_Sendable = Any
20
- #endif
21
-
22
16
/// A `ServiceContext` is a heterogeneous storage type with value semantics for keyed values in a type-safe fashion.
23
17
///
24
18
/// Its values are uniquely identified via ``ServiceContextKey``s (by type identity). These keys also dictate the type of
@@ -71,8 +65,8 @@ public typealias _ServiceContext_Sendable = Any
71
65
/// `ServiceContext` does not expose more functions on purpose to prevent abuse and treating it as too much of an
72
66
/// arbitrary value smuggling container, but only make it convenient for tracing and instrumentation systems which need
73
67
/// to access either specific or all items carried inside a baggage.
74
- public struct ServiceContext : _ServiceContext_Sendable {
75
- private var _storage = [ AnyServiceContextKey: _ServiceContext_Sendable ] ( )
68
+ public struct ServiceContext : Sendable {
69
+ private var _storage = [ AnyServiceContextKey: Sendable ] ( )
76
70
77
71
/// Internal on purpose, please use ``ServiceContext/TODO(_:function:file:line:)`` or ``ServiceContext/topLevel`` to create an "empty" baggage,
78
72
/// which carries more meaning to other developers why an empty baggage was used.
@@ -157,7 +151,7 @@ extension ServiceContext {
157
151
158
152
/// Carried automatically by a "to do" baggage.
159
153
/// It can be used to track where a baggage originated and which "to do" baggage must be fixed into a real one to avoid this.
160
- public struct TODOLocation : _ServiceContext_Sendable {
154
+ public struct TODOLocation : Sendable {
161
155
/// Source file location where the to-do ``ServiceContext`` was created
162
156
public let file : String
163
157
/// Source line location where the to-do ``ServiceContext`` was created
@@ -230,7 +224,6 @@ extension ServiceContext {
230
224
231
225
// MARK: - Propagating ServiceContext
232
226
233
- #if swift(>=5.5) && canImport(_Concurrency)
234
227
@available ( macOS 10 . 15 , iOS 13 . 0 , watchOS 6 . 0 , tvOS 13 . 0 , * )
235
228
extension ServiceContext {
236
229
/// A `ServiceContext` is automatically propagated through task-local storage. This API enables binding a top-level `ServiceContext` and
@@ -251,15 +244,19 @@ extension ServiceContext {
251
244
/// To access the task-local value, use `ServiceContext.current`.
252
245
///
253
246
/// SeeAlso: [Swift Task Locals](https://developer.apple.com/documentation/swift/tasklocal)
254
- #if swift(>=5.7)
255
- @_unsafeInheritExecutor // same as withValue declared in the stdlib; because we do not want to hop off the executor at all
256
- public static func withValue< T> ( _ value: ServiceContext ? , operation: ( ) async throws -> T ) async rethrows -> T {
247
+ #if swift(>=6.0)
248
+ public static func withValue< T> ( _ value: ServiceContext ? ,
249
+ isolation: isolated( any Actor ) ? = #isolation,
250
+ operation: ( ) async throws -> T ) async rethrows -> T
251
+ {
257
252
try await ServiceContext . $current. withValue ( value, operation: operation)
258
253
}
259
- #else
254
+ #endif
255
+
256
+ @available ( * , deprecated, message: " Prefer withValue(_:isolation:operation:) " )
257
+ @_disfavoredOverload
258
+ @_unsafeInheritExecutor // Deprecated trick to avoid executor hop here; 6.0 introduces the proper replacement: #isolation
260
259
public static func withValue< T> ( _ value: ServiceContext ? , operation: ( ) async throws -> T ) async rethrows -> T {
261
260
try await ServiceContext . $current. withValue ( value, operation: operation)
262
261
}
263
- #endif
264
262
}
265
- #endif
0 commit comments