diff --git a/Sources/RandomKit/Types/RandomGenerator/RandomBytesGenerator.swift b/Sources/RandomKit/Types/RandomGenerator/RandomBytesGenerator.swift index 6c21439..1f34e4e 100644 --- a/Sources/RandomKit/Types/RandomGenerator/RandomBytesGenerator.swift +++ b/Sources/RandomKit/Types/RandomGenerator/RandomBytesGenerator.swift @@ -25,8 +25,15 @@ // THE SOFTWARE. // +#if swift(>=3.1) +/// A workaround to make `RandomBytesGenerator` a `Sequence` for Swift 3.1+. +public typealias _RandomBytesGeneratorComposition = RandomGenerator & Sequence +#else +public typealias _RandomBytesGeneratorComposition = RandomGenerator +#endif + /// A type that specializes in generating random bytes in the form of a `Bytes` type. -public protocol RandomBytesGenerator: RandomGenerator { +public protocol RandomBytesGenerator: _RandomBytesGeneratorComposition { /// A type that stores bytes within its own value. associatedtype Bytes @@ -34,8 +41,50 @@ public protocol RandomBytesGenerator: RandomGenerator { /// Returns random `Bytes`. mutating func randomBytes() -> Bytes + #if swift(>=3.1) + /// Returns an iterator over the elements of this sequence. + func makeIterator() -> RandomBytesIterator + #endif + +} + +#if swift(>=3.1) + +/// An iterator over the `Byte`s of a `RandomBytesGenerator` source. +/// +/// ``` +/// func perform(with randomValue: UInt64) -> Bool { +/// ... +/// } +/// +/// for x in Xoroshiro.seeded { +/// guard perform(with: x) else { +/// break +/// } +/// ... +/// } +/// ``` +public struct RandomBytesIterator: IteratorProtocol { + + /// The source generator from which `next()` retrieves values. + public var source: Source + + /// Advances to the next element and returns it. + public mutating func next() -> Source.Bytes? { + return source.randomBytes() + } + } +extension RandomBytesGenerator { + /// Returns an iterator over the elements of this sequence. + public func makeIterator() -> RandomBytesIterator { + return RandomBytesIterator(source: self) + } +} + +#endif + extension RandomBytesGenerator where Bytes == UInt64 { /// Generates a random unsigned 64-bit integer.