Skip to content

Commit

Permalink
Fix compilation errors on earlier versions of swift
Browse files Browse the repository at this point in the history
  • Loading branch information
younata committed Oct 14, 2024
1 parent af883cd commit 8e8946e
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 78 deletions.
4 changes: 4 additions & 0 deletions Nimble.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
898F28B025D9F4C30052B8D0 /* AlwaysFailMatcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 898F28AF25D9F4C30052B8D0 /* AlwaysFailMatcher.swift */; };
899441EF2902EE4B00C1FAF9 /* AsyncAwaitTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 899441EE2902EE4B00C1FAF9 /* AsyncAwaitTest.swift */; };
899441F82902EF2500C1FAF9 /* DSL+AsyncAwait.swift in Sources */ = {isa = PBXBuildFile; fileRef = 899441F32902EF0900C1FAF9 /* DSL+AsyncAwait.swift */; };
8998C1082CBD9980009A3E1C /* BeLogical+Conformances.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8998C1072CBD9980009A3E1C /* BeLogical+Conformances.swift */; };
89B8C60F2C6476A6001F12D3 /* Negation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89B8C60E2C6476A6001F12D3 /* Negation.swift */; };
89B8C6112C6478F2001F12D3 /* NegationTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89B8C6102C6478F2001F12D3 /* NegationTest.swift */; };
89C297CC2A911CDA002A143F /* AsyncTimerSequenceTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89C297CB2A911CDA002A143F /* AsyncTimerSequenceTest.swift */; };
Expand Down Expand Up @@ -336,6 +337,7 @@
898F28AF25D9F4C30052B8D0 /* AlwaysFailMatcher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlwaysFailMatcher.swift; sourceTree = "<group>"; };
899441EE2902EE4B00C1FAF9 /* AsyncAwaitTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AsyncAwaitTest.swift; sourceTree = "<group>"; };
899441F32902EF0900C1FAF9 /* DSL+AsyncAwait.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "DSL+AsyncAwait.swift"; sourceTree = "<group>"; };
8998C1072CBD9980009A3E1C /* BeLogical+Conformances.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "BeLogical+Conformances.swift"; sourceTree = "<group>"; };
89B8C60E2C6476A6001F12D3 /* Negation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Negation.swift; sourceTree = "<group>"; };
89B8C6102C6478F2001F12D3 /* NegationTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NegationTest.swift; sourceTree = "<group>"; };
89C297CB2A911CDA002A143F /* AsyncTimerSequenceTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AsyncTimerSequenceTest.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -595,6 +597,7 @@
1FD8CD151968AB07008ED995 /* BeLessThan.swift */,
1FD8CD161968AB07008ED995 /* BeLessThanOrEqual.swift */,
1FD8CD171968AB07008ED995 /* BeLogical.swift */,
8998C1072CBD9980009A3E1C /* BeLogical+Conformances.swift */,
1FD8CD181968AB07008ED995 /* BeNil.swift */,
106112BC2251DFE7000A5848 /* BeResult.swift */,
1F91DD301C74BF61002C309F /* BeVoid.swift */,
Expand Down Expand Up @@ -900,6 +903,7 @@
7A0A26231E7F52360092A34E /* ToSucceed.swift in Sources */,
89F5E0862908E655001F9377 /* Polling+AsyncAwait.swift in Sources */,
897F84F42BA922B500BF354B /* NSLocking+Nimble.swift in Sources */,
8998C1082CBD9980009A3E1C /* BeLogical+Conformances.swift in Sources */,
899441F82902EF2500C1FAF9 /* DSL+AsyncAwait.swift in Sources */,
1FD8CD491968AB07008ED995 /* BeGreaterThanOrEqualTo.swift in Sources */,
1FE661571E6574E30035F243 /* ExpectationMessage.swift in Sources */,
Expand Down
10 changes: 8 additions & 2 deletions Sources/Nimble/AsyncExpression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ private final class MemoizedClosure<T: Sendable>: Sendable {
}

private let lock = NSRecursiveLock()
#if swift(>=5.10)
nonisolated(unsafe) private var _state = State.notStarted
nonisolated(unsafe) private var _continuations = [CheckedContinuation<T, Error>]()
nonisolated(unsafe) private var _task: Task<Void, Never>?
#else
private var _state = State.notStarted
private var _continuations = [CheckedContinuation<T, Error>]()
private var _task: Task<Void, Never>?
#endif

let closure: @Sendable () async throws -> T

Expand All @@ -25,9 +31,9 @@ private final class MemoizedClosure<T: Sendable>: Sendable {

@Sendable func callAsFunction(_ withoutCaching: Bool) async throws -> T {
if withoutCaching {
try await closure()
return try await closure()
} else {
try await withCheckedThrowingContinuation { continuation in
return try await withCheckedThrowingContinuation { continuation in
lock.withLock {
switch _state {
case .notStarted:
Expand Down
4 changes: 4 additions & 0 deletions Sources/Nimble/Expression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import Foundation

private final class MemoizedValue<T>: Sendable {
private let lock = NSRecursiveLock()
#if swift(>=5.10)
nonisolated(unsafe) private var cache: T? = nil
#else
private var cache: T? = nil
#endif
private let closure: @Sendable () throws -> T

init(_ closure: @escaping @Sendable () throws -> T) {
Expand Down
150 changes: 150 additions & 0 deletions Sources/Nimble/Matchers/BeLogical+Conformances.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import Foundation

#if swift(>=6)
extension Int8: @retroactive ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).int8Value
}
}

extension UInt8: @retroactive ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).uint8Value
}
}

extension Int16: @retroactive ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).int16Value
}
}

extension UInt16: @retroactive ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).uint16Value
}
}

extension Int32: @retroactive ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).int32Value
}
}

extension UInt32: @retroactive ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).uint32Value
}
}

extension Int64: @retroactive ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).int64Value
}
}

extension UInt64: @retroactive ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).uint64Value
}
}

extension Float: @retroactive ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).floatValue
}
}

extension Double: @retroactive ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).doubleValue
}
}

extension Int: @retroactive ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).intValue
}
}

extension UInt: @retroactive ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).uintValue
}
}

#else

extension Int8: ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).int8Value
}
}

extension UInt8: ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).uint8Value
}
}

extension Int16: ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).int16Value
}
}

extension UInt16: ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).uint16Value
}
}

extension Int32: ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).int32Value
}
}

extension UInt32: ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).uint32Value
}
}

extension Int64: ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).int64Value
}
}

extension UInt64: ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).uint64Value
}
}

extension Float: ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).floatValue
}
}

extension Double: ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).doubleValue
}
}

extension Int: ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).intValue
}
}

extension UInt: ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).uintValue
}
}

#endif
72 changes: 0 additions & 72 deletions Sources/Nimble/Matchers/BeLogical.swift
Original file line number Diff line number Diff line change
@@ -1,77 +1,5 @@
import Foundation

extension Int8: @retroactive ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).int8Value
}
}

extension UInt8: @retroactive ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).uint8Value
}
}

extension Int16: @retroactive ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).int16Value
}
}

extension UInt16: @retroactive ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).uint16Value
}
}

extension Int32: @retroactive ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).int32Value
}
}

extension UInt32: @retroactive ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).uint32Value
}
}

extension Int64: @retroactive ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).int64Value
}
}

extension UInt64: @retroactive ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).uint64Value
}
}

extension Float: @retroactive ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).floatValue
}
}

extension Double: @retroactive ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).doubleValue
}
}

extension Int: @retroactive ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).intValue
}
}

extension UInt: @retroactive ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = NSNumber(value: value).uintValue
}
}

internal func rename<T>(_ matcher: Matcher<T>, failureMessage message: ExpectationMessage) -> Matcher<T> {
return Matcher { actualExpression in
let result = try matcher.satisfies(actualExpression)
Expand Down
17 changes: 16 additions & 1 deletion Sources/Nimble/Matchers/PostNotification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@
import Foundation

final class NotificationCollector: Sendable {
#if swift(>=5.10)
nonisolated(unsafe) private(set) var observedNotifications: [Notification]
nonisolated(unsafe) private(set) var observedNotificationDescriptions: [String]
nonisolated(unsafe) private var tokens: [NSObjectProtocol]
#else
private(set) var observedNotifications: [Notification]
private(set) var observedNotificationDescriptions: [String]
private var tokens: [NSObjectProtocol]
#endif
private let notificationCenter: NotificationCenter
private let names: Set<Notification.Name>
nonisolated(unsafe) private var tokens: [NSObjectProtocol]
private let lock = NSRecursiveLock()

required init(notificationCenter: NotificationCenter, names: Set<Notification.Name> = []) {
Expand Down Expand Up @@ -55,13 +61,22 @@ final class NotificationCollector: Sendable {
}

#if !os(Windows)
#if swift(>=5.10)
nonisolated(unsafe) private let mainThread = pthread_self()
#else
private let mainThread = pthread_self()
#endif
#else
private let mainThread = Thread.mainThread
#endif

private final class OnlyOnceChecker: Sendable {
#if swift(>=5.10)
nonisolated(unsafe) var hasRun = false
#else
var hasRun = false
#endif

let lock = NSRecursiveLock()

func runOnlyOnce(_ closure: @Sendable () throws -> Void) rethrows {
Expand Down
5 changes: 5 additions & 0 deletions Sources/Nimble/Polling.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,13 @@ public struct AsyncDefaults {
public struct PollingDefaults: @unchecked Sendable {
private static let lock = NSRecursiveLock()

#if swift(>=5.10)
nonisolated(unsafe) private static var _timeout: NimbleTimeInterval = .seconds(1)
nonisolated(unsafe) private static var _pollInterval: NimbleTimeInterval = .milliseconds(10)
#else
private static var _timeout: NimbleTimeInterval = .seconds(1)
private static var _pollInterval: NimbleTimeInterval = .milliseconds(10)
#endif

public static var timeout: NimbleTimeInterval {
get {
Expand Down
11 changes: 11 additions & 0 deletions Sources/Nimble/Utils/LockedContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,14 @@ final class LockedContainer<T: Sendable>: @unchecked Sendable {
_value = newValue
}
}

extension NSLocking {
func withLock<R>(_ body: () throws -> R) rethrows -> R {
self.lock()
defer {
self.unlock()
}

return try body()
}
}
Loading

0 comments on commit 8e8946e

Please sign in to comment.