Skip to content

Commit 213515d

Browse files
authored
Referencing can be tracked (#27)
1 parent 3dbed15 commit 213515d

File tree

2 files changed

+70
-59
lines changed

2 files changed

+70
-59
lines changed

Sources/StateStruct/Referencing.swift

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44
*/
55
@propertyWrapper
66
@dynamicMemberLookup
7+
@Tracking
78
public struct Referencing<Value> {
8-
9-
public static func == (lhs: Self, rhs: Self) -> Bool {
10-
lhs.storage === rhs.storage
11-
}
12-
9+
1310
public subscript <U>(dynamicMember keyPath: KeyPath<Value, U>) -> U {
1411
_read { yield wrappedValue[keyPath: keyPath] }
1512
}
@@ -29,36 +26,14 @@ public struct Referencing<Value> {
2926
}
3027

3128
public init(wrappedValue: consuming Value) {
32-
self.storage = .init(consume wrappedValue)
29+
self._backing_wrappedValue = .init(wrappedValue)
3330
}
3431

3532
public init(storage: _BackingStorage<Value>) {
36-
self.storage = storage
37-
}
38-
39-
private var storage: _BackingStorage<Value>
40-
41-
public var wrappedValue: Value {
42-
get {
43-
return storage.value
44-
}
45-
set {
46-
if isKnownUniquelyReferenced(&storage) {
47-
storage.value = newValue
48-
} else {
49-
storage = .init(newValue)
50-
}
51-
}
52-
_modify {
53-
if isKnownUniquelyReferenced(&storage) {
54-
yield &storage.value
55-
} else {
56-
var current = storage.value
57-
yield &current
58-
storage = .init(current)
59-
}
60-
}
33+
self._backing_wrappedValue = storage
6134
}
35+
36+
public var wrappedValue: Value
6237

6338
public var projectedValue: Self {
6439
get {
@@ -73,7 +48,7 @@ public struct Referencing<Value> {
7348

7449
extension Referencing where Value : Equatable {
7550
public static func == (lhs: Self, rhs: Self) -> Bool {
76-
lhs.storage === rhs.storage || lhs.wrappedValue == rhs.wrappedValue
51+
lhs._backing_wrappedValue === rhs._backing_wrappedValue
7752
}
7853
}
7954

Tests/StateStructTests/TrackingTests.swift

Lines changed: 63 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,15 @@ struct TrackingTests {
8383
)
8484

8585
}
86-
86+
8787
@Test
8888
func read_over_function() {
8989
let original = MyState.init()
90-
90+
9191
let result = original.tracking {
9292
_ = original.customDescription()
9393
}
94-
94+
9595
#expect(
9696
result.graph.prettyPrint() == """
9797
StateStructTests.MyState {
@@ -322,41 +322,41 @@ struct TrackingTests {
322322

323323
@Test
324324
func write_is_empty() {
325-
325+
326326
let original = MyState.init()
327-
327+
328328
var result = original.tracking {
329329
_ = original.nested.name
330-
330+
331331
var nested = original.nested
332-
nested.name = "AAA"
332+
nested.name = "AAA"
333333
}
334-
334+
335335
result.graph.shakeAsWrite()
336-
336+
337337
result.graph.prettyPrint()
338-
338+
339339
#expect(
340340
result.graph.isEmpty == true
341341
)
342-
342+
343343
}
344-
344+
345345
@Test
346346
func modify_count() {
347-
347+
348348
var original = MyState.init()
349-
349+
350350
func update(_ value: inout MyState.Nested) {
351351
value.name = "AAA"
352352
value.name = "AAA"
353353
value.age = 100
354354
}
355-
355+
356356
let result = original.tracking {
357357
update(&original.nested)
358358
}
359-
359+
360360
#expect(
361361
result.graph.prettyPrint() == """
362362
StateStructTests.MyState {
@@ -368,23 +368,23 @@ struct TrackingTests {
368368
"""
369369
)
370370
}
371-
371+
372372
@Test
373373
func modify_count_1() {
374-
374+
375375
var original = MyState.init()
376-
376+
377377
func update(_ value: inout MyState.Nested) {
378-
let r = value.tracking {
378+
let r = value.tracking {
379379
value = .init(name: "AAA")
380380
}
381381
print(r.graph.prettyPrint())
382382
}
383-
383+
384384
let result = original.tracking {
385385
update(&original.nested)
386386
}
387-
387+
388388
#expect(
389389
result.graph.prettyPrint() == """
390390
StateStructTests.MyState {
@@ -393,24 +393,60 @@ struct TrackingTests {
393393
"""
394394
)
395395
}
396-
396+
397397
@Test
398398
func modify_count_2() {
399-
399+
400400
var original = MyState.init()
401-
401+
402402
func update(_ value: inout MyState) {
403403

404404
}
405-
405+
406406
let result = original.tracking {
407407
update(&original)
408408
}
409-
409+
410410
#expect(
411411
result.graph.prettyPrint() == """
412412
StateStructTests.MyState
413413
"""
414414
)
415415
}
416+
417+
@Test
418+
func referencing() {
419+
420+
let tree = Tree(another: .init(wrappedValue: .init()))
421+
422+
let result = tree.tracking {
423+
_ = tree.another?.value
424+
}
425+
426+
#expect(
427+
result.graph.prettyPrint() == """
428+
StateStructTests.Tree {
429+
another-(1) {
430+
wrappedValue-(1) {
431+
value-(1)
432+
}
433+
}
434+
}
435+
"""
436+
)
437+
}
438+
}
439+
440+
@Tracking
441+
struct AnotherTree {
442+
443+
var value: Int = 0
444+
445+
}
446+
447+
@Tracking
448+
struct Tree {
449+
450+
var another: Referencing<AnotherTree>?
451+
416452
}

0 commit comments

Comments
 (0)