Sending a sendable but non-Sendable type

I'm trying to use NSImage with Tasks. Nominally that requires it be Sendable, but it is explicitly not Sendable for some reason:

@available(*, unavailable)
extension NSImage : @unchecked Sendable {
}

…even though it is explicitly 'sendable' if used correctly (i.e. not concurrently), and has critical use-cases which require it to be passed between threads. Note that I'm pointing to Apple's own documentation on these points, to emphasise the inconsistencies here.

I'm at a loss to come up with any workarounds which aren't absurd (e.g. saving the image to a file - even if only in-memory - and reconstituting it on the other thread). Since I don't control the type, I can't declare it Sendable. About the best I can think of is silliness like:

struct NSImageFixed: @unchecked Sendable {
    let image: NSImage
}

…but please tell me there's a better way. :slightly_smiling_face:

(I did try searching this forum's archives, but there are so many topics about Sendable that I couldn't find any that aid in this matter, even though surely there must be plenty. I also searched Apple's Dev Forums and found plenty of people hitting the same issue yet no viable solutions)

It’s not Sendable because it’s mutable, like every other mutable reference type. Your wrapper struct is the most correct answer short of [Returned for revision] SE-0414: Region-based isolation

1 Like

With SE-0412 you can use a nonisolated(unsafe) local variable just before passing a value across isolation boundaries to suppress the Sendable check.

5 Likes

Thanks! (for pointing that out and to its authors @John_McCall & @sophia) That's more like what I was hoping for. I think that's a much more measured and reasonable solution to this sort of thing, than polluting the type system (so to speak).

I'm also hopeful that future enhancements in the vein of SE-414: Region based isolation will continue to alleviate some of the concurrency friction. (I don't think SE-414 applies in this particular case, I'm just talking more generally)

1 Like