Skip to content

Commit 8772661

Browse files
committed
lift requirement on anyhow for handler function
1 parent 1609b90 commit 8772661

File tree

3 files changed

+47
-31
lines changed

3 files changed

+47
-31
lines changed

azalea/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ pre-release-replacements = [
1212
]
1313

1414
[dependencies]
15-
anyhow = { workspace = true }
1615
#async-trait = { workspace = true }
1716
azalea-auth = { version = "0.11.0", path = "../azalea-auth" }
1817
azalea-block = { version = "0.11.0", path = "../azalea-block" }
@@ -50,6 +49,7 @@ uuid = { workspace = true }
5049
criterion = { workspace = true }
5150
parking_lot = { workspace = true, features = ["deadlock_detection"] }
5251
rand = { workspace = true }
52+
anyhow = { workspace = true }
5353

5454
[features]
5555
default = ["log", "serde"]

azalea/src/lib.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ use protocol::{resolver::ResolverError, ServerAddress};
4444
use swarm::SwarmBuilder;
4545
use thiserror::Error;
4646

47-
pub type BoxHandleFn<S> =
48-
Box<dyn Fn(Client, azalea_client::Event, S) -> BoxFuture<'static, Result<(), anyhow::Error>>>;
47+
pub type BoxHandleFn<S, R> = Box<dyn Fn(Client, azalea_client::Event, S) -> BoxFuture<'static, R>>;
4948
pub type HandleFn<S, Fut> = fn(Client, azalea_client::Event, S) -> Fut;
5049

5150
#[derive(Error, Debug)]
@@ -74,19 +73,20 @@ pub enum StartError {
7473
/// # Ok(())
7574
/// # }
7675
/// ```
77-
pub struct ClientBuilder<S>
76+
pub struct ClientBuilder<S, R>
7877
where
7978
S: Default + Send + Sync + Clone + Component + 'static,
79+
R: Send + 'static,
8080
{
8181
/// Internally, ClientBuilder is just a wrapper over SwarmBuilder since it's
8282
/// technically just a subset of it so we can avoid duplicating code this
8383
/// way.
84-
swarm: SwarmBuilder<S, swarm::NoSwarmState>,
84+
swarm: SwarmBuilder<S, swarm::NoSwarmState, R, ()>,
8585
}
86-
impl ClientBuilder<NoState> {
86+
impl ClientBuilder<NoState, ()> {
8787
/// Start building a client that can join the world.
8888
#[must_use]
89-
pub fn new() -> ClientBuilder<NoState> {
89+
pub fn new() -> Self {
9090
Self::new_without_plugins()
9191
.add_plugins(DefaultPlugins)
9292
.add_plugins(DefaultBotPlugins)
@@ -116,7 +116,7 @@ impl ClientBuilder<NoState> {
116116
/// # }
117117
/// ```
118118
#[must_use]
119-
pub fn new_without_plugins() -> ClientBuilder<NoState> {
119+
pub fn new_without_plugins() -> Self {
120120
Self {
121121
swarm: SwarmBuilder::new_without_plugins(),
122122
}
@@ -139,19 +139,21 @@ impl ClientBuilder<NoState> {
139139
/// }
140140
/// ```
141141
#[must_use]
142-
pub fn set_handler<S, Fut>(self, handler: HandleFn<S, Fut>) -> ClientBuilder<S>
142+
pub fn set_handler<S, Fut, R>(self, handler: HandleFn<S, Fut>) -> ClientBuilder<S, R>
143143
where
144144
S: Default + Send + Sync + Clone + Component + 'static,
145-
Fut: Future<Output = Result<(), anyhow::Error>> + Send + 'static,
145+
Fut: Future<Output = R> + Send + 'static,
146+
R: Send + 'static,
146147
{
147148
ClientBuilder {
148149
swarm: self.swarm.set_handler(handler),
149150
}
150151
}
151152
}
152-
impl<S> ClientBuilder<S>
153+
impl<S, R> ClientBuilder<S, R>
153154
where
154155
S: Default + Send + Sync + Clone + Component + 'static,
156+
R: Send + 'static,
155157
{
156158
/// Set the client state instead of initializing defaults.
157159
#[must_use]
@@ -206,7 +208,7 @@ where
206208
self.swarm.start_with_default_opts(address, opts).await
207209
}
208210
}
209-
impl Default for ClientBuilder<NoState> {
211+
impl Default for ClientBuilder<NoState, ()> {
210212
fn default() -> Self {
211213
Self::new()
212214
}

azalea/src/swarm/mod.rs

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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>
5159
where
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>
127135
where
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>
175183
where
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>
226237
where
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

502516
pub 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

Comments
 (0)