@@ -15,18 +15,20 @@ import _Concurrency
1515
1616@available ( SwiftStdlib 5 . 6 , * )
1717public protocol ActorTransport : Sendable {
18+ /// The identity used by actors that communicate via this transport
19+ associatedtype Identity : ActorIdentity = AnyActorIdentity
1820
1921 // ==== ---------------------------------------------------------------------
2022 // - MARK: Resolving actors by identity
2123
22- /// Upon decoding of a `AnyActorIdentity ` this function will be called
24+ /// Upon decoding of an `Identity ` this function will be called
2325 /// on the transport that is set un the Decoder's `userInfo` at decoding time.
2426 /// This user info is to be set by the transport, as it receives messages and
2527 /// attempts decoding them. This way, messages received on a specific transport
2628 /// (which may be representing a connection, or general transport mechanism)
2729 /// is able to utilize local knowledge and type information about what kind of
2830 /// identity to attempt to decode.
29- func decodeIdentity( from decoder: Decoder ) throws -> AnyActorIdentity
31+ func decodeIdentity( from decoder: Decoder ) throws -> Identity
3032
3133 /// Resolve a local or remote actor address to a real actor instance, or throw if unable to.
3234 /// The returned value is either a local actor or proxy to a remote actor.
@@ -48,7 +50,7 @@ public protocol ActorTransport: Sendable {
4850 ///
4951 /// Detecting liveness of such remote actors shall be offered / by transport libraries
5052 /// by other means, such as "watching an actor for termination" or similar.
51- func resolve< Act> ( _ identity: AnyActorIdentity , as actorType: Act . Type ) throws -> Act ?
53+ func resolve< Act> ( _ identity: Identity , as actorType: Act . Type ) throws -> Act ?
5254 where Act: DistributedActor
5355
5456 // ==== ---------------------------------------------------------------------
@@ -64,37 +66,63 @@ public protocol ActorTransport: Sendable {
6466 /// E.g. if an actor is created under address `addr1` then immediately invoking
6567 /// `transport.resolve(address: addr1, as: Greeter.self)` MUST return a reference
6668 /// to the same actor.
67- func assignIdentity< Act> ( _ actorType: Act . Type ) -> AnyActorIdentity
69+ func assignIdentity< Act> ( _ actorType: Act . Type ) -> Identity
6870 where Act: DistributedActor
6971
7072 func actorReady< Act> ( _ actor : Act )
7173 where Act: DistributedActor
7274
7375 /// Called during actor deinit/destroy.
74- func resignIdentity( _ id: AnyActorIdentity )
76+ func resignIdentity( _ id: Identity )
77+ }
78+
79+ @available ( SwiftStdlib 5 . 6 , * )
80+ extension ActorTransport {
81+ /// Try to resolve based on a type-erased actor identity.
82+ func resolve< Act> (
83+ anyIdentity identity: AnyActorIdentity , as actorType: Act . Type
84+ ) throws -> Act ? where Act: DistributedActor {
85+ return try resolve ( identity. underlying as! Identity , as: actorType)
86+ }
87+
88+ /// Try to resign based on a type-erased actor identity.
89+ func resignAnyIdentity( _ id: AnyActorIdentity ) {
90+ resignIdentity ( id. underlying as! Identity )
91+ }
92+
93+ func decodeAnyIdentity( from decoder: Decoder ) throws -> AnyActorIdentity {
94+ AnyActorIdentity ( try decodeIdentity ( from: decoder) )
95+ }
96+
97+ func assignAnyIdentity< Act> ( _ actorType: Act . Type ) -> AnyActorIdentity
98+ where Act: DistributedActor {
99+ return AnyActorIdentity ( assignIdentity ( actorType) )
100+ }
75101}
76102
77103@available ( SwiftStdlib 5 . 6 , * )
78104public struct AnyActorTransport : ActorTransport {
105+ public typealias Identity = AnyActorIdentity
106+
79107 let transport : ActorTransport
80108
81109 public init < Transport: ActorTransport > ( _ transport: Transport ) {
82110 self . transport = transport
83111 }
84112
85- public func decodeIdentity( from decoder: Decoder ) throws -> AnyActorIdentity {
86- return try transport. decodeIdentity ( from: decoder)
113+ public func decodeIdentity( from decoder: Decoder ) throws -> Identity {
114+ return try transport. decodeAnyIdentity ( from: decoder)
87115 }
88116
89117 public func resolve< Act> (
90- _ identity: AnyActorIdentity , as actorType: Act . Type
118+ _ identity: Identity , as actorType: Act . Type
91119 ) throws -> Act ? where Act: DistributedActor {
92- return try transport. resolve ( identity, as: actorType)
120+ return try transport. resolve ( anyIdentity : identity, as: actorType)
93121 }
94122
95- public func assignIdentity< Act> ( _ actorType: Act . Type ) -> AnyActorIdentity
123+ public func assignIdentity< Act> ( _ actorType: Act . Type ) -> Identity
96124 where Act: DistributedActor {
97- return transport. assignIdentity ( actorType)
125+ return transport. assignAnyIdentity ( actorType)
98126 }
99127
100128 public func actorReady< Act> ( _ actor : Act )
@@ -103,8 +131,8 @@ public struct AnyActorTransport: ActorTransport {
103131 }
104132
105133 /// Called during actor deinit/destroy.
106- public func resignIdentity( _ id: AnyActorIdentity ) {
107- transport. resignIdentity ( id)
134+ public func resignIdentity( _ id: Identity ) {
135+ transport. resignAnyIdentity ( id)
108136 }
109137}
110138
0 commit comments