File tree Expand file tree Collapse file tree 5 files changed +77
-7
lines changed
StructTransactionMacroTests Expand file tree Collapse file tree 5 files changed +77
-7
lines changed Original file line number Diff line number Diff line change
1
+ import Foundation
2
+
3
+ public struct Storage : Equatable {
4
+
5
+ public struct Identifier : Hashable {
6
+ public let name : String
7
+
8
+ public init ( name: String ) {
9
+ self . name = name
10
+ }
11
+ }
12
+
13
+ public private( set) var readIdentifiers : Set < Identifier > = [ ]
14
+ public private( set) var writeIdentifiers : Set < Identifier > = [ ]
15
+
16
+ public mutating func read( identifier: Identifier ) {
17
+ readIdentifiers. insert ( identifier)
18
+ }
19
+
20
+ public mutating func write( identifier: Identifier ) {
21
+ writeIdentifiers. insert ( identifier)
22
+ }
23
+ }
24
+
25
+ public func _tracking_modifyStorage( _ modifier: ( inout Storage ) -> Void ) {
26
+ guard Thread . current. threadDictionary [ " tracking " ] != nil else {
27
+ return
28
+ }
29
+ var storage = Thread . current. threadDictionary [ " tracking " ] as! Storage
30
+ modifier ( & storage)
31
+ Thread . current. threadDictionary [ " tracking " ] = storage
32
+ }
33
+
34
+ public func withTracking( _ perform: ( ) -> Void ) -> Storage {
35
+ let current = Thread . current. threadDictionary [ " tracking " ] as? Storage
36
+ defer {
37
+ Thread . current. threadDictionary [ " tracking " ] = current
38
+ }
39
+
40
+ Thread . current. threadDictionary [ " tracking " ] = Storage ( )
41
+ perform ( )
42
+ let result = Thread . current. threadDictionary [ " tracking " ] as! Storage
43
+ return result
44
+ }
Original file line number Diff line number Diff line change @@ -95,13 +95,21 @@ extension COWTrackingPropertyMacro: AccessorMacro {
95
95
96
96
let getAccessor = AccessorDeclSyntax (
97
97
"""
98
- get { \( raw: backingName) .value }
98
+ get {
99
+ _tracking_modifyStorage {
100
+ $0.read(identifier: .init(name: " \( raw: propertyName) " ))
101
+ }
102
+ return \( raw: backingName) .value
103
+ }
99
104
"""
100
105
)
101
106
102
107
let setAccessor = AccessorDeclSyntax (
103
108
"""
104
- set {
109
+ set {
110
+ _tracking_modifyStorage {
111
+ $0.write(identifier: .init(name: " \( raw: propertyName) " ))
112
+ }
105
113
if !isKnownUniquelyReferenced(& \( raw: backingName) ) {
106
114
\( raw: backingName) = .init( \( raw: backingName) .value)
107
115
} else {
@@ -114,6 +122,9 @@ extension COWTrackingPropertyMacro: AccessorMacro {
114
122
let modifyAccessor = AccessorDeclSyntax (
115
123
"""
116
124
_modify {
125
+ _tracking_modifyStorage {
126
+ $0.write(identifier: .init(name: " \( raw: propertyName) " ))
127
+ }
117
128
if !isKnownUniquelyReferenced(& \( raw: backingName) ) {
118
129
\( raw: backingName) = .init( \( raw: backingName) .value)
119
130
}
Original file line number Diff line number Diff line change @@ -38,16 +38,25 @@ final class COWTrackingProperyMacroTests: XCTestCase {
38
38
39
39
private var stored_0: Int = 18 {
40
40
get {
41
- _backing_stored_0.value
41
+ _tracking_modifyStorage {
42
+ $0.read(identifier: .init(name: " stored_0 " ))
43
+ }
44
+ return _backing_stored_0.value
42
45
}
43
46
set {
47
+ _tracking_modifyStorage {
48
+ $0.write(identifier: .init(name: " stored_0 " ))
49
+ }
44
50
if !isKnownUniquelyReferenced(&_backing_stored_0) {
45
51
_backing_stored_0 = .init(_backing_stored_0.value)
46
52
} else {
47
53
_backing_stored_0.value = newValue
48
54
}
49
55
}
50
56
_modify {
57
+ _tracking_modifyStorage {
58
+ $0.write(identifier: .init(name: " stored_0 " ))
59
+ }
51
60
if !isKnownUniquelyReferenced(&_backing_stored_0) {
52
61
_backing_stored_0 = .init(_backing_stored_0.value)
53
62
}
Original file line number Diff line number Diff line change @@ -44,13 +44,13 @@ final class TrackingMacroTests: XCTestCase {
44
44
} expansion: {
45
45
"""
46
46
struct MyState {
47
- @TrackingProperty
47
+ @COWTrackingProperty
48
48
49
49
private var stored_0: Int = 18
50
- @TrackingProperty
50
+ @COWTrackingProperty
51
51
52
52
var stored_1: String
53
- @TrackingProperty
53
+ @COWTrackingProperty
54
54
55
55
let stored_2: Int = 0
56
56
@@ -60,7 +60,7 @@ final class TrackingMacroTests: XCTestCase {
60
60
get { 0 }
61
61
set { }
62
62
}
63
- @TrackingProperty
63
+ @COWTrackingProperty
64
64
65
65
var height: Int
66
66
Original file line number Diff line number Diff line change
1
+ import Testing
2
+
3
+ @Suite ( " Tests " )
4
+ enum Tests {
5
+
6
+ }
You can’t perform that action at this time.
0 commit comments