Skip to content

Commit 6c836e1

Browse files
authored
Support Hashable (#19)
1 parent 1785fa0 commit 6c836e1

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

Sources/StateStruct/CopyOnWrite.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11

2+
/**
3+
non-atomic
4+
*/
25
public final class _BackingStorage<Value>: @unchecked Sendable {
36

47
public var value: Value
@@ -15,6 +18,11 @@ extension _BackingStorage: Equatable where Value: Equatable {
1518
}
1619
}
1720

21+
extension _BackingStorage: Hashable where Value: Hashable {
22+
public func hash(into hasher: inout Hasher) {
23+
value.hash(into: &hasher)
24+
}
25+
}
1826

1927
#if DEBUG
2028
private struct Before {

Sources/StateStruct/Source.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ public macro COWTrackingProperty() =
5757
var count: Int = 0
5858
}
5959

60+
@Tracking
61+
struct HashableState: Hashable {
62+
var count: Int = 0
63+
}
64+
6065
@Tracking
6166
struct MyState {
6267

Sources/StateStruct/Tracking.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@ extension Array {
1010
}
1111

1212

13-
public final class _TrackingContext: Sendable, Equatable {
13+
public final class _TrackingContext: Sendable, Hashable {
1414

1515
public static func == (lhs: _TrackingContext, rhs: _TrackingContext) -> Bool {
1616
// ``_TrackingContext`` is used only for embedding into the struct.
1717
// It always returns true when checked for equality to prevent
1818
// interfering with the actual equality check of the struct.
1919
return true
2020
}
21+
22+
public func hash(into hasher: inout Hasher) {
23+
0.hash(into: &hasher)
24+
}
2125

2226
@inlinable
2327
public var path: PropertyPath? {

0 commit comments

Comments
 (0)