@@ -77,8 +77,32 @@ impl RouterProxy {
7777
7878 /// Add a new `(receiver, callback)` pair to the router, and send a wakeup message
7979 /// to the router.
80- pub fn add_typed_route < T > ( & self , receiver : IpcReceiver < T > , mut callback : TypedRouterHandler < T > )
81- where
80+ pub fn add_typed_route < T > (
81+ & self ,
82+ receiver : IpcReceiver < T > ,
83+ mut callback : TypedRouterMultiHandler < T > ,
84+ ) where
85+ T : Serialize + for < ' de > Deserialize < ' de > + ' static ,
86+ {
87+ // Before passing the message on to the callback, turn it into the appropriate type
88+ let modified_callback = move |msg : IpcMessage | {
89+ let typed_message = msg. to :: < T > ( ) ;
90+ callback ( typed_message)
91+ } ;
92+
93+ self . add_route (
94+ receiver. to_opaque ( ) ,
95+ RouterHandler :: Multi ( Box :: new ( modified_callback) ) ,
96+ ) ;
97+ }
98+
99+ /// Add a new `(receiver, callback)` pair to the router, and send a wakeup message
100+ /// to the router.
101+ pub fn add_typed_one_shot_route < T > (
102+ & self ,
103+ receiver : IpcReceiver < T > ,
104+ callback : TypedRouterOneShotHandler < T > ,
105+ ) where
82106 T : Serialize + for < ' de > Deserialize < ' de > + ' static ,
83107 {
84108 // Before passing the message on to the callback, turn it into the appropriate type
@@ -87,7 +111,10 @@ impl RouterProxy {
87111 callback ( typed_message)
88112 } ;
89113
90- self . add_route ( receiver. to_opaque ( ) , Box :: new ( modified_callback) ) ;
114+ self . add_route (
115+ receiver. to_opaque ( ) ,
116+ RouterHandler :: Once ( Box :: new ( modified_callback) ) ,
117+ ) ;
91118 }
92119
93120 /// Send a shutdown message to the router containing a ACK sender,
@@ -218,7 +245,18 @@ impl Router {
218245 } ,
219246 // Event from one of our registered receivers, call callback.
220247 IpcSelectionResult :: MessageReceived ( id, message) => {
221- self . handlers . get_mut ( & id) . unwrap ( ) ( message)
248+ match self . handlers . get_mut ( & id) . unwrap ( ) {
249+ RouterHandler :: Once ( _) => { } ,
250+ RouterHandler :: Multi ( handler) => {
251+ ( handler) ( message) ;
252+ continue ;
253+ } ,
254+ } ;
255+ let RouterHandler :: Once ( handler) = self . handlers . remove ( & id) . unwrap ( )
256+ else {
257+ continue ;
258+ } ;
259+ ( handler) ( message) ;
222260 } ,
223261 IpcSelectionResult :: ChannelClosed ( id) => {
224262 let _ = self . handlers . remove ( & id) . unwrap ( ) ;
@@ -238,7 +276,18 @@ enum RouterMsg {
238276}
239277
240278/// Function to call when a new event is received from the corresponding receiver.
241- pub type RouterHandler = Box < dyn FnMut ( IpcMessage ) + Send > ;
279+ pub type RouterMultiHandler = Box < dyn FnMut ( IpcMessage ) + Send > ;
280+
281+ /// Function to call once when a new event is received from the corresponding receiver.
282+ pub type RouterOneShotHandler = Box < dyn FnOnce ( IpcMessage ) + Send > ;
283+
284+ enum RouterHandler {
285+ Once ( RouterOneShotHandler ) ,
286+ Multi ( RouterMultiHandler ) ,
287+ }
288+
289+ /// Like [RouterMultiHandler] but includes the type that will be passed to the callback
290+ pub type TypedRouterMultiHandler < T > = Box < dyn FnMut ( Result < T , bincode:: Error > ) + Send > ;
242291
243- /// Like [RouterHandler ] but includes the type that will be passed to the callback
244- pub type TypedRouterHandler < T > = Box < dyn FnMut ( Result < T , bincode:: Error > ) + Send > ;
292+ /// Like [RouterOneShotHandler ] but includes the type that will be passed to the callback
293+ pub type TypedRouterOneShotHandler < T > = Box < dyn FnOnce ( Result < T , bincode:: Error > ) + Send > ;
0 commit comments