Instance Method
reduceBytes(into:_:)
Fold the body’s bytes into a value, one chunk at a time.
func reduceBytes<R>(into initialResult: R, _ updateAccumulatingResult: @escaping (inout R, Span<UInt8>) async throws -> Void) async throws -> R
Parameters
initialResultThe value to start from.
updateAccumulatingResultFolds each chunk into the accumulator.
Return Value
The accumulated value. An empty body returns initialResult untouched.
Discussion
The streaming counterpart to computing something over a whole body without ever holding it in memory - hashing, counting, checksumming, incremental parsing. Chunk delivery and backpressure are exactly withStreamingBytes(_:)’s, so a streaming body is folded as it arrives and a buffered one is folded in a single step.
let digest = try await body.reduceBytes(into: SHA256()) { hasher, span in
span.withUnsafeBytes { hasher.update(bufferPointer: $0) }
}.finalize()
Like withStreamingBytes(_:) this consumes a streaming body.