PassthroughMessageSequence.swift 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Copyright 2021, 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. #if compiler(>=5.5.2) && canImport(_Concurrency)
  17. /// An ``AsyncSequence`` adapter for a ``PassthroughMessageSource``.`
  18. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  19. @usableFromInline
  20. internal struct PassthroughMessageSequence<Element, Failure: Error>: AsyncSequence {
  21. @usableFromInline
  22. internal typealias Element = Element
  23. @usableFromInline
  24. internal typealias AsyncIterator = Iterator
  25. /// The source of messages in the sequence.
  26. @usableFromInline
  27. internal let _source: PassthroughMessageSource<Element, Failure>
  28. @usableFromInline
  29. internal func makeAsyncIterator() -> Iterator {
  30. return Iterator(storage: self._source)
  31. }
  32. @usableFromInline
  33. internal init(consuming source: PassthroughMessageSource<Element, Failure>) {
  34. self._source = source
  35. }
  36. @usableFromInline
  37. internal struct Iterator: AsyncIteratorProtocol {
  38. @usableFromInline
  39. internal let _storage: PassthroughMessageSource<Element, Failure>
  40. fileprivate init(storage: PassthroughMessageSource<Element, Failure>) {
  41. self._storage = storage
  42. }
  43. @inlinable
  44. internal func next() async throws -> Element? {
  45. return try await self._storage.consumeNextElement()
  46. }
  47. }
  48. }
  49. #endif // compiler(>=5.5.2) && canImport(_Concurrency)