@@ -312,8 +312,8 @@ mod main_actor {
312
312
impl < P : Params > EntityHandle < P > {
313
313
pub fn send ( & self ) -> & mpsc:: Sender < entity_actor:: Command < P > > {
314
314
match self {
315
- EntityHandle :: Live { send : sender } => sender ,
316
- EntityHandle :: ShuttingDown { send : sender , .. } => sender ,
315
+ EntityHandle :: Live { send } => send ,
316
+ EntityHandle :: ShuttingDown { send, .. } => send ,
317
317
}
318
318
}
319
319
}
@@ -426,6 +426,9 @@ mod main_actor {
426
426
task
427
427
}
428
428
429
+ /// This function needs to be polled by the owner of the actor state to advance the
430
+ /// entity manager state machine. If it returns a future, that future must be spawned
431
+ /// by the caller.
429
432
#[ must_use = "this function may return a future that must be spawned by the caller" ]
430
433
pub async fn tick ( & mut self ) -> Option < impl Future < Output = ( ) > + Send + ' static > {
431
434
if let Some ( cmd) = self . internal_recv . recv ( ) . await {
@@ -492,6 +495,8 @@ mod main_actor {
492
495
}
493
496
494
497
/// Get or create an entity actor for the given id.
498
+ ///
499
+ /// If this function returns a future, it must be spawned by the caller.
495
500
fn get_or_create (
496
501
& mut self ,
497
502
id : P :: EntityId ,
@@ -501,14 +506,17 @@ mod main_actor {
501
506
) {
502
507
let mut task = None ;
503
508
let handle = self . live . entry ( id. clone ( ) ) . or_insert_with ( || {
504
- if let Some ( ( sender, mut actor) ) = self . pool . pop ( ) {
509
+ if let Some ( ( send, mut actor) ) = self . pool . pop ( ) {
510
+ // Get an actor from the pool of inactive actors and initialize it.
505
511
actor. state . id = id. clone ( ) ;
506
512
actor. state . global = self . state . clone ( ) ;
513
+ // strictly speaking this is not needed, since we reset the state when adding the actor to the pool.
507
514
actor. state . state . reset ( ) ;
508
515
task = Some ( actor. run ( ) ) ;
509
- EntityHandle :: Live { send : sender }
516
+ EntityHandle :: Live { send }
510
517
} else {
511
- let ( sender, recv) = mpsc:: channel ( self . entity_inbox_size ) ;
518
+ // Create a new entity actor and inbox.
519
+ let ( send, recv) = mpsc:: channel ( self . entity_inbox_size ) ;
512
520
let state: entity_actor:: State < P > = entity_actor:: State {
513
521
id : id. clone ( ) ,
514
522
global : self . state . clone ( ) ,
@@ -523,7 +531,7 @@ mod main_actor {
523
531
) ,
524
532
} ;
525
533
task = Some ( actor. run ( ) ) ;
526
- EntityHandle :: Live { send : sender }
534
+ EntityHandle :: Live { send }
527
535
}
528
536
} ) ;
529
537
( handle, task)
0 commit comments