How to use swift-atomics to expose non-isolated state of an actor?

I don't think you can do this safely without Exposing the Memory Locations of Class Instance Variables.

ManagedAtomic uses _getUnsafePointerToStoredProperties (swift-atomics/HighLevelTypes.swift.gyb at 919eb1d83e02121cdb434c7bfc1f0c66ef17febe · apple/swift-atomics · GitHub), which you could adjust for the size of the Actor header if you really wanted to (adapt swift/Builtin.swift at 486dc27e1d6736b2b0a7b93f3ffba9bae52d99e8 · apple/swift · GitHub; you can use Unmanaged.passUnretained.toOpaque() in place of Builtin.bridgeToRawPointer(x)). Just using a let currentTime: ManagedAtomic<UInt64> and accepting the pointer indirection is by far going to be the easiest and safest route, though – it's only one additional indirection/two pointers in total.

3 Likes