//===----------------------------------------------------------------------===// // // This source file is part of the Swift.org open source project // // Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See http://swift.org/LICENSE.txt for license information // See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors // //===----------------------------------------------------------------------===// ////////////////////////////////////////// // FIXME: Workaround for inability to create existentials of protocols // with associated types // This file contains "existentials" for the protocols defined in // Policy.swift. Similar components should usually be defined next to // their respective protocols. /// A type-erased generator. /// /// The generator for `SequenceOf`. Forwards operations to an /// arbitrary underlying generator with the same `Element` type, /// hiding the specifics of the underlying generator type. /// /// See also: `SequenceOf`. public struct GeneratorOf : GeneratorType, SequenceType { /// Construct an instance whose `next()` method calls `nextElement`. public init(_ nextElement: ()->T?) { self._next = nextElement } /// Construct an instance whose `next()` method pulls its results /// from `base`. public init(var _ base: G) { self._next = { base.next() } } /// Advance to the next element and return it, or `nil` if no next /// element exists. /// /// Requires: `next()` has not been applied to a copy of `self` /// since the copy was made, and no preceding call to `self.next()` /// has returned `nil`. public mutating func next() -> T? { return _next() } /// `GeneratorOf` is also a `SequenceType`, so it `generate`\ /// 's a copy of itself public func generate() -> GeneratorOf { return self } let _next: ()->T? } /// A type-erased sequence. /// /// Forwards operations to an arbitrary underlying sequence with the /// same `Element` type, hiding the specifics of the underlying /// sequence type. /// /// See also: `GeneratorOf`. public struct SequenceOf : SequenceType { /// Construct an instance whose `generate()` method forwards to /// `makeUnderlyingGenerator` public init( _ makeUnderlyingGenerator: ()->G ) { _generate = { GeneratorOf(makeUnderlyingGenerator()) } } /// Construct an instance whose `generate()` method forwards to /// that of `base`. public init(_ base: S) { self = SequenceOf({ base.generate() }) } /// Return a *generator* over the elements of this *sequence*. /// /// Complexity: O(1) public func generate() -> GeneratorOf { return _generate() } let _generate: ()->GeneratorOf } internal struct _CollectionOf< IndexType_ : ForwardIndexType, T > : CollectionType { init(startIndex: IndexType_, endIndex: IndexType_, _ subscriptImpl: (IndexType_)->T) { self.startIndex = startIndex self.endIndex = endIndex _subscriptImpl = subscriptImpl } /// Return a *generator* over the elements of this *sequence*. /// /// Complexity: O(1) func generate() -> GeneratorOf { var index = startIndex return GeneratorOf { () -> T? in if _fastPath(index != self.endIndex) { ++index return self._subscriptImpl(index) } return .None } } let startIndex: IndexType_ let endIndex: IndexType_ subscript(i: IndexType_) -> T { return _subscriptImpl(i) } let _subscriptImpl: (IndexType_)->T } /// A type-erased sink. /// /// Forwards operations to an arbitrary underlying sink with the same /// `Element` type, hiding the specifics of the underlying sink type. public struct SinkOf : SinkType { /// Construct an instance whose `put(x)` calls `putElement(x)` public init(_ putElement: (T)->()) { _put = putElement } /// Construct an instance whose `put(x)` calls `base.put(x)` public init(var _ base: S) { _put = { base.put($0) } } /// Write `x` to this sink. public func put(x: T) { _put(x) } let _put: (T)->() }