Yes, you can! As hinted in the proposal, swift-collections now has a UniqueArray and UniqueDeque.
This code would not compile, with a ~Copyable type, you would be forced to use an Optional<Continuation> here.
Yes, you can! As hinted in the proposal, swift-collections now has a UniqueArray and UniqueDeque.
This code would not compile, with a ~Copyable type, you would be forced to use an Optional<Continuation> here.
That was a consideration for the existing UnsafeContinuation and CheckedContinuation types, that one of the primary use cases of continuations is indeed for bridging to foreign APIs, and since we can't really expect foreign APIs to ever be 100% annotated perfectly for the static Swift guarantees they might provide, so there would likely be indefinite need for a fully-dynamically-enforced continuation variant to pass through these interfaces.
Given only a non-Copyable Continuation, you can box a noncopyable value in an object, whose reference would then be Copyable, and furthermore place the value itself in an Optional so that you can dynamically consume it using Optional.take. That's the usual way we recommend interfacing noncopyable values with interfaces that are unfriendly to static ownership checking. One could look at CheckedContinuation as being a prepackaged "box-and-optionalize" type for the noncopyable Continuation. Perhaps there should even be initializers on CheckedContinuation and Continuation to make it easy to construct one by forwarding ownership from the other:
extension CheckedContinuation {
// Take responsibility away from the given `Continuation`, which will become
// dynamically checked.
init(_ continuation: consuming Continuation)
}
extension Continuation {
// Try to take responsibility away from the given `CheckedContinuation`, if
// it is uniquely referenced, and has not already been resumed. May return
// nil if unique ownership of the unresumed continuation cannot be regained.
init?(_ checkedContinuation: consuming CheckedContinuation)
}
Those conversions could reduce friction in adopting Continuation for its stronger guarantees as much as possible, while still making it straightforward to degrade to dynamic checking where it becomes necessary.