From ebc20216ba67aeb955a9d3b82873e7fc15b70ae6 Mon Sep 17 00:00:00 2001 From: Nico Richard Date: Thu, 19 Jan 2023 10:25:21 -0600 Subject: [PATCH] Swift 5.7 light touch adoption of some new features (#59) * packaging version upgrade * if lets * Add a primary associated type for Generator * Simplify repeated * Cleanup newlines * Add an autoclosure type erased initializer (another way to create constants) --- Package.swift | 2 +- Sources/Genything/AnyGenerator.swift | 7 +++++++ Sources/Genything/Arbitrary/Type/Swift+Arbitrary.swift | 8 ++------ Sources/Genything/Generator.swift | 2 +- Sources/Genything/Operators/Generator+distinct.swift | 2 +- Sources/Genything/Operators/Generator+map.swift | 3 --- Sources/Trickery/Fake+Emails.swift | 2 +- .../Operators/Generator_DistinctTests.swift | 2 +- 8 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Package.swift b/Package.swift index e4943da2..542150ea 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:5.6 +// swift-tools-version:5.7 // The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription diff --git a/Sources/Genything/AnyGenerator.swift b/Sources/Genything/AnyGenerator.swift index 20fa26aa..05cdbbb6 100644 --- a/Sources/Genything/AnyGenerator.swift +++ b/Sources/Genything/AnyGenerator.swift @@ -14,6 +14,13 @@ public struct AnyGenerator: Generator { wrappedNext = next } + /// Creates an `AnyGenerator` which generates elements via the provided autoclosure + /// + /// - Parameter next: A block which returns the next element + public init(_ next: @autoclosure @escaping () -> T) { + wrappedNext = { _ in next() } + } + /// Produces the next element from this generator using the provided random source /// /// - Parameter randomSource: A source of random to be used by upstream generators diff --git a/Sources/Genything/Arbitrary/Type/Swift+Arbitrary.swift b/Sources/Genything/Arbitrary/Type/Swift+Arbitrary.swift index d8172037..96bf28d1 100644 --- a/Sources/Genything/Arbitrary/Type/Swift+Arbitrary.swift +++ b/Sources/Genything/Arbitrary/Type/Swift+Arbitrary.swift @@ -211,13 +211,9 @@ extension LazySequence: Arbitrary where Base: Arbitrary { extension Repeated: Arbitrary where Element: Arbitrary { /// A generator of `Repeated`s where `Element` conforms to `Arbitrary` public static var arbitrary: AnyGenerator { - let constructor: (Element, Int) -> Repeated = { element, count in - repeatElement(element, count: count) - } - - return Element.arbitrary + Element.arbitrary .zip(Int.arbitrary) - .map { t in constructor(t.0, t.1) } + .map(repeatElement(_:count:)) } } diff --git a/Sources/Genything/Generator.swift b/Sources/Genything/Generator.swift index 6acc7fab..9b982fa4 100644 --- a/Sources/Genything/Generator.swift +++ b/Sources/Genything/Generator.swift @@ -3,7 +3,7 @@ /// A Generators typically creates values using a `RandomSource`. If the `RandomSource` is not shared between multiple generators, then they are said to be independent e.g. One generator does not affect the randomness of any other generators. /// /// - Note: The generator protocol has no requirement to be random nor is there any requirement to be stateless. The generator might generate random elements or follow some predetermined pattern. Therefore, sharing a generator between unrelated contexts should be done with care. -public protocol Generator { +public protocol Generator { /// The type of element which this Generator produces associatedtype T /// Produces the next element from this generator using the provided random source diff --git a/Sources/Genything/Operators/Generator+distinct.swift b/Sources/Genything/Operators/Generator+distinct.swift index 86d1754f..cd97d144 100644 --- a/Sources/Genything/Operators/Generator+distinct.swift +++ b/Sources/Genything/Operators/Generator+distinct.swift @@ -50,7 +50,7 @@ private struct FIFODistinctMemory { private mutating func append(_ item: T) { defer { - if let maxSize = maxSize, memory.count > maxSize { + if let maxSize, memory.count > maxSize { memory.removeFirst() } } diff --git a/Sources/Genything/Operators/Generator+map.swift b/Sources/Genything/Operators/Generator+map.swift index 46afbcff..6de8d90c 100644 --- a/Sources/Genything/Operators/Generator+map.swift +++ b/Sources/Genything/Operators/Generator+map.swift @@ -8,9 +8,6 @@ extension Generator { public func map(_ transform: @escaping (T) -> R) -> AnyGenerator { Generators.Map(source: self, transform: transform).eraseToAnyGenerator() } - - - } extension Generators { diff --git a/Sources/Trickery/Fake+Emails.swift b/Sources/Trickery/Fake+Emails.swift index 928148ff..f6e7e2ed 100644 --- a/Sources/Trickery/Fake+Emails.swift +++ b/Sources/Trickery/Fake+Emails.swift @@ -49,7 +49,7 @@ extension Fake { public static func business(_ name: String? = nil) -> AnyGenerator { let nameGen: AnyGenerator = { - if let name = name { + if let name { return Generators.constant(name).eraseToAnyGenerator() } return Fake.BusinessNames.any diff --git a/Tests/GenythingTests/Operators/Generator_DistinctTests.swift b/Tests/GenythingTests/Operators/Generator_DistinctTests.swift index 350a9453..382ca76f 100644 --- a/Tests/GenythingTests/Operators/Generator_DistinctTests.swift +++ b/Tests/GenythingTests/Operators/Generator_DistinctTests.swift @@ -11,7 +11,7 @@ final class Generator_DistinctTests: XCTestCase { var last: Int? // Storage for the last bit we generated testAllSatisfy(bitGenerator) { bit in defer { last = bit } // Store the bit for the next comparison - if let last = last, last == bit { + if let last, last == bit { // If this bit matches the last, the function does not work as expected return false }