Skip to content

Commit

Permalink
Update package to support Swift 6.0; Drop older than 5.7 (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktoso authored Jul 19, 2024
1 parent 89b1cd2 commit 0c62c5b
Show file tree
Hide file tree
Showing 12 changed files with 18 additions and 556 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.6
// swift-tools-version:5.7

import PackageDescription

Expand Down
43 changes: 0 additions & 43 deletions Package@swift-5.1.swift

This file was deleted.

42 changes: 0 additions & 42 deletions Package@swift-5.2.swift

This file was deleted.

42 changes: 0 additions & 42 deletions Package@swift-5.3.swift

This file was deleted.

42 changes: 0 additions & 42 deletions Package@swift-5.4.swift

This file was deleted.

43 changes: 0 additions & 43 deletions Package@swift-5.5.swift

This file was deleted.

29 changes: 13 additions & 16 deletions Sources/ServiceContextModule/ServiceContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@
//
//===----------------------------------------------------------------------===//

#if swift(>=5.5) && canImport(_Concurrency)
public typealias _ServiceContext_Sendable = Swift.Sendable
#else
public typealias _ServiceContext_Sendable = Any
#endif

/// A `ServiceContext` is a heterogeneous storage type with value semantics for keyed values in a type-safe fashion.
///
/// Its values are uniquely identified via ``ServiceContextKey``s (by type identity). These keys also dictate the type of
Expand Down Expand Up @@ -71,8 +65,8 @@ public typealias _ServiceContext_Sendable = Any
/// `ServiceContext` does not expose more functions on purpose to prevent abuse and treating it as too much of an
/// arbitrary value smuggling container, but only make it convenient for tracing and instrumentation systems which need
/// to access either specific or all items carried inside a baggage.
public struct ServiceContext: _ServiceContext_Sendable {
private var _storage = [AnyServiceContextKey: _ServiceContext_Sendable]()
public struct ServiceContext: Sendable {
private var _storage = [AnyServiceContextKey: Sendable]()

/// Internal on purpose, please use ``ServiceContext/TODO(_:function:file:line:)`` or ``ServiceContext/topLevel`` to create an "empty" baggage,
/// which carries more meaning to other developers why an empty baggage was used.
Expand Down Expand Up @@ -157,7 +151,7 @@ extension ServiceContext {

/// Carried automatically by a "to do" baggage.
/// 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.
public struct TODOLocation: _ServiceContext_Sendable {
public struct TODOLocation: Sendable {
/// Source file location where the to-do ``ServiceContext`` was created
public let file: String
/// Source line location where the to-do ``ServiceContext`` was created
Expand Down Expand Up @@ -230,7 +224,6 @@ extension ServiceContext {

// MARK: - Propagating ServiceContext

#if swift(>=5.5) && canImport(_Concurrency)
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
extension ServiceContext {
/// A `ServiceContext` is automatically propagated through task-local storage. This API enables binding a top-level `ServiceContext` and
Expand All @@ -251,15 +244,19 @@ extension ServiceContext {
/// To access the task-local value, use `ServiceContext.current`.
///
/// SeeAlso: [Swift Task Locals](https://developer.apple.com/documentation/swift/tasklocal)
#if swift(>=5.7)
@_unsafeInheritExecutor // same as withValue declared in the stdlib; because we do not want to hop off the executor at all
public static func withValue<T>(_ value: ServiceContext?, operation: () async throws -> T) async rethrows -> T {
#if swift(>=6.0)
public static func withValue<T>(_ value: ServiceContext?,
isolation: isolated(any Actor)? = #isolation,
operation: () async throws -> T) async rethrows -> T
{
try await ServiceContext.$current.withValue(value, operation: operation)
}
#else
#endif

@available(*, deprecated, message: "Prefer withValue(_:isolation:operation:)")
@_disfavoredOverload
@_unsafeInheritExecutor // Deprecated trick to avoid executor hop here; 6.0 introduces the proper replacement: #isolation
public static func withValue<T>(_ value: ServiceContext?, operation: () async throws -> T) async rethrows -> T {
try await ServiceContext.$current.withValue(value, operation: operation)
}
#endif
}
#endif
6 changes: 3 additions & 3 deletions Sources/ServiceContextModule/ServiceContextKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
/// }
///
/// This pattern allows library authors fine-grained control over which values may be set, and which only get by end-users.
public protocol ServiceContextKey: _ServiceContext_Sendable {
public protocol ServiceContextKey: Sendable {
/// The type of value uniquely identified by this key.
associatedtype Value: _ServiceContext_Sendable
associatedtype Value: Sendable

/// The human-readable name of this key.
/// This name will be used instead of the type name when a value is printed.
Expand All @@ -62,7 +62,7 @@ extension ServiceContextKey {
}

/// A type-erased ``ServiceContextKey`` used when iterating through the ``ServiceContext`` using its `forEach` method.
public struct AnyServiceContextKey: _ServiceContext_Sendable {
public struct AnyServiceContextKey: Sendable {
/// The key's type erased to `Any.Type`.
public let keyType: Any.Type

Expand Down
42 changes: 0 additions & 42 deletions Tests/LinuxMain.swift

This file was deleted.

38 changes: 0 additions & 38 deletions Tests/ServiceContextTests/ServiceContextTests+XCTest.swift

This file was deleted.

Loading

0 comments on commit 0c62c5b

Please sign in to comment.