1313//
1414//===----------------------------------------------------------------------===//
1515
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-
2216/// A `ServiceContext` is a heterogeneous storage type with value semantics for keyed values in a type-safe fashion.
2317///
2418/// 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
7165/// `ServiceContext` does not expose more functions on purpose to prevent abuse and treating it as too much of an
7266/// arbitrary value smuggling container, but only make it convenient for tracing and instrumentation systems which need
7367/// 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 ] ( )
7670
7771 /// Internal on purpose, please use ``ServiceContext/TODO(_:function:file:line:)`` or ``ServiceContext/topLevel`` to create an "empty" baggage,
7872 /// which carries more meaning to other developers why an empty baggage was used.
@@ -157,7 +151,7 @@ extension ServiceContext {
157151
158152/// Carried automatically by a "to do" baggage.
159153/// 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 {
161155 /// Source file location where the to-do ``ServiceContext`` was created
162156 public let file : String
163157 /// Source line location where the to-do ``ServiceContext`` was created
@@ -230,7 +224,6 @@ extension ServiceContext {
230224
231225// MARK: - Propagating ServiceContext
232226
233- #if swift(>=5.5) && canImport(_Concurrency)
234227@available ( macOS 10 . 15 , iOS 13 . 0 , watchOS 6 . 0 , tvOS 13 . 0 , * )
235228extension ServiceContext {
236229 /// A `ServiceContext` is automatically propagated through task-local storage. This API enables binding a top-level `ServiceContext` and
@@ -251,15 +244,19 @@ extension ServiceContext {
251244 /// To access the task-local value, use `ServiceContext.current`.
252245 ///
253246 /// 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+ {
257252 try await ServiceContext . $current. withValue ( value, operation: operation)
258253 }
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
260259 public static func withValue< T> ( _ value: ServiceContext ? , operation: ( ) async throws -> T ) async rethrows -> T {
261260 try await ServiceContext . $current. withValue ( value, operation: operation)
262261 }
263- #endif
264262}
265- #endif
0 commit comments