Skip to content

Commit

Permalink
Migrate FoundationExtensions to Swift 6 (#57)
Browse files Browse the repository at this point in the history
* update package to swift 6

* fix keypath sendable error

* fix AnyError sendability

* fix data race possibility in Promise

* fix numeric extension tests

* fix Data+Matches tests

* fix DispatchTimeIntervalTests

* remove testing native ios codable api

* remove testing native combine api

* fix json tests

* adjust promise to swift 6

* add Duration helpers

* fix runtime warning for swift 6

* fix AnyEncodableTests

* fix swift 6 errors

* keep iOS 13 support

* provide thread safe static variable

---------

Co-authored-by: Oguz Yuksel <[email protected]>
  • Loading branch information
OguzYuuksel and Oguz Yuksel authored Sep 26, 2024
1 parent c5aa0f4 commit f664fd1
Show file tree
Hide file tree
Showing 23 changed files with 507 additions and 624 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.2
// swift-tools-version:6.0
import PackageDescription

let package = Package(
Expand Down
305 changes: 0 additions & 305 deletions Sources/FoundationExtensions/Combine/Publisher+Stream.swift

This file was deleted.

4 changes: 3 additions & 1 deletion Sources/FoundationExtensions/Concurrency/AsyncSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import _Concurrency

@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
extension AsyncSequence {

/// Returns a new asynchronous sequence that transforms the elements of the sequence using a key path.
Expand All @@ -16,3 +15,6 @@ extension AsyncSequence {
map { $0[keyPath: kp] }
}
}

// FIXME: https://github.com/swiftlang/swift/issues/57560
extension KeyPath: @unchecked Sendable {}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import Foundation

extension Locale {
internal static var getReference: () -> Locale = {
internal static func getReference() -> Locale {
return .current
}

Expand Down
3 changes: 2 additions & 1 deletion Sources/FoundationExtensions/Data/Data+Matches.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ extension Data {
/// - Returns: Returns true, if the expected bytes match our bytes, respecting the mask.
public func matches(_ match: MatchMask) -> Bool {
guard self.count >= match.expected.count, // match must be not longer than ourself (AA cannot match A)
match.expected.count > 0, // we need at least one match byte
match.expected.count == match.mask.count else { // the mask must be as long as the expected bytes
return false
}
Expand All @@ -99,6 +98,8 @@ extension Data {
}
// our data is empty, but we have match bytes? That's not a match 👎
return false
} else if match.expected.count == 0 {
return false
}

for (index, byte) in self.enumerated().prefix(match.expected.count) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ public struct ReadFileContents {
}

extension ReadFileContents {
public static let live = ReadFileContents { origin in
Result {
try Data(contentsOf: origin, options: .alwaysMapped)
public static var live: Self {
.init { origin in
Result {
try Data(contentsOf: origin, options: .alwaysMapped)
}
}
}
}
17 changes: 16 additions & 1 deletion Sources/FoundationExtensions/L10n/Localizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,23 @@ public enum Localizer {
}
}

// Swift 6 requires statics to be thread safe!
nonisolated(unsafe) private static var _fallbackMode: FallbackMode = .fallbackToEnglish
private static let lock = NSLock()

/// Determines how keys without a translation in the Locale language should be handled.
public static var fallbackMode: FallbackMode = .fallbackToEnglish
public static var fallbackMode: FallbackMode {
get {
lock.lock()
defer { lock.unlock() }
return _fallbackMode
}
set {
lock.lock()
defer { lock.unlock() }
_fallbackMode = newValue
}
}

fileprivate static func localizedString(_ key: String, bundle: Bundle, comment: String) -> String {
// Code here is adopted from https://stackoverflow.com/a/48415872
Expand Down
Loading

0 comments on commit f664fd1

Please sign in to comment.