Files
swift-async-algorithms/Sources/AsyncAlgorithms/AsyncLazySequence.swift
T
Philippe Hausler 361c7926d1 Add a package layout and initial implementation and tests for AsyncLazySequence (#2)
* Add a package layout and initial implementation and tests for AsyncLazySequence; a type for converting non asynchronous sequences into asynchronous ones

* Add a first iteration pass expectation to ensure that tasks spawn for testing cancellation of AsyncLazySequence

* Simplify the iterator for AsyncLazySequence
2022-01-11 10:26:32 -08:00

68 lines
1.6 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
//
//===----------------------------------------------------------------------===//
extension Sequence {
@inlinable
public var async: AsyncLazySequence<Self> {
AsyncLazySequence(self)
}
}
@frozen
public struct AsyncLazySequence<Base: Sequence>: AsyncSequence {
public typealias Element = Base.Element
@frozen
public struct Iterator: AsyncIteratorProtocol {
@usableFromInline
var iterator: Base.Iterator?
@usableFromInline
init(_ iterator: Base.Iterator) {
self.iterator = iterator
}
@inlinable
public mutating func next() async -> Base.Element? {
if !Task.isCancelled, let value = iterator?.next() {
return value
} else {
iterator = nil
return nil
}
}
}
@usableFromInline
let base: Base
@usableFromInline
init(_ base: Base) {
self.base = base
}
@inlinable
public func makeAsyncIterator() -> Iterator {
Iterator(base.makeIterator())
}
}
extension AsyncLazySequence: Sendable where Base: Sendable { }
extension AsyncLazySequence.Iterator: Sendable where Base.Iterator: Sendable { }
extension AsyncLazySequence: Codable where Base: Codable { }
extension AsyncLazySequence: Equatable where Base: Equatable { }
extension AsyncLazySequence: Hashable where Base: Hashable { }