despite ample enthusiasm, i have struggled to apply ~Copyable
in any productive manner. perhaps i have simply not learned the correct patterns yet.
here’s a noncopyable struct of atomic counters:
import Atomics
struct Counters:~Copyable
{
let foosBarred:UnsafeAtomic<Int>
let barsFooed:UnsafeAtomic<Int>
// many many more atomics
init()
{
self.foosBarred = .create(0)
self.barsFooed = .create(0)
...
}
deinit
{
self.foosBarred.destroy()
self.barsFooed.destroy()
...
}
}
it is owned by an actor:
final
actor ServerLoop
{
private nonisolated
let plugins:Plugins
private nonisolated
let count:Counters
private
var state:State
}
now i need the actor to be able to yield its stored properties as something that is not isolated to the actor:
struct Server:~Copyable
{
let plugins:Plugins
let count:Counters
let state:Server.State
}
extension ServerLoop
{
func withServer(_ body:(borrowing Server) async throws -> Void) async rethrows
{
let server:Server = ???
}
}
although the Server
could never escape or outlive the ServerLoop
, there does not seem to be a way to temporarily pass the Counters
to the closure argument without making Counters
a class
and falling back to reference counting.
indeed, giving up and going back to ARC pretty much describes my experience with ~Copyable
. how can i get ~Copyable
to do something useful here?