@@ -93,17 +93,6 @@ internal struct _HasherTailBuffer {
93
93
get { return value &>> 56 }
94
94
}
95
95
96
- internal var isFinalized : Bool {
97
- @inline ( __always)
98
- get { return value == 1 }
99
- }
100
-
101
- @inline ( __always)
102
- internal mutating func finalize( ) {
103
- // A byteCount of 0 with a nonzero tail never occurs during normal use.
104
- value = 1
105
- }
106
-
107
96
@inline ( __always)
108
97
internal mutating func append( _ bytes: UInt64 ) -> UInt64 {
109
98
let c = byteCount & 7
@@ -158,13 +147,11 @@ internal struct _BufferingHasher<Core: _HasherCore> {
158
147
159
148
@inline ( __always)
160
149
internal mutating func combine( _ value: UInt64 ) {
161
- precondition ( !_buffer. isFinalized)
162
150
_core. compress ( _buffer. append ( value) )
163
151
}
164
152
165
153
@inline ( __always)
166
154
internal mutating func combine( _ value: UInt32 ) {
167
- precondition ( !_buffer. isFinalized)
168
155
let value = UInt64 ( truncatingIfNeeded: value)
169
156
if let chunk = _buffer. append ( value, count: 4 ) {
170
157
_core. compress ( chunk)
@@ -173,7 +160,6 @@ internal struct _BufferingHasher<Core: _HasherCore> {
173
160
174
161
@inline ( __always)
175
162
internal mutating func combine( _ value: UInt16 ) {
176
- precondition ( !_buffer. isFinalized)
177
163
let value = UInt64 ( truncatingIfNeeded: value)
178
164
if let chunk = _buffer. append ( value, count: 2 ) {
179
165
_core. compress ( chunk)
@@ -182,7 +168,6 @@ internal struct _BufferingHasher<Core: _HasherCore> {
182
168
183
169
@inline ( __always)
184
170
internal mutating func combine( _ value: UInt8 ) {
185
- precondition ( !_buffer. isFinalized)
186
171
let value = UInt64 ( truncatingIfNeeded: value)
187
172
if let chunk = _buffer. append ( value, count: 1 ) {
188
173
_core. compress ( chunk)
@@ -191,7 +176,6 @@ internal struct _BufferingHasher<Core: _HasherCore> {
191
176
192
177
@inline ( __always)
193
178
internal mutating func combine( bytes: UInt64 , count: Int ) {
194
- precondition ( !_buffer. isFinalized)
195
179
_sanityCheck ( count <= 8 )
196
180
let count = UInt64 ( truncatingIfNeeded: count)
197
181
if let chunk = _buffer. append ( bytes, count: count) {
@@ -201,7 +185,6 @@ internal struct _BufferingHasher<Core: _HasherCore> {
201
185
202
186
@inline ( __always)
203
187
internal mutating func combine( bytes: UnsafeRawBufferPointer ) {
204
- precondition ( !_buffer. isFinalized)
205
188
var remaining = bytes. count
206
189
guard remaining > 0 else { return }
207
190
var data = bytes. baseAddress!
@@ -239,10 +222,7 @@ internal struct _BufferingHasher<Core: _HasherCore> {
239
222
240
223
@inline ( __always)
241
224
internal mutating func finalize( ) -> UInt64 {
242
- precondition ( !_buffer. isFinalized)
243
- let hash = _core. finalize ( tailAndByteCount: _buffer. value)
244
- _buffer. finalize ( )
245
- return hash
225
+ return _core. finalize ( tailAndByteCount: _buffer. value)
246
226
}
247
227
}
248
228
@@ -258,12 +238,11 @@ internal struct _BufferingHasher<Core: _HasherCore> {
258
238
/// hasher.combine("Hello")
259
239
/// let hashValue = hasher.finalize()
260
240
///
261
- /// Within the execution of a Swift program, `Hasher` guarantees that
262
- /// `finalize()` will always return the same value as long as it is fed the
263
- /// exact same sequence of bytes. However, the underlying hash algorithm is
264
- /// designed to exhibit avalanche effects: slight changes to the seed or the
265
- /// input byte sequence will typically produce drastic changes in the generated
266
- /// hash value.
241
+ /// Within the execution of a Swift program, `Hasher` guarantees that finalizing
242
+ /// it will always produce the same hash value as long as it is fed the exact
243
+ /// same sequence of bytes. However, the underlying hash algorithm is designed
244
+ /// to exhibit avalanche effects: slight changes to the seed or the input byte
245
+ /// sequence will typically produce drastic changes in the generated hash value.
267
246
///
268
247
/// - Note: Do not save or otherwise reuse hash values across executions of your
269
248
/// program. `Hasher` is usually randomly seeded, which means it will return
@@ -377,7 +356,16 @@ public struct Hasher {
377
356
/// Finalizing invalidates the hasher; additional bits cannot be combined
378
357
/// into it, and it cannot be finalized again.
379
358
@effects ( releasenone)
380
- public mutating func finalize( ) -> Int {
359
+ @usableFromInline
360
+ internal mutating func _finalize( ) -> Int {
381
361
return Int ( truncatingIfNeeded: _core. finalize ( ) )
382
362
}
363
+
364
+ /// Finalize the hasher state and return the hash value. Finalizing consumes
365
+ /// the hasher, forbidding further operations.
366
+ @effects ( releasenone)
367
+ public __consuming func finalize( ) -> Int {
368
+ var core = _core
369
+ return Int ( truncatingIfNeeded: core. finalize ( ) )
370
+ }
383
371
}
0 commit comments