@@ -400,9 +400,9 @@ pub struct Router<'r> {
400400
401401 /// Listeners for different message types.
402402 // Has a separate non-async Mutex so it can be used by RouterAsyncListener::drop()
403- // TODO filter by more than just MsgType, maybe have a Map of some sort?
403+ // TODO filter by more than just MsgType
404404 app_listeners :
405- BlockingMutex < RefCell < [ Option < ( MsgType , AppCookie ) > ; MAX_LISTENERS ] > > ,
405+ BlockingMutex < RefCell < Vec < ( MsgType , AppCookie ) , MAX_LISTENERS > > > ,
406406
407407 recv_wakers : WakerPool ,
408408}
@@ -430,8 +430,7 @@ impl<'r> Router<'r> {
430430 ) -> Self {
431431 let inner = RouterInner { stack, lookup } ;
432432
433- let app_listeners =
434- BlockingMutex :: new ( RefCell :: new ( [ const { None } ; MAX_LISTENERS ] ) ) ;
433+ let app_listeners = BlockingMutex :: new ( RefCell :: new ( Vec :: new ( ) ) ) ;
435434
436435 Self {
437436 inner : AsyncMutex :: new ( inner) ,
@@ -542,10 +541,7 @@ impl<'r> Router<'r> {
542541 // Find the matching listener
543542 self . app_listeners . lock ( |a| {
544543 let mut a = a. borrow_mut ( ) ;
545- for e in a. iter_mut ( ) {
546- let Some ( ( t, cookie) ) = e else {
547- continue ;
548- } ;
544+ for ( t, cookie) in a. iter_mut ( ) {
549545 if * t == typ {
550546 // OK unwrap: only set once
551547 let handle = handle. take ( ) . unwrap ( ) ;
@@ -584,36 +580,31 @@ impl<'r> Router<'r> {
584580 let mut a = a. borrow_mut ( ) ;
585581
586582 // Check for existing binds with the same type
587- for bind in a. iter ( ) {
588- if bind. as_ref ( ) . is_some_and ( |( t, _) | * t == typ) {
589- return Err ( Error :: AddrInUse ) ;
590- }
583+ if a. iter ( ) . any ( |( t, _cookie) | * t == typ) {
584+ return Err ( Error :: AddrInUse ) ;
591585 }
592586
593587 // Find a free slot
594- let slot =
595- a. iter_mut ( ) . find ( |e| e. is_none ( ) ) . ok_or ( Error :: NoSpace ) ?;
588+ if a. is_full ( ) {
589+ return Err ( Error :: NoSpace ) ;
590+ }
596591 let cookie = self . recv_wakers . alloc ( ) ?;
597- * slot = Some ( ( typ, cookie) ) ;
592+ let _ = a . push ( ( typ, cookie) ) ;
598593 Ok ( cookie)
599594 } )
600595 }
601596
602- fn app_unbind ( & self , cookie : AppCookie ) -> Result < ( ) > {
597+ fn app_unbind ( & self , cookie : AppCookie ) {
603598 self . app_listeners . lock ( |a| {
604599 let mut a = a. borrow_mut ( ) ;
605- let bind = a. get_mut ( cookie. 0 ) . ok_or ( Error :: BadArgument ) ?;
606600
607- if bind . is_none ( ) {
608- return Err ( Error :: BadArgument ) ;
609- }
601+ let orig = a . len ( ) ;
602+ a . retain ( | ( _t , c ) | * c != cookie ) ;
603+ debug_assert_eq ! ( orig , a . len ( ) + 1 , "One entry removed" ) ;
610604
611- // Clear the bind.
612- * bind = None ;
613605 // No need to wake any waker, unbind only occurs
614606 // on RouterAsyncListener::drop.
615607 self . recv_wakers . remove ( cookie) ;
616- Ok ( ( ) )
617608 } )
618609 }
619610
@@ -948,9 +939,6 @@ impl<'r> mctp::AsyncListener for RouterAsyncListener<'r> {
948939
949940impl Drop for RouterAsyncListener < ' _ > {
950941 fn drop ( & mut self ) {
951- if self . router . app_unbind ( self . cookie ) . is_err ( ) {
952- // should be infallible, cookie should be valid.
953- debug_assert ! ( false , "bad unbind" ) ;
954- }
942+ self . router . app_unbind ( self . cookie )
955943 }
956944}
0 commit comments