Files
swift-async-algorithms/Sources/AsyncAlgorithms/PartialIteration.swift
kperryua b8242c4a6b Add sequences that chunk values into collections based on a variety of criteria (#55)
* Add sequences that chunk values into collections based on a variety of criteria

* Factor out Merge2 logic so its termination behavior can be customized

* Rewrite AsyncChunksOfCountAndTriggerSequence in terms of the newly configurable merge state machine

* Name changes

* Fixed tests

* Enable chunked(byTime:) functions

* Broken up into one file per sequence
2022-02-23 17:17:20 -08:00

48 lines
1.3 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Async Algorithms open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
enum PartialIteration<Iterator: AsyncIteratorProtocol, Partial: Sendable>: CustomStringConvertible {
case idle(Iterator)
case pending(Task<Partial, Never>)
case terminal
var description: String {
switch self {
case .idle: return "idle"
case .pending: return "pending"
case .terminal: return "terminal"
}
}
mutating func resolve(_ result: Result<Iterator.Element?, Error>, _ iterator: Iterator) rethrows -> Iterator.Element? {
do {
guard let value = try result._rethrowGet() else {
self = .terminal
return nil
}
self = .idle(iterator)
return value
} catch {
self = .terminal
throw error
}
}
mutating func cancel() {
if case .pending(let task) = self {
task.cancel()
}
self = .terminal
}
}
extension PartialIteration: Sendable where Iterator: Sendable, Iterator.Element: Sendable { }