1111//===----------------------------------------------------------------------===//
1212
1313import Swift
14- import _Concurrency
1514
1615#if canImport(Darwin)
1716import Darwin
@@ -27,33 +26,25 @@ import WinSDK
2726/// for learning about `distributed actor` isolation, as well as early
2827/// prototyping stages of development where a real system is not necessary yet.
2928@available ( SwiftStdlib 5 . 7 , * )
30- public struct LocalTestingDistributedActorSystem : DistributedActorSystem , @unchecked Sendable {
29+ public final class LocalTestingDistributedActorSystem : DistributedActorSystem , @unchecked Sendable {
3130 public typealias ActorID = LocalTestingActorAddress
3231 public typealias ResultHandler = LocalTestingInvocationResultHandler
3332 public typealias InvocationEncoder = LocalTestingInvocationEncoder
3433 public typealias InvocationDecoder = LocalTestingInvocationDecoder
35- public typealias SerializationRequirement = any Codable
36-
37-
38- @usableFromInline
39- final class _Storage {
34+ public typealias SerializationRequirement = Codable
4035
41- var activeActors : [ ActorID : any DistributedActor ] = [ : ]
42- let activeActorsLock = _Lock ( )
36+ private var activeActors : [ ActorID : any DistributedActor ] = [ : ]
37+ private let activeActorsLock = _Lock ( )
4338
44- var assignedIDs : Set < ActorID > = [ ]
45- let assignedIDsLock = _Lock ( )
46- }
47- let storage : _Storage
48- var idProvider : ActorIDProvider = ActorIDProvider ( )
39+ private var idProvider : ActorIDProvider = ActorIDProvider ( )
40+ private var assignedIDs : Set < ActorID > = [ ]
41+ private let assignedIDsLock = _Lock ( )
4942
50- public init ( ) {
51- storage = . init( )
52- }
43+ public init ( ) { }
5344
5445 public func resolve< Act> ( id: ActorID , as actorType: Act . Type )
5546 throws -> Act ? where Act: DistributedActor , Act. ID == ActorID {
56- guard let anyActor = storage . activeActorsLock. withLock ( { self . storage . activeActors [ id] } ) else {
47+ guard let anyActor = self . activeActorsLock. withLock ( { self . activeActors [ id] } ) else {
5748 throw LocalTestingDistributedActorSystemError ( message: " Unable to locate id ' \( id) ' locally " )
5849 }
5950 guard let actor = anyActor as? Act else {
@@ -65,23 +56,25 @@ public struct LocalTestingDistributedActorSystem: DistributedActorSystem, @unche
6556 public func assignID< Act> ( _ actorType: Act . Type ) -> ActorID
6657 where Act: DistributedActor , Act. ID == ActorID {
6758 let id = self . idProvider. next ( )
68- storage . assignedIDsLock. withLock {
69- self . storage . assignedIDs. insert ( id)
59+ self . assignedIDsLock. withLock {
60+ self . assignedIDs. insert ( id)
7061 }
7162 return id
7263 }
7364
7465 public func actorReady< Act> ( _ actor : Act )
7566 where Act: DistributedActor , Act. ID == ActorID {
76- guard storage . assignedIDsLock. withLock ( { self . storage . assignedIDs. contains ( actor . id) } ) else {
67+ guard self . assignedIDsLock. withLock ( { self . assignedIDs. contains ( actor . id) } ) else {
7768 fatalError ( " Attempted to mark an unknown actor ' \( actor . id) ' ready " )
7869 }
79- self . storage. activeActors [ actor . id] = actor
70+ self . activeActorsLock. withLock {
71+ self . activeActors [ actor . id] = actor
72+ }
8073 }
8174
8275 public func resignID( _ id: ActorID ) {
83- storage . activeActorsLock. withLock {
84- self . storage . activeActors. removeValue ( forKey: id)
76+ self . activeActorsLock. withLock {
77+ self . activeActors. removeValue ( forKey: id)
8578 }
8679 }
8780
@@ -99,7 +92,7 @@ public struct LocalTestingDistributedActorSystem: DistributedActorSystem, @unche
9992 where Act: DistributedActor ,
10093 Act. ID == ActorID ,
10194 Err: Error ,
102- Res: Codable {
95+ Res: SerializationRequirement {
10396 fatalError ( " Attempted to make remote call to \( target) on actor \( actor ) using a local-only actor system " )
10497 }
10598
@@ -115,40 +108,51 @@ public struct LocalTestingDistributedActorSystem: DistributedActorSystem, @unche
115108 fatalError ( " Attempted to make remote call to \( target) on actor \( actor ) using a local-only actor system " )
116109 }
117110
118- @usableFromInline
119- final class ActorIDProvider {
111+ private struct ActorIDProvider {
120112 private var counter : Int = 0
121113 private let counterLock = _Lock ( )
122114
123- @usableFromInline
124115 init ( ) { }
125116
126- func next( ) -> LocalTestingActorAddress {
117+ mutating func next( ) -> LocalTestingActorAddress {
127118 let id : Int = self . counterLock. withLock {
128119 self . counter += 1
129120 return self . counter
130121 }
131- return LocalTestingActorAddress ( id )
122+ return LocalTestingActorAddress ( parse : " \( id ) " )
132123 }
133124 }
134125}
135126
136127@available ( SwiftStdlib 5 . 7 , * )
137- public struct LocalTestingActorAddress : Hashable , Sendable , Codable {
138- public let uuid : String
128+ @ available ( * , deprecated , renamed : " LocalTestingActorID " )
129+ public typealias LocalTestingActorAddress = LocalTestingActorID
139130
140- public init ( _ uuid: Int ) {
141- self . uuid = " \( uuid) "
131+ @available ( SwiftStdlib 5 . 7 , * )
132+ public struct LocalTestingActorID : Hashable , Sendable , Codable {
133+ @available ( * , deprecated, renamed: " id " )
134+ public var address : String {
135+ self . id
136+ }
137+ public let id : String
138+
139+ @available ( * , deprecated, renamed: " init(id:) " )
140+ public init ( parse id: String ) {
141+ self . id = id
142+ }
143+
144+ public init ( id: String ) {
145+ self . id = id
142146 }
143147
144148 public init ( from decoder: Decoder ) throws {
145149 let container = try decoder. singleValueContainer ( )
146- self . uuid = try container. decode ( String . self)
150+ self . id = try container. decode ( String . self)
147151 }
148152
149153 public func encode( to encoder: Encoder ) throws {
150154 var container = encoder. singleValueContainer ( )
151- try container. encode ( self . uuid )
155+ try container. encode ( self . id )
152156 }
153157}
154158
@@ -228,7 +232,7 @@ public struct LocalTestingDistributedActorSystemError: DistributedActorSystemErr
228232// === lock ----------------------------------------------------------------
229233
230234@available ( SwiftStdlib 5 . 7 , * )
231- internal class _Lock {
235+ fileprivate class _Lock {
232236 #if os(iOS) || os(macOS) || os(tvOS) || os(watchOS)
233237 private let underlying : UnsafeMutablePointer < os_unfair_lock >
234238 #elseif os(Windows)
@@ -277,6 +281,7 @@ internal class _Lock {
277281 #endif
278282 }
279283
284+
280285 @discardableResult
281286 func withLock< T> ( _ body: ( ) -> T ) -> T {
282287 #if os(iOS) || os(macOS) || os(tvOS) || os(watchOS)
0 commit comments