Skip to content

Commit 44567fc

Browse files
authored
Add reader (#2)
1 parent e12f20c commit 44567fc

File tree

4 files changed

+236
-141
lines changed

4 files changed

+236
-141
lines changed

Sources/StructTransaction/Source.swift

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,43 @@
11

2-
@attached(extension, conformances: StateModifyingType, names: named(Modifying), named(modify(source:modifier:)), named(ModifyingTarget))
3-
public macro Writing() = #externalMacro(module: "StructTransactionMacros", type: "WriterMacro")
2+
@attached(extension, conformances: DetectingType, names: named(Modifying), named(modify(source:modifier:)), named(read(source:reader:)), named(ModifyingTarget))
3+
public macro Detecting() = #externalMacro(module: "StructTransactionMacros", type: "WriterMacro")
44

5-
public protocol StateModifyingType {
5+
public protocol DetectingType {
66

77
associatedtype Modifying
88

99
@discardableResult
1010
static func modify(source: inout Self, modifier: (inout Modifying) throws -> Void) rethrows -> ModifyingResult
11+
12+
@discardableResult
13+
static func read(source: Self, reader: (Modifying) throws -> Void) rethrows -> ReadResult
1114
}
1215

13-
extension StateModifyingType {
16+
extension DetectingType {
1417

1518
@discardableResult
1619
public mutating func modify(modifier: (inout Modifying) throws -> Void) rethrows -> ModifyingResult {
1720
try Self.modify(source: &self, modifier: modifier)
1821
}
1922

23+
@discardableResult
24+
public mutating func read(reader: (Modifying) throws -> Void) rethrows -> ReadResult {
25+
try Self.read(source: self, reader: reader)
26+
}
27+
}
28+
29+
public struct ReadResult {
30+
31+
public let readIdentifiers: Set<String>
32+
33+
public init(
34+
readIdentifiers: Set<String>
35+
) {
36+
self.readIdentifiers = readIdentifiers
37+
}
2038
}
2139

40+
2241
public struct ModifyingResult {
2342

2443
public let readIdentifiers: Set<String>

Sources/StructTransactionMacros/WriterMacro.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ extension WriterMacro: ExtensionMacro {
9292

9393
let modifyingDecl =
9494
("""
95-
extension \(type.trimmed): StateModifyingType {
95+
extension \(type.trimmed): DetectingType {
9696
9797
typealias ModifyingTarget = Self
9898
@@ -109,6 +109,20 @@ extension WriterMacro: ExtensionMacro {
109109
}
110110
}
111111
112+
@discardableResult
113+
public static func read(source: Self, reader: (Modifying) throws -> Void) rethrows -> ReadResult {
114+
// FIXME: avoid copying
115+
var reading = source
116+
117+
return try withUnsafeMutablePointer(to: &reading) { pointer in
118+
let modifying = Modifying(pointer: pointer)
119+
try reader(modifying)
120+
return ReadResult(
121+
readIdentifiers: modifying.$_readIdentifiers
122+
)
123+
}
124+
}
125+
112126
\(modifyingStructDecl)
113127
}
114128
""" as DeclSyntax)

0 commit comments

Comments
 (0)