UncheckedAsyncIteratorSequence.swift 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. public import Synchronization // should be @usableFromInline
  17. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  18. @usableFromInline
  19. /// An `AsyncSequence` which wraps an existing async iterator.
  20. final class UncheckedAsyncIteratorSequence<
  21. Base: AsyncIteratorProtocol
  22. >: AsyncSequence, @unchecked Sendable {
  23. // This is '@unchecked Sendable' because iterators are typically marked as not being Sendable
  24. // to avoid multiple iterators being created. This is done to avoid multiple concurrent consumers
  25. // of a single async sequence.
  26. //
  27. // However, gRPC needs to read the first message in a sequence of inbound request/response parts
  28. // to check how the RPC should be handled. To do this it creates an async iterator and waits for
  29. // the first value and then decides what to do. If it continues processing the RPC it uses this
  30. // wrapper type to turn the iterator back into an async sequence and then drops the iterator on
  31. // the floor so that there is only a single consumer of the original source.
  32. @usableFromInline
  33. typealias Element = Base.Element
  34. /// The base iterator.
  35. @usableFromInline
  36. private(set) var base: Base
  37. /// Set to `true` when an iterator has been made.
  38. @usableFromInline
  39. let _hasMadeIterator = Atomic(false)
  40. @inlinable
  41. init(_ base: Base) {
  42. self.base = base
  43. }
  44. @usableFromInline
  45. struct AsyncIterator: AsyncIteratorProtocol {
  46. @usableFromInline
  47. private(set) var base: Base
  48. @inlinable
  49. init(base: Base) {
  50. self.base = base
  51. }
  52. @inlinable
  53. mutating func next() async throws -> Element? {
  54. try await self.base.next()
  55. }
  56. }
  57. @inlinable
  58. func makeAsyncIterator() -> AsyncIterator {
  59. let (exchanged, original) = self._hasMadeIterator.compareExchange(
  60. expected: false,
  61. desired: true,
  62. ordering: .relaxed
  63. )
  64. guard exchanged else {
  65. fatalError("Only one iterator can be made")
  66. }
  67. assert(!original)
  68. return AsyncIterator(base: self.base)
  69. }
  70. }