@@ -47,7 +47,15 @@ pub struct Swarm {
4747}
4848
4949/// Create a new [`Swarm`].
50- pub struct SwarmBuilder < S , SS >
50+ ///
51+ /// The generics of this struct stand for the following:
52+ /// - S: State
53+ /// - SS: Swarm State
54+ /// - R: Return type of the handler
55+ /// - SR: Return type of the swarm handler
56+ ///
57+ /// You shouldn't have to manually set them though, they'll be inferred for you.
58+ pub struct SwarmBuilder < S , SS , R , SR >
5159where
5260 S : Send + Sync + Clone + Component + ' static ,
5361 SS : Default + Send + Sync + Clone + Resource + ' static ,
@@ -61,21 +69,21 @@ where
6169 /// The state for the overall swarm.
6270 pub ( crate ) swarm_state : SS ,
6371 /// The function that's called every time a bot receives an [`Event`].
64- pub ( crate ) handler : Option < BoxHandleFn < S > > ,
72+ pub ( crate ) handler : Option < BoxHandleFn < S , R > > ,
6573 /// The function that's called every time the swarm receives a
6674 /// [`SwarmEvent`].
67- pub ( crate ) swarm_handler : Option < BoxSwarmHandleFn < SS > > ,
75+ pub ( crate ) swarm_handler : Option < BoxSwarmHandleFn < SS , SR > > ,
6876
6977 /// How long we should wait between each bot joining the server. Set to
7078 /// None to have every bot connect at the same time. None is different than
7179 /// a duration of 0, since if a duration is present the bots will wait for
7280 /// the previous one to be ready.
7381 pub ( crate ) join_delay : Option < std:: time:: Duration > ,
7482}
75- impl SwarmBuilder < NoState , NoSwarmState > {
83+ impl SwarmBuilder < NoState , NoSwarmState , ( ) , ( ) > {
7684 /// Start creating the swarm.
7785 #[ must_use]
78- pub fn new ( ) -> SwarmBuilder < NoState , NoSwarmState > {
86+ pub fn new ( ) -> Self {
7987 Self :: new_without_plugins ( )
8088 . add_plugins ( DefaultPlugins )
8189 . add_plugins ( DefaultBotPlugins )
@@ -108,7 +116,7 @@ impl SwarmBuilder<NoState, NoSwarmState> {
108116 /// # }
109117 /// ```
110118 #[ must_use]
111- pub fn new_without_plugins ( ) -> SwarmBuilder < NoState , NoSwarmState > {
119+ pub fn new_without_plugins ( ) -> Self {
112120 SwarmBuilder {
113121 // we create the app here so plugins can add onto it.
114122 // the schedules won't run until [`Self::start`] is called.
@@ -123,7 +131,7 @@ impl SwarmBuilder<NoState, NoSwarmState> {
123131 }
124132}
125133
126- impl < SS > SwarmBuilder < NoState , SS >
134+ impl < SS , R , SR > SwarmBuilder < NoState , SS , R , SR >
127135where
128136 SS : Default + Send + Sync + Clone + Resource + ' static ,
129137{
@@ -154,9 +162,9 @@ where
154162 /// # }
155163 /// ```
156164 #[ must_use]
157- pub fn set_handler < S , Fut > ( self , handler : HandleFn < S , Fut > ) -> SwarmBuilder < S , SS >
165+ pub fn set_handler < S , Fut , Ret > ( self , handler : HandleFn < S , Fut > ) -> SwarmBuilder < S , SS , Ret , SR >
158166 where
159- Fut : Future < Output = Result < ( ) , anyhow :: Error > > + Send + ' static ,
167+ Fut : Future < Output = Ret > + Send + ' static ,
160168 S : Send + Sync + Clone + Component + Default + ' static ,
161169 {
162170 SwarmBuilder {
@@ -171,7 +179,7 @@ where
171179 }
172180}
173181
174- impl < S > SwarmBuilder < S , NoSwarmState >
182+ impl < S , R , SR > SwarmBuilder < S , NoSwarmState , R , SR >
175183where
176184 S : Send + Sync + Clone + Component + ' static ,
177185{
@@ -203,10 +211,13 @@ where
203211 /// }
204212 /// ```
205213 #[ must_use]
206- pub fn set_swarm_handler < SS , Fut > ( self , handler : SwarmHandleFn < SS , Fut > ) -> SwarmBuilder < S , SS >
214+ pub fn set_swarm_handler < SS , Fut , SRet > (
215+ self ,
216+ handler : SwarmHandleFn < SS , Fut > ,
217+ ) -> SwarmBuilder < S , SS , R , SRet >
207218 where
208219 SS : Default + Send + Sync + Clone + Resource + ' static ,
209- Fut : Future < Output = Result < ( ) , anyhow :: Error > > + Send + ' static ,
220+ Fut : Future < Output = SRet > + Send + ' static ,
210221 {
211222 SwarmBuilder {
212223 handler : self . handler ,
@@ -222,10 +233,12 @@ where
222233 }
223234}
224235
225- impl < S , SS > SwarmBuilder < S , SS >
236+ impl < S , SS , R , SR > SwarmBuilder < S , SS , R , SR >
226237where
227238 S : Send + Sync + Clone + Component + ' static ,
228239 SS : Default + Send + Sync + Clone + Resource + ' static ,
240+ R : Send + ' static ,
241+ SR : Send + ' static ,
229242{
230243 /// Add a vec of [`Account`]s to the swarm.
231244 ///
@@ -354,9 +367,10 @@ where
354367 } ;
355368
356369 let address: ServerAddress = default_join_opts. custom_address . clone ( ) . unwrap_or ( address) ;
357- let resolved_address: SocketAddr = match default_join_opts. custom_resolved_address {
358- Some ( resolved_address) => resolved_address,
359- None => resolver:: resolve_address ( & address) . await ?,
370+ let resolved_address = if let Some ( a) = default_join_opts. custom_resolved_address {
371+ a
372+ } else {
373+ resolver:: resolve_address ( & address) . await ?
360374 } ;
361375
362376 let instance_container = Arc :: new ( RwLock :: new ( InstanceContainer :: default ( ) ) ) ;
@@ -476,7 +490,7 @@ where
476490 }
477491}
478492
479- impl Default for SwarmBuilder < NoState , NoSwarmState > {
493+ impl Default for SwarmBuilder < NoState , NoSwarmState , ( ) , ( ) > {
480494 fn default ( ) -> Self {
481495 Self :: new ( )
482496 }
@@ -500,8 +514,8 @@ pub enum SwarmEvent {
500514}
501515
502516pub type SwarmHandleFn < SS , Fut > = fn ( Swarm , SwarmEvent , SS ) -> Fut ;
503- pub type BoxSwarmHandleFn < SS > =
504- Box < dyn Fn ( Swarm , SwarmEvent , SS ) -> BoxFuture < ' static , Result < ( ) , anyhow :: Error > > + Send > ;
517+ pub type BoxSwarmHandleFn < SS , R > =
518+ Box < dyn Fn ( Swarm , SwarmEvent , SS ) -> BoxFuture < ' static , R > + Send > ;
505519
506520/// Make a bot [`Swarm`].
507521///
0 commit comments