Skip to content

Instantly share code, notes, and snippets.

@zwaldowski
Created September 21, 2022 17:02
Show Gist options
  • Save zwaldowski/218f1dd11a10899635f0d256121f5d2b to your computer and use it in GitHub Desktop.
Save zwaldowski/218f1dd11a10899635f0d256121f5d2b to your computer and use it in GitHub Desktop.
/// Captures a Cocoa object as a parameter to a `@Sendable` function or closure.
@propertyWrapper
public struct Copy<Wrapped>: @unchecked Sendable where Wrapped: NSCopying {
// from: <https://github.com/apple/swift-evolution/blob/main/proposals/0302-concurrent-value-and-concurrent-closures.md?plain=1#L568>
public let wrappedValue: Wrapped
public init(wrappedValue: Wrapped) {
self.wrappedValue = wrappedValue.copy() as! Wrapped // swiftlint:disable:this force_cast
}
}
extension Copy: Equatable where Wrapped: Equatable {}
extension Copy: Hashable where Wrapped: Hashable {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment