eraseToStream
and eraseToThrowingStream
unable to handle non-sendable Elements
#43
Replies: 1 comment
-
@BrentMifsud The problem is And so we think that this API we provide should require sendability. If you are targeting iOS 18+ then the good news is you can erase sequences directly now. You can just use If you still deploy to older platforms then I think it is up to you to traffic unsafe sendable items through. Now unfortunately nonisolated(unsafe) let nonSendableAsyncSequence = …
AsyncStream(nonSendableAsyncSequence) // 🛑
nonSendableAsyncSequence.eraseToAsyncStream() // 🛑 ...and maybe it's worth filing a bug with Apple. We should probably conditionally conform extension UncheckedSendable: @retroactive AsyncSequence
where Value: AsyncSequence {
public func makeAsyncIterator() -> Value.AsyncIterator {
value.makeAsyncIterator()
}
} Which will allows you to do the following: AsyncStream(UncheckedSendable(nonSendableAsyncSequence))
UncheckedSendable(nonSendableAsyncSequence).eraseToAsyncStream() Since this isn't a bug in the library I'm going to convert to a discussion. |
Beta Was this translation helpful? Give feedback.
-
Description
Something I noticed today when using NotificationCenter.notifications is that I cant actually use eraseToStream to type erase because
Notification
does not conform to sendable.Apple recommends using a .map or .compactMap to take out the value that you need to send, however I noticed that even if you have stripped out the non-sendable type via a map or compact map, eraseToStream still produces errors in swift 6 and warnings in swift 5
Here is an example of observing background refresh status changes:
The only workaround I have found is to do something like this (rather than using eraseToStream)
Checklist
main
branch of this package.Expected behavior
Since
AsyncStream
andAsyncThrowingStream
are capable of handling non-sendable elements, I believeeraseToStream
anderaseToThrowingStream
should mirror this behavior.Actual behavior
I get a compiler warning (in swift 5) and an error (in swift 6) when using eraseTo(Throwing)Stream on a sequence with non-sendable elements.
Steps to reproduce
You can reproduce it with this code snippet:
swift-concurrency-extras version information
1.2.0
Destination operating system
iOS 18.0
Xcode version information
16.0.0
Swift Compiler version information
Beta Was this translation helpful? Give feedback.
All reactions