Compare commits

...
4 Commits
Author SHA1 Message Date
Yuta Saito 6ae9a051f7 Add support for SWIFTCI_USE_LOCAL_DEPS convention (#311)
To use this package in utils/build-script pipeline
2024-04-10 14:41:45 -07:00
Franz Busch 46b4464735 Fix some strict concurrency warnings (#310)
# Motivation

There were a few new strict concurrency warnings that this PR fixes.
2024-04-04 20:46:16 +01:00
Eric Horacek d162617838 Depend on specific products from swift-collections (#307)
* Only depend on OrderedCollections from swift-collections

Before this change, once consumers update to SwiftCollections 1.1.0 they'll pull in all of the new collection dependencies (HashTree, BitCollections, etc.). In our case, this increased our binary size by ~1MB. To fix this, we switch to only depending on what we need.

* Add DequeModule dependency
2024-02-28 12:47:12 +00:00
leogdion 14729ea7f9 Update README.md (#306)
Missing Version Number
2024-01-24 11:28:26 +00:00
6 changed files with 59 additions and 25 deletions

No files matched your search

+15 -5
View File
@@ -13,14 +13,13 @@ let package = Package(
products: [
.library(name: "AsyncAlgorithms", targets: ["AsyncAlgorithms"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-collections.git", from: "1.0.4"),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
],
targets: [
.target(
name: "AsyncAlgorithms",
dependencies: [.product(name: "Collections", package: "swift-collections")],
dependencies: [
.product(name: "OrderedCollections", package: "swift-collections"),
.product(name: "DequeModule", package: "swift-collections"),
],
swiftSettings: [
.enableExperimentalFeature("StrictConcurrency=complete"),
]
@@ -49,3 +48,14 @@ let package = Package(
),
]
)
if Context.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
package.dependencies += [
.package(url: "https://github.com/apple/swift-collections.git", from: "1.1.0"),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
]
} else {
package.dependencies += [
.package(path: "../swift-collections"),
]
}
+1 -1
View File
@@ -67,7 +67,7 @@ To use the `AsyncAlgorithms` library in a SwiftPM project,
add the following line to the dependencies in your `Package.swift` file:
```swift
.package(url: "https://github.com/apple/swift-async-algorithms"),
.package(url: "https://github.com/apple/swift-async-algorithms", from: "1.0.0"),
```
Include `"AsyncAlgorithms"` as a dependency for your executable target:
@@ -16,16 +16,19 @@ struct BoundedBufferStateMachine<Base: AsyncSequence> {
typealias SuspendedProducer = UnsafeContinuation<Void, Never>
typealias SuspendedConsumer = UnsafeContinuation<Result<Base.Element, Error>?, Never>
// We are using UnsafeTransfer here since we have to get the elements from the task
// into the consumer task. This is a transfer but we cannot prove this to the compiler at this point
// since next is not marked as transferring the return value.
fileprivate enum State {
case initial(base: Base)
case buffering(
task: Task<Void, Never>,
buffer: Deque<Result<Element, Error>>,
buffer: Deque<Result<UnsafeTransfer<Element>, Error>>,
suspendedProducer: SuspendedProducer?,
suspendedConsumer: SuspendedConsumer?
)
case modifying
case finished(buffer: Deque<Result<Element, Error>>)
case finished(buffer: Deque<Result<UnsafeTransfer<Element>, Error>>)
}
private var state: State
@@ -139,7 +142,7 @@ struct BoundedBufferStateMachine<Base: AsyncSequence> {
// we have to stack the new element or suspend the producer if the buffer is full
precondition(buffer.count < limit, "Invalid state. The buffer should be available for stacking a new element.")
self.state = .modifying
buffer.append(.success(element))
buffer.append(.success(.init(element)))
self.state = .buffering(task: task, buffer: buffer, suspendedProducer: nil, suspendedConsumer: nil)
return .none
@@ -218,7 +221,7 @@ struct BoundedBufferStateMachine<Base: AsyncSequence> {
self.state = .modifying
let result = buffer.popFirst()!
self.state = .buffering(task: task, buffer: buffer, suspendedProducer: nil, suspendedConsumer: nil)
return .returnResult(producerContinuation: suspendedProducer, result: result)
return .returnResult(producerContinuation: suspendedProducer, result: result.map { $0.wrapped })
case .buffering(_, _, _, .some):
preconditionFailure("Invalid states. There is already a suspended consumer.")
@@ -233,7 +236,7 @@ struct BoundedBufferStateMachine<Base: AsyncSequence> {
self.state = .modifying
let result = buffer.popFirst()!
self.state = .finished(buffer: buffer)
return .returnResult(producerContinuation: nil, result: result)
return .returnResult(producerContinuation: nil, result: result.map { $0.wrapped })
}
}
@@ -257,7 +260,7 @@ struct BoundedBufferStateMachine<Base: AsyncSequence> {
self.state = .modifying
let result = buffer.popFirst()!
self.state = .buffering(task: task, buffer: buffer, suspendedProducer: nil, suspendedConsumer: nil)
return .returnResult(producerContinuation: suspendedProducer, result: result)
return .returnResult(producerContinuation: suspendedProducer, result: result.map { $0.wrapped })
case .buffering(_, _, _, .some):
preconditionFailure("Invalid states. There is already a suspended consumer.")
@@ -272,7 +275,7 @@ struct BoundedBufferStateMachine<Base: AsyncSequence> {
self.state = .modifying
let result = buffer.popFirst()!
self.state = .finished(buffer: buffer)
return .returnResult(producerContinuation: nil, result: result)
return .returnResult(producerContinuation: nil, result: result.map { $0.wrapped })
}
}
@@ -21,15 +21,18 @@ struct UnboundedBufferStateMachine<Base: AsyncSequence> {
case bufferingOldest(Int)
}
// We are using UnsafeTransfer here since we have to get the elements from the task
// into the consumer task. This is a transfer but we cannot prove this to the compiler at this point
// since next is not marked as transferring the return value.
fileprivate enum State {
case initial(base: Base)
case buffering(
task: Task<Void, Never>,
buffer: Deque<Result<Element, Error>>,
buffer: Deque<Result<UnsafeTransfer<Element>, Error>>,
suspendedConsumer: SuspendedConsumer?
)
case modifying
case finished(buffer: Deque<Result<Element, Error>>)
case finished(buffer: Deque<Result<UnsafeTransfer<Element>, Error>>)
}
private var state: State
@@ -84,15 +87,15 @@ struct UnboundedBufferStateMachine<Base: AsyncSequence> {
self.state = .modifying
switch self.policy {
case .unlimited:
buffer.append(.success(element))
buffer.append(.success(.init(element)))
case .bufferingNewest(let limit):
if buffer.count >= limit {
_ = buffer.popFirst()
}
buffer.append(.success(element))
buffer.append(.success(.init(element)))
case .bufferingOldest(let limit):
if buffer.count < limit {
buffer.append(.success(element))
buffer.append(.success(.init(element)))
}
}
self.state = .buffering(task: task, buffer: buffer, suspendedConsumer: nil)
@@ -170,7 +173,7 @@ struct UnboundedBufferStateMachine<Base: AsyncSequence> {
self.state = .modifying
let result = buffer.popFirst()!
self.state = .buffering(task: task, buffer: buffer, suspendedConsumer: nil)
return .returnResult(result)
return .returnResult(result.map { $0.wrapped })
case .modifying:
preconditionFailure("Invalid state.")
@@ -182,7 +185,7 @@ struct UnboundedBufferStateMachine<Base: AsyncSequence> {
self.state = .modifying
let result = buffer.popFirst()!
self.state = .finished(buffer: buffer)
return .returnResult(result)
return .returnResult(result.map { $0.wrapped })
}
}
@@ -208,7 +211,7 @@ struct UnboundedBufferStateMachine<Base: AsyncSequence> {
self.state = .modifying
let result = buffer.popFirst()!
self.state = .buffering(task: task, buffer: buffer, suspendedConsumer: nil)
return .resumeConsumer(result)
return .resumeConsumer(result.map { $0.wrapped })
case .modifying:
preconditionFailure("Invalid state.")
@@ -220,7 +223,7 @@ struct UnboundedBufferStateMachine<Base: AsyncSequence> {
self.state = .modifying
let result = buffer.popFirst()!
self.state = .finished(buffer: buffer)
return .resumeConsumer(result)
return .resumeConsumer(result.map { $0.wrapped })
}
}
@@ -251,3 +254,5 @@ struct UnboundedBufferStateMachine<Base: AsyncSequence> {
extension UnboundedBufferStateMachine: Sendable where Base: Sendable { }
extension UnboundedBufferStateMachine.State: Sendable where Base: Sendable { }
@@ -10,9 +10,6 @@
//===----------------------------------------------------------------------===//
import OrderedCollections
// NOTE: this is only marked as unchecked since the swift-collections tag is before auditing for Sendable
extension OrderedSet: @unchecked Sendable where Element: Sendable { }
struct ChannelStateMachine<Element: Sendable, Failure: Error>: Sendable {
private struct SuspendedProducer: Hashable, Sendable {
let id: UInt64
@@ -0,0 +1,19 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Async Algorithms open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
/// A wrapper struct to unconditionally to transfer an non-Sendable value.
struct UnsafeTransfer<Element>: @unchecked Sendable {
let wrapped: Element
init(_ wrapped: Element) {
self.wrapped = wrapped
}
}