Skip to content

Commit dccc3fa

Browse files
authored
Deprecate type-erasing AsyncStream.init (#41)
In Swift 6, we can erase `any AsyncSequence<Element, Failure>` directly.
1 parent e150f55 commit dccc3fa

File tree

3 files changed

+28
-29
lines changed

3 files changed

+28
-29
lines changed

README.md

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -54,35 +54,6 @@ sendability of the underlying value.
5454

5555
The library comes with numerous helper APIs spread across the two Swift stream types:
5656

57-
* There are helpers that erase any `AsyncSequence` conformance to either concrete stream type.
58-
This allows you to treat the stream type as a kind of "type erased" `AsyncSequence`.
59-
60-
For example, suppose you have a dependency client like this:
61-
62-
```swift
63-
struct ScreenshotsClient {
64-
var screenshots: () -> AsyncStream<Void>
65-
}
66-
```
67-
68-
Then you can construct a live implementation that "erases" the
69-
`NotificationCenter.Notifications` async sequence to a stream:
70-
71-
```swift
72-
extension ScreenshotsClient {
73-
static let live = Self(
74-
screenshots: {
75-
NotificationCenter.default
76-
.notifications(named: UIApplication.userDidTakeScreenshotNotification)
77-
.map { _ in }
78-
.eraseToStream() // ⬅️
79-
}
80-
)
81-
}
82-
```
83-
84-
Use `eraseToThrowingStream()` to propagate failures from throwing async sequences.
85-
8657
* Swift 5.9's `makeStream(of:)` functions have been back-ported. It can be handy in tests that need
8758
to override a dependency endpoint that returns a stream:
8859

Sources/ConcurrencyExtras/AsyncStream.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ extension AsyncStream {
5252
/// ```
5353
///
5454
/// - Parameter sequence: An async sequence.
55+
@available(iOS, deprecated: 18, message: "Use 'any AsyncSequence<Element, Never>', instead.")
56+
@available(macOS, deprecated: 15, message: "Use 'any AsyncSequence<Element, Never>', instead.")
57+
@available(tvOS, deprecated: 18, message: "Use 'any AsyncSequence<Element, Never>', instead.")
58+
@available(
59+
watchOS, deprecated: 11, message: "Use 'any AsyncSequence<Element, Never>', instead."
60+
)
5561
public init<S: AsyncSequence>(_ sequence: S) where S.Element == Element, S: Sendable {
5662
let lock = NSLock()
5763
let iterator = UncheckedBox<S.AsyncIterator?>(wrappedValue: nil)
@@ -79,6 +85,12 @@ extension AsyncStream {
7985
extension AsyncSequence {
8086
/// Erases this async sequence to an async stream that produces elements till this sequence
8187
/// terminates (or fails).
88+
@available(iOS, deprecated: 18, message: "Use 'any AsyncSequence<Element, Never>', instead.")
89+
@available(macOS, deprecated: 15, message: "Use 'any AsyncSequence<Element, Never>', instead.")
90+
@available(tvOS, deprecated: 18, message: "Use 'any AsyncSequence<Element, Never>', instead.")
91+
@available(
92+
watchOS, deprecated: 11, message: "Use 'any AsyncSequence<Element, Never>', instead."
93+
)
8294
public func eraseToStream() -> AsyncStream<Element> where Self: Sendable {
8395
AsyncStream(self)
8496
}

Sources/ConcurrencyExtras/AsyncThrowingStream.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ extension AsyncThrowingStream where Failure == Error {
55
/// terminates, rethrowing any failure.
66
///
77
/// - Parameter sequence: An async sequence.
8+
@available(iOS, deprecated: 18, message: "Use 'any AsyncSequence<Element, any Error>', instead.")
9+
@available(
10+
macOS, deprecated: 15, message: "Use 'any AsyncSequence<Element, any Error>', instead."
11+
)
12+
@available(tvOS, deprecated: 18, message: "Use 'any AsyncSequence<Element, any Error>', instead.")
13+
@available(
14+
watchOS, deprecated: 11, message: "Use 'any AsyncSequence<Element, any Error>', instead."
15+
)
816
public init<S: AsyncSequence>(_ sequence: S) where S.Element == Element, S: Sendable {
917
let lock = NSLock()
1018
let iterator = UncheckedBox<S.AsyncIterator?>(wrappedValue: nil)
@@ -34,6 +42,14 @@ extension AsyncThrowingStream where Failure == Error {
3442
extension AsyncSequence {
3543
/// Erases this async sequence to an async throwing stream that produces elements till this
3644
/// sequence terminates, rethrowing any error on failure.
45+
@available(iOS, deprecated: 18, message: "Use 'any AsyncSequence<Element, any Error>', instead.")
46+
@available(
47+
macOS, deprecated: 15, message: "Use 'any AsyncSequence<Element, any Error>', instead."
48+
)
49+
@available(tvOS, deprecated: 18, message: "Use 'any AsyncSequence<Element, any Error>', instead.")
50+
@available(
51+
watchOS, deprecated: 11, message: "Use 'any AsyncSequence<Element, any Error>', instead."
52+
)
3753
public func eraseToThrowingStream() -> AsyncThrowingStream<Element, Error> where Self: Sendable {
3854
AsyncThrowingStream(self)
3955
}

0 commit comments

Comments
 (0)