@@ -312,8 +312,8 @@ mod main_actor {
312312 impl < P : Params > EntityHandle < P > {
313313 pub fn send ( & self ) -> & mpsc:: Sender < entity_actor:: Command < P > > {
314314 match self {
315- EntityHandle :: Live { send : sender } => sender ,
316- EntityHandle :: ShuttingDown { send : sender , .. } => sender ,
315+ EntityHandle :: Live { send } => send ,
316+ EntityHandle :: ShuttingDown { send, .. } => send ,
317317 }
318318 }
319319 }
@@ -426,6 +426,9 @@ mod main_actor {
426426 task
427427 }
428428
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.
429432 #[ must_use = "this function may return a future that must be spawned by the caller" ]
430433 pub async fn tick ( & mut self ) -> Option < impl Future < Output = ( ) > + Send + ' static > {
431434 if let Some ( cmd) = self . internal_recv . recv ( ) . await {
@@ -492,6 +495,8 @@ mod main_actor {
492495 }
493496
494497 /// 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.
495500 fn get_or_create (
496501 & mut self ,
497502 id : P :: EntityId ,
@@ -501,14 +506,17 @@ mod main_actor {
501506 ) {
502507 let mut task = None ;
503508 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.
505511 actor. state . id = id. clone ( ) ;
506512 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.
507514 actor. state . state . reset ( ) ;
508515 task = Some ( actor. run ( ) ) ;
509- EntityHandle :: Live { send : sender }
516+ EntityHandle :: Live { send }
510517 } 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 ) ;
512520 let state: entity_actor:: State < P > = entity_actor:: State {
513521 id : id. clone ( ) ,
514522 global : self . state . clone ( ) ,
@@ -523,7 +531,7 @@ mod main_actor {
523531 ) ,
524532 } ;
525533 task = Some ( actor. run ( ) ) ;
526- EntityHandle :: Live { send : sender }
534+ EntityHandle :: Live { send }
527535 }
528536 } ) ;
529537 ( handle, task)
0 commit comments