Skip to content

Instantly share code, notes, and snippets.

@T1T4N
Created August 13, 2024 08:10
Show Gist options
  • Save T1T4N/734154a02dc708bfed7fe2fc742f115d to your computer and use it in GitHub Desktop.
Save T1T4N/734154a02dc708bfed7fe2fc742f115d to your computer and use it in GitHub Desktop.
A generic box type for adding unchecked Sendable conformance to a type
@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