Skip to content

Commit 5be6ebd

Browse files
authored
Add dynamicMemberLookup (#23)
1 parent 9f30b59 commit 5be6ebd

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

Sources/StateStruct/CopyOnWrite.swift

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11

22
import os.lock
33

4-
/**
5-
non-atomic
6-
*/
4+
@dynamicMemberLookup
75
public final class _BackingStorage<Value>: Sendable {
86

97
public var value: Value {
@@ -21,6 +19,24 @@ public final class _BackingStorage<Value>: Sendable {
2119

2220
private let _value: ManagedCriticalState<Value>
2321

22+
public subscript <U>(dynamicMember keyPath: KeyPath<Value, U>) -> U {
23+
_read { yield value[keyPath: keyPath] }
24+
}
25+
26+
public subscript <U>(dynamicMember keyPath: KeyPath<Value, U?>) -> U? {
27+
_read { yield value[keyPath: keyPath] }
28+
}
29+
30+
public subscript <U>(dynamicMember keyPath: WritableKeyPath<Value, U>) -> U {
31+
_read { yield value[keyPath: keyPath] }
32+
_modify { yield &value[keyPath: keyPath] }
33+
}
34+
35+
public subscript <U>(dynamicMember keyPath: WritableKeyPath<Value, U?>) -> U? {
36+
_read { yield value[keyPath: keyPath] }
37+
_modify { yield &value[keyPath: keyPath] }
38+
}
39+
2440
public init(_ value: consuming Value) {
2541
self._value = .init(value)
2642
}

0 commit comments

Comments
 (0)