From 87726617725e4f95c53b3bc8a573553b04c57563 Mon Sep 17 00:00:00 2001 From: mat Date: Mon, 23 Dec 2024 10:34:44 +0000 Subject: lift requirement on anyhow for handler function --- azalea/src/swarm/mod.rs | 52 +++++++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 19 deletions(-) (limited to 'azalea/src/swarm') diff --git a/azalea/src/swarm/mod.rs b/azalea/src/swarm/mod.rs index c79e6685..18bde3f1 100644 --- a/azalea/src/swarm/mod.rs +++ b/azalea/src/swarm/mod.rs @@ -47,7 +47,15 @@ pub struct Swarm { } /// Create a new [`Swarm`]. -pub struct SwarmBuilder +/// +/// The generics of this struct stand for the following: +/// - S: State +/// - SS: Swarm State +/// - R: Return type of the handler +/// - SR: Return type of the swarm handler +/// +/// You shouldn't have to manually set them though, they'll be inferred for you. +pub struct SwarmBuilder where S: Send + Sync + Clone + Component + 'static, SS: Default + Send + Sync + Clone + Resource + 'static, @@ -61,10 +69,10 @@ where /// The state for the overall swarm. pub(crate) swarm_state: SS, /// The function that's called every time a bot receives an [`Event`]. - pub(crate) handler: Option>, + pub(crate) handler: Option>, /// The function that's called every time the swarm receives a /// [`SwarmEvent`]. - pub(crate) swarm_handler: Option>, + pub(crate) swarm_handler: Option>, /// How long we should wait between each bot joining the server. Set to /// None to have every bot connect at the same time. None is different than @@ -72,10 +80,10 @@ where /// the previous one to be ready. pub(crate) join_delay: Option, } -impl SwarmBuilder { +impl SwarmBuilder { /// Start creating the swarm. #[must_use] - pub fn new() -> SwarmBuilder { + pub fn new() -> Self { Self::new_without_plugins() .add_plugins(DefaultPlugins) .add_plugins(DefaultBotPlugins) @@ -108,7 +116,7 @@ impl SwarmBuilder { /// # } /// ``` #[must_use] - pub fn new_without_plugins() -> SwarmBuilder { + pub fn new_without_plugins() -> Self { SwarmBuilder { // we create the app here so plugins can add onto it. // the schedules won't run until [`Self::start`] is called. @@ -123,7 +131,7 @@ impl SwarmBuilder { } } -impl SwarmBuilder +impl SwarmBuilder where SS: Default + Send + Sync + Clone + Resource + 'static, { @@ -154,9 +162,9 @@ where /// # } /// ``` #[must_use] - pub fn set_handler(self, handler: HandleFn) -> SwarmBuilder + pub fn set_handler(self, handler: HandleFn) -> SwarmBuilder where - Fut: Future> + Send + 'static, + Fut: Future + Send + 'static, S: Send + Sync + Clone + Component + Default + 'static, { SwarmBuilder { @@ -171,7 +179,7 @@ where } } -impl SwarmBuilder +impl SwarmBuilder where S: Send + Sync + Clone + Component + 'static, { @@ -203,10 +211,13 @@ where /// } /// ``` #[must_use] - pub fn set_swarm_handler(self, handler: SwarmHandleFn) -> SwarmBuilder + pub fn set_swarm_handler( + self, + handler: SwarmHandleFn, + ) -> SwarmBuilder where SS: Default + Send + Sync + Clone + Resource + 'static, - Fut: Future> + Send + 'static, + Fut: Future + Send + 'static, { SwarmBuilder { handler: self.handler, @@ -222,10 +233,12 @@ where } } -impl SwarmBuilder +impl SwarmBuilder where S: Send + Sync + Clone + Component + 'static, SS: Default + Send + Sync + Clone + Resource + 'static, + R: Send + 'static, + SR: Send + 'static, { /// Add a vec of [`Account`]s to the swarm. /// @@ -354,9 +367,10 @@ where }; let address: ServerAddress = default_join_opts.custom_address.clone().unwrap_or(address); - let resolved_address: SocketAddr = match default_join_opts.custom_resolved_address { - Some(resolved_address) => resolved_address, - None => resolver::resolve_address(&address).await?, + let resolved_address = if let Some(a) = default_join_opts.custom_resolved_address { + a + } else { + resolver::resolve_address(&address).await? }; let instance_container = Arc::new(RwLock::new(InstanceContainer::default())); @@ -476,7 +490,7 @@ where } } -impl Default for SwarmBuilder { +impl Default for SwarmBuilder { fn default() -> Self { Self::new() } @@ -500,8 +514,8 @@ pub enum SwarmEvent { } pub type SwarmHandleFn = fn(Swarm, SwarmEvent, SS) -> Fut; -pub type BoxSwarmHandleFn = - Box BoxFuture<'static, Result<(), anyhow::Error>> + Send>; +pub type BoxSwarmHandleFn = + Box BoxFuture<'static, R> + Send>; /// Make a bot [`Swarm`]. /// -- cgit v1.2.3