@@ -20,15 +20,15 @@ impl<G: GenServer> Clone for GenServerHandle<G> {
2020}
2121
2222impl < G : GenServer > GenServerHandle < G > {
23- pub ( crate ) fn new ( mut initial_state : G :: State ) -> Self {
23+ pub ( crate ) fn new ( initial_state : G :: State ) -> Self {
2424 let ( tx, mut rx) = mpsc:: channel :: < GenServerInMsg < G > > ( ) ;
2525 let handle = GenServerHandle { tx } ;
2626 let mut gen_server: G = GenServer :: new ( ) ;
2727 let handle_clone = handle. clone ( ) ;
2828 // Ignore the JoinHandle for now. Maybe we'll use it in the future
2929 let _join_handle = rt:: spawn ( async move {
3030 if gen_server
31- . run ( & handle, & mut rx, & mut initial_state)
31+ . run ( & handle, & mut rx, initial_state)
3232 . await
3333 . is_err ( )
3434 {
@@ -38,7 +38,7 @@ impl<G: GenServer> GenServerHandle<G> {
3838 handle_clone
3939 }
4040
41- pub ( crate ) fn new_blocking ( mut initial_state : G :: State ) -> Self {
41+ pub ( crate ) fn new_blocking ( initial_state : G :: State ) -> Self {
4242 let ( tx, mut rx) = mpsc:: channel :: < GenServerInMsg < G > > ( ) ;
4343 let handle = GenServerHandle { tx } ;
4444 let mut gen_server: G = GenServer :: new ( ) ;
@@ -47,7 +47,7 @@ impl<G: GenServer> GenServerHandle<G> {
4747 let _join_handle = rt:: spawn_blocking ( || {
4848 rt:: block_on ( async move {
4949 if gen_server
50- . run ( & handle, & mut rx, & mut initial_state)
50+ . run ( & handle, & mut rx, initial_state)
5151 . await
5252 . is_err ( )
5353 {
@@ -109,7 +109,7 @@ where
109109 type CastMsg : Send + Sized ;
110110 type OutMsg : Send + Sized ;
111111 type State : Clone + Send ;
112- type Error : Debug ;
112+ type Error : Debug + Send ;
113113
114114 fn new ( ) -> Self ;
115115
@@ -130,23 +130,29 @@ where
130130 & mut self ,
131131 handle : & GenServerHandle < Self > ,
132132 rx : & mut mpsc:: Receiver < GenServerInMsg < Self > > ,
133- state : & mut Self :: State ,
133+ state : Self :: State ,
134134 ) -> impl Future < Output = Result < ( ) , GenServerError > > + Send {
135135 async {
136- if let Err ( err) = self . init ( handle, state) . await {
137- tracing:: error!( "Initialization failed: {err:?}" ) ;
138- return Err ( GenServerError :: Initialization ) ;
136+ match self . init ( handle, state) . await {
137+ Ok ( mut new_state) => {
138+ self . main_loop ( handle, rx, & mut new_state) . await ?;
139+ Ok ( ( ) )
140+ }
141+ Err ( err) => {
142+ tracing:: error!( "Initialization failed: {err:?}" ) ;
143+ Err ( GenServerError :: Initialization )
144+ }
139145 }
140- self . main_loop ( handle, rx, state) . await ?;
141- Ok ( ( ) )
142146 }
143147 }
144148
145149 fn init (
146150 & mut self ,
147- handle : & GenServerHandle < Self > ,
148- state : & mut Self :: State ,
149- ) -> impl Future < Output = Result < ( ) , Self :: Error > > + Send ;
151+ _handle : & GenServerHandle < Self > ,
152+ state : Self :: State ,
153+ ) -> impl Future < Output = Result < Self :: State , Self :: Error > > + Send {
154+ async { Ok ( state) }
155+ }
150156
151157 fn main_loop (
152158 & mut self ,
@@ -267,14 +273,6 @@ mod tests {
267273 Self { }
268274 }
269275
270- async fn init (
271- & mut self ,
272- _handle : & GenServerHandle < Self > ,
273- _state : & mut Self :: State ,
274- ) -> Result < ( ) , Self :: Error > {
275- Ok ( ( ) )
276- }
277-
278276 async fn handle_call (
279277 & mut self ,
280278 _: Self :: CallMsg ,
@@ -314,14 +312,6 @@ mod tests {
314312 Self { }
315313 }
316314
317- async fn init (
318- & mut self ,
319- _handle : & GenServerHandle < Self > ,
320- _state : & mut Self :: State ,
321- ) -> Result < ( ) , Self :: Error > {
322- Ok ( ( ) )
323- }
324-
325315 async fn handle_call (
326316 & mut self ,
327317 message : Self :: CallMsg ,
0 commit comments