Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UncheckedSendable: AsyncSequence #44

Merged
merged 7 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Sources/ConcurrencyExtras/AsyncStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ extension AsyncStream {
}
}

@available(*, deprecated, message: "Explicitly wrap given sequence in 'UncheckedSendable'.")
public init<S: AsyncSequence>(_ sequence: S) where S.Element == Element {
self.init(UncheckedSendable(sequence))
}

/// An `AsyncStream` that never emits and never completes unless cancelled.
public static var never: Self {
Self { _ in }
Expand All @@ -94,4 +99,9 @@ extension AsyncSequence {
public func eraseToStream() -> AsyncStream<Element> where Self: Sendable {
AsyncStream(self)
}

@available(*, deprecated, message: "Explicitly wrap this sequence in 'UncheckedSendable' before erasing to stream.")
public func eraseToStream() -> AsyncStream<Element> {
AsyncStream(UncheckedSendable(self))
}
}
10 changes: 10 additions & 0 deletions Sources/ConcurrencyExtras/AsyncThrowingStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ extension AsyncThrowingStream where Failure == Error {
}
}

@available(*, deprecated, message: "Explicitly wrap given sequence in 'UncheckedSendable'.")
public init<S: AsyncSequence>(_ sequence: S) where S.Element == Element {
self.init(UncheckedSendable(sequence))
}

/// An `AsyncThrowingStream` that never emits and never completes unless cancelled.
public static var never: Self {
Self { _ in }
Expand Down Expand Up @@ -53,4 +58,9 @@ extension AsyncSequence {
public func eraseToThrowingStream() -> AsyncThrowingStream<Element, Error> where Self: Sendable {
AsyncThrowingStream(self)
}

@available(*, deprecated, message: "Explicitly wrap this sequence in 'UncheckedSendable' before erasing to throwing stream.")
public func eraseToThrowingStream() -> AsyncThrowingStream<Element, Error> {
AsyncThrowingStream(UncheckedSendable(self))
}
}
9 changes: 9 additions & 0 deletions Sources/ConcurrencyExtras/UncheckedSendable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ public struct UncheckedSendable<Value>: @unchecked Sendable {
}
}

extension UncheckedSendable: AsyncSequence where Value: AsyncSequence {
public typealias AsyncIterator = Value.AsyncIterator
public typealias Element = Value.Element

public func makeAsyncIterator() -> AsyncIterator {
value.makeAsyncIterator()
}
}

#if swift(>=5.10)
@available(iOS, deprecated: 9999, message: "Use 'nonisolated(unsafe) let', instead.")@available(
macOS, deprecated: 9999, message: "Use 'nonisolated(unsafe) let', instead."
Expand Down
40 changes: 24 additions & 16 deletions Tests/ConcurrencyExtrasTests/AsyncStreamTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,41 @@

@available(iOS 15, *)
private let sendable: @Sendable () async -> AsyncStream<Void> = {
NotificationCenter.default
.notifications(named: UIApplication.userDidTakeScreenshotNotification)
.map { _ in }
.eraseToStream()
UncheckedSendable(
NotificationCenter.default
.notifications(named: UIApplication.userDidTakeScreenshotNotification)
.map { _ in }
)
.eraseToStream()
}

@available(iOS 15, *)
private let mainActor: @MainActor () -> AsyncStream<Void> = {
NotificationCenter.default
.notifications(named: UIApplication.userDidTakeScreenshotNotification)
.map { _ in }
.eraseToStream()
UncheckedSendable(
NotificationCenter.default
.notifications(named: UIApplication.userDidTakeScreenshotNotification)
.map { _ in }
)
.eraseToStream()
}

@available(iOS 15, *)
private let sendableThrowing: @Sendable () async -> AsyncThrowingStream<Void, Error> = {
NotificationCenter.default
.notifications(named: UIApplication.userDidTakeScreenshotNotification)
.map { _ in }
.eraseToThrowingStream()
UncheckedSendable(
NotificationCenter.default
.notifications(named: UIApplication.userDidTakeScreenshotNotification)
.map { _ in }
)
.eraseToThrowingStream()
}

@available(iOS 15, *)
private let mainActorThrowing: @MainActor () -> AsyncThrowingStream<Void, Error> = {
NotificationCenter.default
.notifications(named: UIApplication.userDidTakeScreenshotNotification)
.map { _ in }
.eraseToThrowingStream()
UncheckedSendable(
NotificationCenter.default
.notifications(named: UIApplication.userDidTakeScreenshotNotification)
.map { _ in }
)
.eraseToThrowingStream()
}
#endif
Loading