@@ -360,15 +360,15 @@ where
360360 pub fn write_with_channel_selection (
361361 & mut self ,
362362 cb : impl FnOnce ( & mut Value ) -> ChannelSelection < Channel > ,
363- ) {
363+ ) -> ChannelSelection < Channel > {
364364 let value = self . antenna . peek ( ) . station . value . write_unchecked ( ) ;
365365 let mut guard = RadioGuard {
366366 channels : Vec :: default ( ) ,
367367 antenna : self . antenna ,
368368 value,
369369 } ;
370370 let channel_selection = cb ( & mut guard. value ) ;
371- let channel = match channel_selection {
371+ let channel = match channel_selection. clone ( ) {
372372 ChannelSelection :: Current => Some ( self . antenna . peek ( ) . channel . clone ( ) ) ,
373373 ChannelSelection :: Silence => None ,
374374 ChannelSelection :: Select ( c) => Some ( c) ,
@@ -379,6 +379,8 @@ where
379379 }
380380 self . antenna . peek ( ) . station . cleanup ( ) ;
381381 }
382+
383+ channel_selection
382384 }
383385
384386 /// Modify the state silently, no component will be notified.
@@ -422,6 +424,24 @@ impl<Channel> ChannelSelection<Channel> {
422424 pub fn silence ( & mut self ) {
423425 * self = Self :: Silence
424426 }
427+
428+ /// Check if it is of type [ChannelSelection::Current]
429+ pub fn is_current ( & self ) -> bool {
430+ matches ! ( self , Self :: Current )
431+ }
432+
433+ /// Check if it is of type [ChannelSelection::Select] and return the channel.
434+ pub fn is_select ( & self ) -> Option < & Channel > {
435+ match self {
436+ Self :: Select ( channel) => Some ( channel) ,
437+ _ => None ,
438+ }
439+ }
440+
441+ /// Check if it is of type [ChannelSelection::Silence]
442+ pub fn is_silence ( & self ) -> bool {
443+ matches ! ( self , Self :: Silence )
444+ }
425445}
426446
427447/// Consume the state and subscribe using the given `channel`
@@ -476,8 +496,9 @@ pub trait DataReducer {
476496
477497pub trait RadioReducer {
478498 type Action ;
499+ type Channel ;
479500
480- fn apply ( & mut self , action : Self :: Action ) ;
501+ fn apply ( & mut self , action : Self :: Action ) -> ChannelSelection < Self :: Channel > ;
481502}
482503
483504impl <
@@ -487,9 +508,10 @@ impl<
487508 > RadioReducer for Radio < Data , Channel >
488509{
489510 type Action = Action ;
511+ type Channel = Channel ;
490512
491- fn apply ( & mut self , action : Action ) {
492- self . write_with_channel_selection ( |data| data. reduce ( action) ) ;
513+ fn apply ( & mut self , action : Action ) -> ChannelSelection < Channel > {
514+ self . write_with_channel_selection ( |data| data. reduce ( action) )
493515 }
494516}
495517
0 commit comments