Created
August 13, 2024 08:10
-
-
Save T1T4N/734154a02dc708bfed7fe2fc742f115d to your computer and use it in GitHub Desktop.
A generic box type for adding unchecked Sendable conformance to a type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@dynamicMemberLookup | |
final class UnsafeSendableBox<T>: @unchecked Sendable { | |
let value: T | |
init(_ value: T) { | |
self.value = value | |
} | |
subscript<Value>(dynamicMember keyPath: KeyPath<T, Value>) -> Value { | |
value[keyPath: keyPath] | |
} | |
subscript<Value>(dynamicMember keyPath: ReferenceWritableKeyPath<T, Value>) -> Value { | |
get { value[keyPath: keyPath] } | |
set { value[keyPath: keyPath] = newValue } | |
} | |
} | |
extension UnsafeSendableBox where T: DispatchSourceMemoryPressure { | |
func activate() { | |
value.activate() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment