AnyStream

public struct AnyStream<Output> : StreamProtocol

A type-erasing stream.

Use AnyStream to wrap a stream whose type has details you don’t want to expose. This is typically the case when returning streams from a function or storing streams in properties.

You can also use AnyStream to create a custom stream by providing a closure for the pollNext method, rather than implementing StreamProtocol directly on a custom type.

  • Declaration

    Swift

    public typealias PollNextFn = (inout Context) -> Poll<Output?>
  • Creates a type-erasing stream implemented by the provided closure.

    var iter = (0..<3).makeIterator()
    var s = AnyStream { _ in
        Poll.ready(iter.next())
    }
    assert(s.next() == 0)
    assert(s.next() == 1)
    assert(s.next() == 2)
    assert(s.next() == nil)
    

    Declaration

    Swift

    @inlinable
    public init(_ pollNext: @escaping PollNextFn)
  • Creates a type-erasing stream to wrap the provided stream.

    This initializer performs a heap allocation unless the wrapped stream is already type-erased.

    Declaration

    Swift

    @inlinable
    public init<S>(_ stream: S) where Output == S.Output, S : StreamProtocol
  • Declaration

    Swift

    @inlinable
    public func pollNext(_ context: inout Context) -> Poll<Output?>