2
2
//
3
3
// This source file is part of the Swift open source project
4
4
//
5
- // Copyright (c) 2023 Apple Inc. and the Swift project authors
5
+ // Copyright (c) 2023-2024 Apple Inc. and the Swift project authors
6
6
// Licensed under Apache License v2.0 with Runtime Library Exception
7
7
//
8
8
// See https://swift.org/LICENSE.txt for license information
@@ -16,17 +16,21 @@ import struct SystemPackage.FilePath
16
16
17
17
/// Indicates that values of a conforming type can be hashed with an arbitrary hashing function. Unlike `Hashable`,
18
18
/// this protocol doesn't utilize random seed values and produces consistent hash values across process launches.
19
- package protocol CacheKey : Encodable {
19
+ public protocol CacheKey : Encodable { }
20
+
21
+ /// Types that cannot be decomposed more to be hashed
22
+ protocol LeafCacheKey : CacheKey {
23
+ func hash( with hashFunction: inout some HashFunction )
20
24
}
21
25
22
- extension Bool : CacheKey {
26
+ extension Bool : LeafCacheKey {
23
27
func hash( with hashFunction: inout some HashFunction ) {
24
28
String ( reflecting: Self . self) . hash ( with: & hashFunction)
25
29
hashFunction. update ( data: self ? [ 1 ] : [ 0 ] )
26
30
}
27
31
}
28
32
29
- extension Int : CacheKey {
33
+ extension Int : LeafCacheKey {
30
34
func hash( with hashFunction: inout some HashFunction ) {
31
35
String ( reflecting: Self . self) . hash ( with: & hashFunction)
32
36
withUnsafeBytes ( of: self ) {
@@ -35,7 +39,7 @@ extension Int: CacheKey {
35
39
}
36
40
}
37
41
38
- extension Int8 : CacheKey {
42
+ extension Int8 : LeafCacheKey {
39
43
func hash( with hashFunction: inout some HashFunction ) {
40
44
String ( reflecting: Self . self) . hash ( with: & hashFunction)
41
45
withUnsafeBytes ( of: self ) {
@@ -44,7 +48,7 @@ extension Int8: CacheKey {
44
48
}
45
49
}
46
50
47
- extension Int16 : CacheKey {
51
+ extension Int16 : LeafCacheKey {
48
52
func hash( with hashFunction: inout some HashFunction ) {
49
53
String ( reflecting: Self . self) . hash ( with: & hashFunction)
50
54
withUnsafeBytes ( of: self ) {
@@ -53,7 +57,7 @@ extension Int16: CacheKey {
53
57
}
54
58
}
55
59
56
- extension Int32 : CacheKey {
60
+ extension Int32 : LeafCacheKey {
57
61
func hash( with hashFunction: inout some HashFunction ) {
58
62
String ( reflecting: Self . self) . hash ( with: & hashFunction)
59
63
withUnsafeBytes ( of: self ) {
@@ -62,7 +66,7 @@ extension Int32: CacheKey {
62
66
}
63
67
}
64
68
65
- extension Int64 : CacheKey {
69
+ extension Int64 : LeafCacheKey {
66
70
func hash( with hashFunction: inout some HashFunction ) {
67
71
String ( reflecting: Self . self) . hash ( with: & hashFunction)
68
72
withUnsafeBytes ( of: self ) {
@@ -71,7 +75,7 @@ extension Int64: CacheKey {
71
75
}
72
76
}
73
77
74
- extension UInt : CacheKey {
78
+ extension UInt : LeafCacheKey {
75
79
func hash( with hashFunction: inout some HashFunction ) {
76
80
String ( reflecting: Self . self) . hash ( with: & hashFunction)
77
81
withUnsafeBytes ( of: self ) {
@@ -80,7 +84,7 @@ extension UInt: CacheKey {
80
84
}
81
85
}
82
86
83
- extension UInt8 : CacheKey {
87
+ extension UInt8 : LeafCacheKey {
84
88
func hash( with hashFunction: inout some HashFunction ) {
85
89
String ( reflecting: Self . self) . hash ( with: & hashFunction)
86
90
withUnsafeBytes ( of: self ) {
@@ -89,7 +93,7 @@ extension UInt8: CacheKey {
89
93
}
90
94
}
91
95
92
- extension UInt16 : CacheKey {
96
+ extension UInt16 : LeafCacheKey {
93
97
func hash( with hashFunction: inout some HashFunction ) {
94
98
String ( reflecting: Self . self) . hash ( with: & hashFunction)
95
99
withUnsafeBytes ( of: self ) {
@@ -98,7 +102,7 @@ extension UInt16: CacheKey {
98
102
}
99
103
}
100
104
101
- extension UInt32 : CacheKey {
105
+ extension UInt32 : LeafCacheKey {
102
106
func hash( with hashFunction: inout some HashFunction ) {
103
107
String ( reflecting: Self . self) . hash ( with: & hashFunction)
104
108
withUnsafeBytes ( of: self ) {
@@ -107,7 +111,7 @@ extension UInt32: CacheKey {
107
111
}
108
112
}
109
113
110
- extension UInt64 : CacheKey {
114
+ extension UInt64 : LeafCacheKey {
111
115
func hash( with hashFunction: inout some HashFunction ) {
112
116
String ( reflecting: Self . self) . hash ( with: & hashFunction)
113
117
withUnsafeBytes ( of: self ) {
@@ -116,7 +120,7 @@ extension UInt64: CacheKey {
116
120
}
117
121
}
118
122
119
- extension Float : CacheKey {
123
+ extension Float : LeafCacheKey {
120
124
func hash( with hashFunction: inout some HashFunction ) {
121
125
String ( reflecting: Self . self) . hash ( with: & hashFunction)
122
126
withUnsafeBytes ( of: self ) {
@@ -125,7 +129,7 @@ extension Float: CacheKey {
125
129
}
126
130
}
127
131
128
- extension Double : CacheKey {
132
+ extension Double : LeafCacheKey {
129
133
func hash( with hashFunction: inout some HashFunction ) {
130
134
String ( reflecting: Self . self) . hash ( with: & hashFunction)
131
135
withUnsafeBytes ( of: self ) {
@@ -134,7 +138,7 @@ extension Double: CacheKey {
134
138
}
135
139
}
136
140
137
- extension String : CacheKey {
141
+ extension String : LeafCacheKey {
138
142
func hash( with hashFunction: inout some HashFunction ) {
139
143
var t = String ( reflecting: Self . self)
140
144
t. withUTF8 {
@@ -147,21 +151,21 @@ extension String: CacheKey {
147
151
}
148
152
}
149
153
150
- extension FilePath : CacheKey {
154
+ extension FilePath : LeafCacheKey {
151
155
func hash( with hashFunction: inout some HashFunction ) {
152
156
String ( reflecting: Self . self) . hash ( with: & hashFunction)
153
157
self . string. hash ( with: & hashFunction)
154
158
}
155
159
}
156
160
157
- extension FilePath . Component : CacheKey {
161
+ extension FilePath . Component : LeafCacheKey {
158
162
func hash( with hashFunction: inout some HashFunction ) {
159
163
String ( reflecting: Self . self) . hash ( with: & hashFunction)
160
164
self . string. hash ( with: & hashFunction)
161
165
}
162
166
}
163
167
164
- extension URL : CacheKey {
168
+ extension URL : LeafCacheKey {
165
169
func hash( with hashFunction: inout some HashFunction ) {
166
170
String ( reflecting: Self . self) . hash ( with: & hashFunction)
167
171
self . description. hash ( with: & hashFunction)
0 commit comments