Skip to content

Commit 25a89df

Browse files
committed
Update
1 parent 306c464 commit 25a89df

File tree

5 files changed

+77
-7
lines changed

5 files changed

+77
-7
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
}

Sources/StructTransactionMacros/COWTrackingPropertyMacro.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,21 @@ extension COWTrackingPropertyMacro: AccessorMacro {
9595

9696
let getAccessor = AccessorDeclSyntax(
9797
"""
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+
}
99104
"""
100105
)
101106

102107
let setAccessor = AccessorDeclSyntax(
103108
"""
104-
set {
109+
set {
110+
_tracking_modifyStorage {
111+
$0.write(identifier: .init(name: "\(raw: propertyName)"))
112+
}
105113
if !isKnownUniquelyReferenced(&\(raw: backingName)) {
106114
\(raw: backingName) = .init(\(raw: backingName).value)
107115
} else {
@@ -114,6 +122,9 @@ extension COWTrackingPropertyMacro: AccessorMacro {
114122
let modifyAccessor = AccessorDeclSyntax(
115123
"""
116124
_modify {
125+
_tracking_modifyStorage {
126+
$0.write(identifier: .init(name: "\(raw: propertyName)"))
127+
}
117128
if !isKnownUniquelyReferenced(&\(raw: backingName)) {
118129
\(raw: backingName) = .init(\(raw: backingName).value)
119130
}

Tests/StructTransactionMacroTests/COWTrackingProperyMacroTests.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,25 @@ final class COWTrackingProperyMacroTests: XCTestCase {
3838
3939
private var stored_0: Int = 18 {
4040
get {
41-
_backing_stored_0.value
41+
_tracking_modifyStorage {
42+
$0.read(identifier: .init(name: "stored_0"))
43+
}
44+
return _backing_stored_0.value
4245
}
4346
set {
47+
_tracking_modifyStorage {
48+
$0.write(identifier: .init(name: "stored_0"))
49+
}
4450
if !isKnownUniquelyReferenced(&_backing_stored_0) {
4551
_backing_stored_0 = .init(_backing_stored_0.value)
4652
} else {
4753
_backing_stored_0.value = newValue
4854
}
4955
}
5056
_modify {
57+
_tracking_modifyStorage {
58+
$0.write(identifier: .init(name: "stored_0"))
59+
}
5160
if !isKnownUniquelyReferenced(&_backing_stored_0) {
5261
_backing_stored_0 = .init(_backing_stored_0.value)
5362
}

Tests/StructTransactionMacroTests/TrackingMacroTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ final class TrackingMacroTests: XCTestCase {
4444
} expansion: {
4545
"""
4646
struct MyState {
47-
@TrackingProperty
47+
@COWTrackingProperty
4848
4949
private var stored_0: Int = 18
50-
@TrackingProperty
50+
@COWTrackingProperty
5151
5252
var stored_1: String
53-
@TrackingProperty
53+
@COWTrackingProperty
5454
5555
let stored_2: Int = 0
5656
@@ -60,7 +60,7 @@ final class TrackingMacroTests: XCTestCase {
6060
get { 0 }
6161
set { }
6262
}
63-
@TrackingProperty
63+
@COWTrackingProperty
6464
6565
var height: Int
6666
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import Testing
2+
3+
@Suite("Tests")
4+
enum Tests {
5+
6+
}

0 commit comments

Comments
 (0)