2
0

RPCAsyncSequence.swift 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. /// A type-erasing `AsyncSequence`.
  17. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  18. public struct RPCAsyncSequence<
  19. Element: Sendable,
  20. Failure: Error
  21. >: AsyncSequence, @unchecked Sendable {
  22. // @unchecked Sendable is required because 'any' doesn't support composition with primary
  23. // associated types. (see: https://github.com/swiftlang/swift/issues/63877)
  24. //
  25. // To work around that limitation the 'init' requires that the async sequence being wrapped
  26. // is 'Sendable' but that constraint must be dropped internally. This is safe, the compiler just
  27. // can't prove it.
  28. @usableFromInline
  29. let _wrapped: any AsyncSequence<Element, Failure>
  30. /// Creates an ``RPCAsyncSequence`` by wrapping another `AsyncSequence`.
  31. @inlinable
  32. public init<Source: AsyncSequence<Element, Failure>>(
  33. wrapping other: Source
  34. ) where Source: Sendable {
  35. self._wrapped = other
  36. }
  37. @inlinable
  38. public func makeAsyncIterator() -> AsyncIterator {
  39. AsyncIterator(wrapping: self._wrapped.makeAsyncIterator())
  40. }
  41. public struct AsyncIterator: AsyncIteratorProtocol {
  42. @usableFromInline
  43. private(set) var iterator: any AsyncIteratorProtocol<Element, Failure>
  44. @inlinable
  45. init(
  46. wrapping other: some AsyncIteratorProtocol<Element, Failure>
  47. ) {
  48. self.iterator = other
  49. }
  50. @inlinable
  51. public mutating func next(
  52. isolation actor: isolated (any Actor)?
  53. ) async throws(Failure) -> Element? {
  54. try await self.iterator.next(isolation: `actor`)
  55. }
  56. @inlinable
  57. public mutating func next() async throws -> Element? {
  58. try await self.next(isolation: nil)
  59. }
  60. }
  61. }
  62. @available(*, unavailable)
  63. extension RPCAsyncSequence.AsyncIterator: Sendable {}