AsyncIteratorSequence.swift 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright 2023, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import Atomics
  17. @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
  18. @usableFromInline
  19. /// An `AsyncSequence` which wraps an existing async iterator.
  20. struct AsyncIteratorSequence<Base: AsyncIteratorProtocol>: AsyncSequence {
  21. @usableFromInline
  22. typealias Element = Base.Element
  23. /// The base iterator.
  24. @usableFromInline
  25. private(set) var base: Base
  26. /// Set to `true` when an iterator has been made.
  27. @usableFromInline
  28. let _hasMadeIterator = ManagedAtomic(false)
  29. @inlinable
  30. init(_ base: Base) {
  31. self.base = base
  32. }
  33. @usableFromInline
  34. struct AsyncIterator: AsyncIteratorProtocol {
  35. @usableFromInline
  36. private(set) var base: Base
  37. @inlinable
  38. init(base: Base) {
  39. self.base = base
  40. }
  41. @inlinable
  42. mutating func next() async throws -> Element? {
  43. try await self.base.next()
  44. }
  45. }
  46. @inlinable
  47. func makeAsyncIterator() -> AsyncIterator {
  48. let (exchanged, original) = self._hasMadeIterator.compareExchange(
  49. expected: false,
  50. desired: true,
  51. ordering: .relaxed
  52. )
  53. guard exchanged else {
  54. fatalError("Only one iterator can be made")
  55. }
  56. assert(!original)
  57. return AsyncIterator(base: self.base)
  58. }
  59. }