@@ -1244,6 +1244,18 @@ impl Mailbox {
1244
1244
)
1245
1245
}
1246
1246
1247
+ /// Bind this message's actor port to this actor's mailbox. This method is
1248
+ /// normally used:
1249
+ /// 1. when we need to intercept a message sent to a handler, and re-route
1250
+ /// that message to the returned receiver;
1251
+ /// 2. mock this message's handler when it is not implemented for this actor
1252
+ /// type, with the returned receiver.
1253
+ pub ( crate ) fn bind_actor_port < M : RemoteMessage > ( & self ) -> ( PortHandle < M > , PortReceiver < M > ) {
1254
+ let ( handle, receiver) = self . open_port ( ) ;
1255
+ handle. bind_actor_port ( ) ;
1256
+ ( handle, receiver)
1257
+ }
1258
+
1247
1259
/// Open a new port with an accumulator. This port accepts A::Update type
1248
1260
/// messages, accumulate them into A::State with the given accumulator.
1249
1261
/// The latest changed state can be received from the returned receiver as
@@ -1372,13 +1384,14 @@ impl Mailbox {
1372
1384
PortRef :: attest ( port_id)
1373
1385
}
1374
1386
1375
- fn bind_to < M : RemoteMessage > ( & self , handle : & PortHandle < M > , port_index : u64 ) {
1387
+ fn bind_to_actor_port < M : RemoteMessage > ( & self , handle : & PortHandle < M > ) {
1376
1388
assert_eq ! (
1377
1389
handle. mailbox. actor_id( ) ,
1378
1390
self . actor_id( ) ,
1379
1391
"port does not belong to mailbox"
1380
1392
) ;
1381
1393
1394
+ let port_index = M :: port ( ) ;
1382
1395
let port_id = self . actor_id ( ) . port_id ( port_index) ;
1383
1396
match self . inner . ports . entry ( port_index) {
1384
1397
Entry :: Vacant ( entry) => {
@@ -1608,10 +1621,13 @@ impl<M: RemoteMessage> PortHandle<M> {
1608
1621
)
1609
1622
}
1610
1623
1611
- /// Bind to a specific port index. This is used by [`actor::Binder`] implementations to
1612
- /// bind actor refs. This is not intended for general use.
1613
- pub fn bind_to ( & self , port_index : u64 ) {
1614
- self . mailbox . bind_to ( self , port_index) ;
1624
+ /// Bind to this message's actor port. This method will panic if the handle
1625
+ /// is already bound.
1626
+ ///
1627
+ /// This is used by [`actor::Binder`] implementations to bind actor refs.
1628
+ /// This is not intended for general use.
1629
+ pub ( crate ) fn bind_actor_port ( & self ) {
1630
+ self . mailbox . bind_to_actor_port ( self ) ;
1615
1631
}
1616
1632
}
1617
1633
@@ -3631,8 +3647,7 @@ mod tests {
3631
3647
actor_id. clone ( ) ,
3632
3648
BoxedMailboxSender :: new ( AsyncLoopForwarder ) ,
3633
3649
) ;
3634
- let ( ret_port, mut ret_rx) = mailbox. open_port :: < Undeliverable < MessageEnvelope > > ( ) ;
3635
- ret_port. bind_to ( Undeliverable :: < MessageEnvelope > :: port ( ) ) ;
3650
+ let ( ret_port, mut ret_rx) = mailbox. bind_actor_port :: < Undeliverable < MessageEnvelope > > ( ) ;
3636
3651
3637
3652
// Create a destination not owned by this mailbox to force
3638
3653
// forwarding.
@@ -3679,9 +3694,8 @@ mod tests {
3679
3694
actor_id. clone ( ) ,
3680
3695
BoxedMailboxSender :: new ( PanickingMailboxSender ) ,
3681
3696
) ;
3682
- let ( undeliverable_tx, mut undeliverable_rx) =
3683
- mailbox. open_port :: < Undeliverable < MessageEnvelope > > ( ) ;
3684
- undeliverable_tx. bind_to ( Undeliverable :: < MessageEnvelope > :: port ( ) ) ;
3697
+ let ( _undeliverable_tx, mut undeliverable_rx) =
3698
+ mailbox. bind_actor_port :: < Undeliverable < MessageEnvelope > > ( ) ;
3685
3699
3686
3700
// Open a local user u64 port.
3687
3701
let ( user_port, mut user_rx) = mailbox. open_port :: < u64 > ( ) ;
0 commit comments