17
17
/// predicates are simple wrappers around closures to provide static type information and
18
18
/// allow composition and wrapping of existing behaviors.
19
19
public struct Predicate < T> {
20
- fileprivate var matcher : ( Expression < T > ) throws -> PredicateResult
20
+ fileprivate let matcher : @ Sendable ( Expression < T > ) throws -> PredicateResult
21
21
22
22
/// Constructs a predicate that knows how take a given value
23
- public init ( _ matcher: @escaping ( Expression < T > ) throws -> PredicateResult ) {
23
+ public init ( _ matcher: @escaping @ Sendable ( Expression < T > ) throws -> PredicateResult ) {
24
24
self . matcher = matcher
25
25
}
26
26
@@ -33,26 +33,28 @@ public struct Predicate<T> {
33
33
}
34
34
}
35
35
36
+ extension Predicate : Sendable where T: Sendable { }
37
+
36
38
/// Provides convenience helpers to defining predicates
37
39
extension Predicate {
38
40
/// Like Predicate() constructor, but automatically guard against nil (actual) values
39
- public static func define( matcher: @escaping ( Expression < T > ) throws -> PredicateResult ) -> Predicate < T > {
41
+ public static func define( matcher: @escaping @ Sendable ( Expression < T > ) throws -> PredicateResult ) -> Predicate < T > {
40
42
return Predicate< T> { actual in
41
43
return try matcher ( actual)
42
44
} . requireNonNil
43
45
}
44
46
45
47
/// Defines a predicate with a default message that can be returned in the closure
46
48
/// Also ensures the predicate's actual value cannot pass with `nil` given.
47
- public static func define( _ message: String = " match " , matcher: @escaping ( Expression < T > , ExpectationMessage ) throws -> PredicateResult ) -> Predicate < T > {
49
+ public static func define( _ message: String = " match " , matcher: @escaping @ Sendable ( Expression < T > , ExpectationMessage ) throws -> PredicateResult ) -> Predicate < T > {
48
50
return Predicate< T> { actual in
49
51
return try matcher ( actual, . expectedActualValueTo( message) )
50
52
} . requireNonNil
51
53
}
52
54
53
55
/// Defines a predicate with a default message that can be returned in the closure
54
56
/// Unlike `define`, this allows nil values to succeed if the given closure chooses to.
55
- public static func defineNilable( _ message: String = " match " , matcher: @escaping ( Expression < T > , ExpectationMessage ) throws -> PredicateResult ) -> Predicate < T > {
57
+ public static func defineNilable( _ message: String = " match " , matcher: @escaping @ Sendable ( Expression < T > , ExpectationMessage ) throws -> PredicateResult ) -> Predicate < T > {
56
58
return Predicate< T> { actual in
57
59
return try matcher ( actual, . expectedActualValueTo( message) )
58
60
}
@@ -64,7 +66,7 @@ extension Predicate {
64
66
/// error message.
65
67
///
66
68
/// Also ensures the predicate's actual value cannot pass with `nil` given.
67
- public static func simple( _ message: String = " match " , matcher: @escaping ( Expression < T > ) throws -> PredicateStatus ) -> Predicate < T > {
69
+ public static func simple( _ message: String = " match " , matcher: @escaping @ Sendable ( Expression < T > ) throws -> PredicateStatus ) -> Predicate < T > {
68
70
return Predicate< T> { actual in
69
71
return PredicateResult ( status: try matcher ( actual) , message: . expectedActualValueTo( message) )
70
72
} . requireNonNil
@@ -74,21 +76,21 @@ extension Predicate {
74
76
/// error message.
75
77
///
76
78
/// Unlike `simple`, this allows nil values to succeed if the given closure chooses to.
77
- public static func simpleNilable( _ message: String = " match " , matcher: @escaping ( Expression < T > ) throws -> PredicateStatus ) -> Predicate < T > {
79
+ public static func simpleNilable( _ message: String = " match " , matcher: @escaping @ Sendable ( Expression < T > ) throws -> PredicateStatus ) -> Predicate < T > {
78
80
return Predicate< T> { actual in
79
81
return PredicateResult ( status: try matcher ( actual) , message: . expectedActualValueTo( message) )
80
82
}
81
83
}
82
84
}
83
85
84
86
// The Expectation style intended for comparison to a PredicateStatus.
85
- public enum ExpectationStyle {
87
+ public enum ExpectationStyle : Sendable {
86
88
case toMatch, toNotMatch
87
89
}
88
90
89
91
/// The value that a Predicates return to describe if the given (actual) value matches the
90
92
/// predicate.
91
- public struct PredicateResult {
93
+ public struct PredicateResult : Sendable {
92
94
/// Status indicates if the predicate matches, does not match, or fails.
93
95
public var status : PredicateStatus
94
96
/// The error message that can be displayed if it does not match
@@ -113,7 +115,7 @@ public struct PredicateResult {
113
115
}
114
116
115
117
/// PredicateStatus is a trinary that indicates if a Predicate matches a given value or not
116
- public enum PredicateStatus {
118
+ public enum PredicateStatus : Sendable {
117
119
/// Matches indicates if the predicate / matcher passes with the given value
118
120
///
119
121
/// For example, `equals(1)` returns `.matches` for `expect(1).to(equal(1))`.
@@ -167,7 +169,7 @@ public enum PredicateStatus {
167
169
168
170
extension Predicate {
169
171
// Someday, make this public? Needs documentation
170
- internal func after( f: @escaping ( Expression < T > , PredicateResult ) throws -> PredicateResult ) -> Predicate < T > {
172
+ internal func after( f: @escaping @ Sendable ( Expression < T > , PredicateResult ) throws -> PredicateResult ) -> Predicate < T > {
171
173
// swiftlint:disable:previous identifier_name
172
174
return Predicate { actual -> PredicateResult in
173
175
let result = try self . satisfies ( actual)
@@ -193,7 +195,7 @@ extension Predicate {
193
195
#if canImport(Darwin)
194
196
import class Foundation. NSObject
195
197
196
- public typealias PredicateBlock = ( _ actualExpression: Expression < NSObject > ) throws -> NMBPredicateResult
198
+ public typealias PredicateBlock = @ Sendable ( _ actualExpression: Expression < NSObject > ) throws -> NMBPredicateResult
197
199
198
200
public class NMBPredicate : NSObject {
199
201
private let predicate : PredicateBlock
@@ -202,7 +204,7 @@ public class NMBPredicate: NSObject {
202
204
self . predicate = predicate
203
205
}
204
206
205
- func satisfies( _ expression: @escaping ( ) throws -> NSObject ? , location: SourceLocation ) -> NMBPredicateResult {
207
+ func satisfies( _ expression: @escaping @ Sendable ( ) throws -> NSObject ? , location: SourceLocation ) -> NMBPredicateResult {
206
208
let expr = Expression ( expression: expression, location: location)
207
209
do {
208
210
return try self . predicate ( expr)
@@ -238,7 +240,7 @@ extension PredicateResult {
238
240
}
239
241
}
240
242
241
- final public class NMBPredicateStatus : NSObject {
243
+ final public class NMBPredicateStatus : NSObject , Sendable {
242
244
private let status : Int
243
245
private init ( status: Int ) {
244
246
self . status = status
0 commit comments