|
10 | 10 | //
|
11 | 11 | //===----------------------------------------------------------------------===//
|
12 | 12 |
|
13 |
| -import Macros |
14 |
| - |
15 | 13 | @_exported import protocol Crypto.HashFunction
|
16 | 14 | import struct Foundation.URL
|
17 | 15 | import struct SystemPackage.FilePath
|
18 | 16 |
|
19 | 17 | /// Indicates that values of a conforming type can be hashed with an arbitrary hashing function. Unlike `Hashable`,
|
20 | 18 | /// this protocol doesn't utilize random seed values and produces consistent hash values across process launches.
|
21 |
| -public protocol CacheKeyProtocol { |
22 |
| - func hash(with hashFunction: inout some HashFunction) |
| 19 | +public protocol CacheKey: Encodable { |
23 | 20 | }
|
24 | 21 |
|
25 |
| -extension Bool: CacheKeyProtocol { |
26 |
| - public func hash(with hashFunction: inout some HashFunction) { |
| 22 | +extension Bool: CacheKey { |
| 23 | + func hash(with hashFunction: inout some HashFunction) { |
27 | 24 | String(reflecting: Self.self).hash(with: &hashFunction)
|
28 | 25 | hashFunction.update(data: self ? [1] : [0])
|
29 | 26 | }
|
30 | 27 | }
|
31 | 28 |
|
32 |
| -extension Int: CacheKeyProtocol { |
33 |
| - public func hash(with hashFunction: inout some HashFunction) { |
| 29 | +extension Int: CacheKey { |
| 30 | + func hash(with hashFunction: inout some HashFunction) { |
34 | 31 | String(reflecting: Self.self).hash(with: &hashFunction)
|
35 | 32 | withUnsafeBytes(of: self) {
|
36 |
| - hashFunction.update(bufferPointer: $0) |
| 33 | + hashFunction.update(data: $0) |
37 | 34 | }
|
38 | 35 | }
|
39 | 36 | }
|
40 | 37 |
|
41 |
| -extension String: CacheKeyProtocol { |
42 |
| - public func hash(with hashFunction: inout some HashFunction) { |
43 |
| - var t = String(reflecting: Self.self) |
44 |
| - t.withUTF8 { |
45 |
| - hashFunction.update(bufferPointer: .init($0)) |
| 38 | +extension Int8: CacheKey { |
| 39 | + func hash(with hashFunction: inout some HashFunction) { |
| 40 | + String(reflecting: Self.self).hash(with: &hashFunction) |
| 41 | + withUnsafeBytes(of: self) { |
| 42 | + hashFunction.update(data: $0) |
46 | 43 | }
|
47 |
| - var x = self |
48 |
| - x.withUTF8 { |
49 |
| - hashFunction.update(bufferPointer: .init($0)) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +extension Int16: CacheKey { |
| 48 | + func hash(with hashFunction: inout some HashFunction) { |
| 49 | + String(reflecting: Self.self).hash(with: &hashFunction) |
| 50 | + withUnsafeBytes(of: self) { |
| 51 | + hashFunction.update(data: $0) |
50 | 52 | }
|
51 | 53 | }
|
52 | 54 | }
|
53 | 55 |
|
54 |
| -extension FilePath: CacheKeyProtocol { |
55 |
| - public func hash(with hashFunction: inout some HashFunction) { |
| 56 | +extension Int32: CacheKey { |
| 57 | + func hash(with hashFunction: inout some HashFunction) { |
56 | 58 | String(reflecting: Self.self).hash(with: &hashFunction)
|
57 |
| - self.string.hash(with: &hashFunction) |
| 59 | + withUnsafeBytes(of: self) { |
| 60 | + hashFunction.update(data: $0) |
| 61 | + } |
58 | 62 | }
|
59 | 63 | }
|
60 | 64 |
|
61 |
| -extension FilePath.Component: CacheKeyProtocol { |
62 |
| - public func hash(with hashFunction: inout some HashFunction) { |
| 65 | +extension Int64: CacheKey { |
| 66 | + func hash(with hashFunction: inout some HashFunction) { |
63 | 67 | String(reflecting: Self.self).hash(with: &hashFunction)
|
64 |
| - self.string.hash(with: &hashFunction) |
| 68 | + withUnsafeBytes(of: self) { |
| 69 | + hashFunction.update(data: $0) |
| 70 | + } |
65 | 71 | }
|
66 | 72 | }
|
67 | 73 |
|
68 |
| -extension URL: CacheKeyProtocol { |
69 |
| - public func hash(with hashFunction: inout some HashFunction) { |
| 74 | +extension UInt: CacheKey { |
| 75 | + func hash(with hashFunction: inout some HashFunction) { |
70 | 76 | String(reflecting: Self.self).hash(with: &hashFunction)
|
71 |
| - self.description.hash(with: &hashFunction) |
| 77 | + withUnsafeBytes(of: self) { |
| 78 | + hashFunction.update(data: $0) |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +extension UInt8: CacheKey { |
| 84 | + func hash(with hashFunction: inout some HashFunction) { |
| 85 | + String(reflecting: Self.self).hash(with: &hashFunction) |
| 86 | + withUnsafeBytes(of: self) { |
| 87 | + hashFunction.update(data: $0) |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +extension UInt16: CacheKey { |
| 93 | + func hash(with hashFunction: inout some HashFunction) { |
| 94 | + String(reflecting: Self.self).hash(with: &hashFunction) |
| 95 | + withUnsafeBytes(of: self) { |
| 96 | + hashFunction.update(data: $0) |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +extension UInt32: CacheKey { |
| 102 | + func hash(with hashFunction: inout some HashFunction) { |
| 103 | + String(reflecting: Self.self).hash(with: &hashFunction) |
| 104 | + withUnsafeBytes(of: self) { |
| 105 | + hashFunction.update(data: $0) |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +extension UInt64: CacheKey { |
| 111 | + func hash(with hashFunction: inout some HashFunction) { |
| 112 | + String(reflecting: Self.self).hash(with: &hashFunction) |
| 113 | + withUnsafeBytes(of: self) { |
| 114 | + hashFunction.update(data: $0) |
| 115 | + } |
72 | 116 | }
|
73 | 117 | }
|
74 | 118 |
|
75 |
| -extension Optional: CacheKeyProtocol where Wrapped: CacheKeyProtocol { |
76 |
| - public func hash(with hashFunction: inout some HashFunction) { |
| 119 | +extension Float: CacheKey { |
| 120 | + func hash(with hashFunction: inout some HashFunction) { |
77 | 121 | String(reflecting: Self.self).hash(with: &hashFunction)
|
78 |
| - if let self { |
79 |
| - true.hash(with: &hashFunction) |
80 |
| - self.hash(with: &hashFunction) |
81 |
| - } else { |
82 |
| - false.hash(with: &hashFunction) |
| 122 | + withUnsafeBytes(of: self) { |
| 123 | + hashFunction.update(data: $0) |
83 | 124 | }
|
84 | 125 | }
|
85 | 126 | }
|
86 | 127 |
|
87 |
| -extension Array: CacheKeyProtocol where Element: CacheKeyProtocol { |
88 |
| - public func hash(with hashFunction: inout some HashFunction) { |
| 128 | +extension Double: CacheKey { |
| 129 | + func hash(with hashFunction: inout some HashFunction) { |
89 | 130 | String(reflecting: Self.self).hash(with: &hashFunction)
|
90 |
| - for element in self { |
91 |
| - element.hash(with: &hashFunction) |
| 131 | + withUnsafeBytes(of: self) { |
| 132 | + hashFunction.update(data: $0) |
92 | 133 | }
|
93 | 134 | }
|
94 | 135 | }
|
95 | 136 |
|
96 |
| -@attached(extension, conformances: CacheKeyProtocol, names: named(hash(with:))) |
97 |
| -public macro CacheKey() = #externalMacro(module: "Macros", type: "CacheKeyMacro") |
| 137 | +extension String: CacheKey { |
| 138 | + func hash(with hashFunction: inout some HashFunction) { |
| 139 | + var t = String(reflecting: Self.self) |
| 140 | + t.withUTF8 { |
| 141 | + hashFunction.update(data: $0) |
| 142 | + } |
| 143 | + var x = self |
| 144 | + x.withUTF8 { |
| 145 | + hashFunction.update(data: $0) |
| 146 | + } |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +extension FilePath: CacheKey { |
| 151 | + func hash(with hashFunction: inout some HashFunction) { |
| 152 | + String(reflecting: Self.self).hash(with: &hashFunction) |
| 153 | + self.string.hash(with: &hashFunction) |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +extension FilePath.Component: CacheKey { |
| 158 | + func hash(with hashFunction: inout some HashFunction) { |
| 159 | + String(reflecting: Self.self).hash(with: &hashFunction) |
| 160 | + self.string.hash(with: &hashFunction) |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +extension URL: CacheKey { |
| 165 | + func hash(with hashFunction: inout some HashFunction) { |
| 166 | + String(reflecting: Self.self).hash(with: &hashFunction) |
| 167 | + self.description.hash(with: &hashFunction) |
| 168 | + } |
| 169 | +} |
0 commit comments