Skip to content

Commit

Permalink
Fix swift 6 concurrency warnings (#19)
Browse files Browse the repository at this point in the history
* Fix swift 6 concurrency warnings

* Prefer using enableExperimentalFeature

* Improve batch API

* Add flag for warnings as errors

* Update macOS.yml

* Update Package.swift

* Update windows.yml

* Update ubuntu.yml

* Update ubuntu.yml
  • Loading branch information
0xLeif authored Sep 20, 2024
1 parent 383e99e commit 05cacf7
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 138 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/macOS.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ on:
jobs:
build:
runs-on: macos-latest

steps:
- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: 16.0
- uses: actions/checkout@v3
- name: Build
run: swift build -v
Expand Down
22 changes: 13 additions & 9 deletions .github/workflows/ubuntu.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
# This workflow will build a Swift project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-swift

name: Ubuntu

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
branches: ["**"]

jobs:
build:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v3
- name: Build
run: swift build -v
- name: Run tests
run: swift test -v
- uses: sersoft-gmbh/swifty-linux-action@v3
with:
release-version: 6.0
- uses: actions/checkout@v3
- name: Build for release
run: swift build -v -c release
- name: Test
run: swift test -v
8 changes: 3 additions & 5 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@ name: Windows

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
branches: ["**"]

jobs:
build:
runs-on: windows-latest
steps:
- uses: compnerd/gha-setup-swift@main
with:
branch: swift-5.9-release
tag: 5.9-RELEASE
branch: swift-6.0-release
tag: 6.0-RELEASE

- uses: actions/checkout@v2
- run: swift build
Expand Down
8 changes: 3 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version: 5.6
// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand All @@ -9,18 +9,16 @@ let package = Package(
.iOS(.v13),
.watchOS(.v6),
.macOS(.v10_15),
.tvOS(.v13)
.tvOS(.v13),
.visionOS(.v1)
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "Fork",
targets: ["Fork"]
)
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "Fork"
),
Expand Down
12 changes: 6 additions & 6 deletions Sources/Fork/BatchedForkedArray.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/// Using a single array and a single async function, batch the parallelized work for each value of the array
public struct BatchedForkedArray<Value, Output> {
public struct BatchedForkedArray<Value: Sendable, Output: Sendable>: Sendable {
private let batchedArray: [[Value]]
private let filter: (Value) async throws -> Bool
private let map: (Value) async throws -> Output
private let filter: @Sendable (Value) async throws -> Bool
private let map: @Sendable (Value) async throws -> Output

/// Create a ``BatchedForkedArray`` using a single `Array`
/// - Parameters:
Expand All @@ -13,8 +13,8 @@ public struct BatchedForkedArray<Value, Output> {
public init(
_ array: [Value],
batch: UInt,
filter: @escaping (Value) async throws -> Bool,
map: @escaping (Value) async throws -> Output
filter: @Sendable @escaping (Value) async throws -> Bool,
map: @Sendable @escaping (Value) async throws -> Output
) {
var index: Int = 0
let batchLimit: UInt = max(batch, 1)
Expand Down Expand Up @@ -81,7 +81,7 @@ extension BatchedForkedArray {
public init(
_ array: [Value],
batch: UInt,
map: @escaping (Value) async throws -> Output
map: @Sendable @escaping (Value) async throws -> Output
) {
self.init(array, batch: batch, filter: { _ in true }, map: map)
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Fork/Extensions/Actor+ForkedActor.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
extension Actor {
/// Create a ``ForkedActor`` from the current `Actor`
public func fork(
leftOutput: @escaping (_ actor: Self) async throws -> Void,
rightOutput: @escaping (_ actor: Self) async throws -> Void
leftOutput: @Sendable @escaping (_ actor: Self) async throws -> Void,
rightOutput: @Sendable @escaping (_ actor: Self) async throws -> Void
) -> ForkedActor<Self> {
ForkedActor(
actor: self,
Expand Down
50 changes: 25 additions & 25 deletions Sources/Fork/Extensions/Sequence+BatchedForkedArray.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
extension Sequence {
extension Sequence where Element: Sendable {
/// Create a ``BatchedForkedArray`` from the current `Sequence`
public func batchedFork<Output>(
public func fork<Output: Sendable>(
batch: UInt,
filter: @escaping (Element) async throws -> Bool,
map: @escaping (Element) async throws -> Output
filter: @Sendable @escaping (Element) async throws -> Bool,
map: @Sendable @escaping (Element) async throws -> Output
) -> BatchedForkedArray<Element, Output> {
BatchedForkedArray(
Array(self),
Expand All @@ -14,59 +14,59 @@ extension Sequence {
}

/// Create a ``BatchedForkedArray`` from the current `Sequence`
public func batchedFork<Output>(
public func fork<Output: Sendable>(
batch: UInt,
map: @escaping (Element) async throws -> Output
map: @Sendable @escaping (Element) async throws -> Output
) -> BatchedForkedArray<Element, Output> {
batchedFork(batch: batch, filter: { _ in true }, map: map)
fork(batch: batch, filter: { _ in true }, map: map)
}

/// Create a ``BatchedForkedArray`` from the current `Sequence` and get the Output Array
public func batchedForked<Output>(
public func forked<Output: Sendable>(
batch: UInt,
filter: @escaping (Element) async throws -> Bool,
map: @escaping (Element) async throws -> Output
filter: @Sendable @escaping (Element) async throws -> Bool,
map: @Sendable @escaping (Element) async throws -> Output
) async throws -> [Output] {
try await fork(filter: filter, map: map).output()
}

/// Create a ``BatchedForkedArray`` from the current `Sequence` and get the Output Array
public func batchedForked<Output>(
public func forked<Output: Sendable>(
batch: UInt,
map: @escaping (Element) async throws -> Output
map: @Sendable @escaping (Element) async throws -> Output
) async throws -> [Output] {
try await batchedForked(batch: batch, filter: { _ in true }, map: map)
try await forked(batch: batch, filter: { _ in true }, map: map)
}

/// Returns an array containing the results of mapping the given closure over the sequence’s elements.
public func asyncBatchedMap<Output>(
public func asyncMap<Output: Sendable>(
batch: UInt,
_ transform: @escaping (Element) async throws -> Output
_ transform: @Sendable @escaping (Element) async throws -> Output
) async throws -> [Output] {
try await batchedFork(batch: batch, map: transform).output()
try await fork(batch: batch, map: transform).output()
}

/// Returns an array containing the results, that aren't nil, of mapping the given closure over the sequence’s elements.
public func asyncBatchedCompactMap<Output>(
public func asyncCompactMap<Output: Sendable>(
batch: UInt,
_ transform: @escaping (Element) async throws -> Output?
_ transform: @Sendable @escaping (Element) async throws -> Output?
) async throws -> [Output] {
try await batchedFork(batch: batch, map: transform).output().compactMap { $0 }
try await fork(batch: batch, map: transform).output().compactMap { $0 }
}

/// Returns an array containing only the true results from the given closure over the sequence’s elements.
public func asyncBatchedFilter(
public func asyncFilter(
batch: UInt,
_ isIncluded: @escaping (Element) async throws -> Bool
_ isIncluded: @Sendable @escaping (Element) async throws -> Bool
) async throws -> [Element] {
try await batchedFork(batch: batch, filter: isIncluded, map: identity).output()
try await fork(batch: batch, filter: isIncluded, map: identity).output()
}

/// Calls the given closure for each of the elements in the Sequence. This function uses ``BatchedForkedArray`` and will be parallelized when possible.
public func asyncBatchedForEach(
public func asyncForEach(
batch: UInt,
_ transform: @escaping (Element) async throws -> Void
_ transform: @Sendable @escaping (Element) async throws -> Void
) async throws {
_ = try await asyncBatchedMap(batch: batch, transform)
_ = try await asyncMap(batch: batch, transform)
}
}
34 changes: 17 additions & 17 deletions Sources/Fork/Extensions/Sequence+ForkedArray.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
extension Sequence {
extension Sequence where Element: Sendable {
/// Create a ``ForkedArray`` from the current `Sequence`
public func fork<Output>(
filter: @escaping (Element) async throws -> Bool,
map: @escaping (Element) async throws -> Output
public func fork<Output: Sendable>(
filter: @Sendable @escaping (Element) async throws -> Bool,
map: @Sendable @escaping (Element) async throws -> Output
) -> ForkedArray<Element, Output> {
ForkedArray(
Array(self),
Expand All @@ -12,51 +12,51 @@ extension Sequence {
}

/// Create a ``ForkedArray`` from the current `Sequence`
public func fork<Output>(
map: @escaping (Element) async throws -> Output
public func fork<Output: Sendable>(
map: @Sendable @escaping (Element) async throws -> Output
) -> ForkedArray<Element, Output> {
fork(filter: { _ in true }, map: map)
}

/// Create a ``ForkedArray`` from the current `Sequence` and get the Output Array
public func forked<Output>(
filter: @escaping (Element) async throws -> Bool,
map: @escaping (Element) async throws -> Output
public func forked<Output: Sendable>(
filter: @Sendable @escaping (Element) async throws -> Bool,
map: @Sendable @escaping (Element) async throws -> Output
) async throws -> [Output] {
try await fork(filter: filter, map: map).output()
}

/// Create a ``ForkedArray`` from the current `Sequence` and get the Output Array
public func forked<Output>(
map: @escaping (Element) async throws -> Output
public func forked<Output: Sendable>(
map: @Sendable @escaping (Element) async throws -> Output
) async throws -> [Output] {
try await forked(filter: { _ in true }, map: map)
}

/// Returns an array containing the results of mapping the given closure over the sequence’s elements.
public func asyncMap<Output>(
_ transform: @escaping (Element) async throws -> Output
public func asyncMap<Output: Sendable>(
_ transform: @Sendable @escaping (Element) async throws -> Output
) async throws -> [Output] {
try await fork(map: transform).output()
}

/// Returns an array containing the results, that aren't nil, of mapping the given closure over the sequence’s elements.
public func asyncCompactMap<Output>(
_ transform: @escaping (Element) async throws -> Output?
public func asyncCompactMap<Output: Sendable>(
_ transform: @Sendable @escaping (Element) async throws -> Output?
) async throws -> [Output] {
try await fork(map: transform).output().compactMap { $0 }
}

/// Returns an array containing only the true results from the given closure over the sequence’s elements.
public func asyncFilter(
_ isIncluded: @escaping (Element) async throws -> Bool
_ isIncluded: @Sendable @escaping (Element) async throws -> Bool
) async throws -> [Element] {
try await fork(filter: isIncluded, map: identity).output()
}

/// Calls the given closure for each of the elements in the Sequence. This function uses ``ForkedArray`` and will be parallelized when possible.
public func asyncForEach(
_ transform: @escaping (Element) async throws -> Void
_ transform: @Sendable @escaping (Element) async throws -> Void
) async throws {
_ = try await asyncMap(transform)
}
Expand Down
Loading

0 comments on commit 05cacf7

Please sign in to comment.