Skip to content

Commit a38046b

Browse files
authored
doc(socketio/connect): improve doc and code readability
1 parent 1ff43c9 commit a38046b

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

socketioxide/src/handler/connect.rs

+19-15
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
//! io.ns("/", handler);
5353
//! io.ns("/admin", handler);
5454
//! ```
55+
5556
use std::pin::Pin;
5657
use std::sync::Arc;
5758

@@ -66,6 +67,7 @@ pub(crate) type BoxedConnectHandler<A> = Box<dyn ErasedConnectHandler<A>>;
6667

6768
type MiddlewareRes = Result<(), Box<dyn std::fmt::Display + Send>>;
6869
type MiddlewareResFut<'a> = Pin<Box<dyn Future<Output = MiddlewareRes> + Send + 'a>>;
70+
6971
pub(crate) trait ErasedConnectHandler<A: Adapter>: Send + Sync + 'static {
7072
fn call(&self, s: Arc<Socket<A>>, auth: Option<String>);
7173
fn call_middleware<'a>(
@@ -100,7 +102,7 @@ pub trait ConnectMiddleware<A: Adapter, T>: Send + Sync + 'static {
100102
) -> impl Future<Output = MiddlewareRes> + Send;
101103

102104
#[doc(hidden)]
103-
fn phantom(&self) -> std::marker::PhantomData<T> {
105+
fn phantom(&self) -> std::marker::PhantomData<(A, T)> {
104106
std::marker::PhantomData
105107
}
106108
}
@@ -144,6 +146,19 @@ pub trait ConnectHandler<A: Adapter, T>: Send + Sync + 'static {
144146
}
145147
}
146148

149+
/// A layered handler, used to compose a [`ConnectHandler`] with a [`ConnectMiddleware`]
150+
pub struct LayeredConnectHandler<A, H, M, T, T1> {
151+
handler: H,
152+
middleware: M,
153+
phantom: std::marker::PhantomData<(A, T, T1)>,
154+
}
155+
156+
struct ConnectMiddlewareLayer<M, N, T, T1> {
157+
middleware: M,
158+
next: N,
159+
phantom: std::marker::PhantomData<(T, T1)>,
160+
}
161+
147162
impl<A: Adapter, T, H> MakeErasedHandler<H, A, T>
148163
where
149164
H: ConnectHandler<A, T> + Send + Sync + 'static,
@@ -174,12 +189,6 @@ where
174189
}
175190
}
176191

177-
pub struct LayeredConnectHandler<A, H, M, T, T1> {
178-
handler: H,
179-
middleware: M,
180-
phantom: std::marker::PhantomData<(A, T, T1)>,
181-
}
182-
183192
impl<A, H, M, T, T1> LayeredConnectHandler<A, H, M, T, T1>
184193
where
185194
A: Adapter,
@@ -192,7 +201,7 @@ where
192201
pub fn with<N, T2>(
193202
self,
194203
next: N,
195-
) -> LayeredConnectHandler<A, H, ConnectMiddlewareLayer<A, M, N, T1, T2>, T, T2>
204+
) -> LayeredConnectHandler<A, H, impl ConnectMiddleware<A, T1>, T, T2>
196205
where
197206
N: ConnectMiddleware<A, T2> + Send + Sync + 'static,
198207
T2: Send + Sync + 'static,
@@ -208,11 +217,7 @@ where
208217
}
209218
}
210219
}
211-
pub struct ConnectMiddlewareLayer<A, M, N, T, T1> {
212-
middleware: M,
213-
next: N,
214-
phantom: std::marker::PhantomData<(A, T, T1)>,
215-
}
220+
216221
impl<A, H, M, T, T1> ConnectHandler<A, T> for LayeredConnectHandler<A, H, M, T, T1>
217222
where
218223
A: Adapter,
@@ -246,7 +251,7 @@ where
246251
}
247252
}
248253

249-
impl<A, M, N, T, T1> ConnectMiddleware<A, T> for ConnectMiddlewareLayer<A, M, N, T, T1>
254+
impl<A, M, N, T, T1> ConnectMiddleware<A, T> for ConnectMiddlewareLayer<M, N, T, T1>
250255
where
251256
A: Adapter,
252257
M: ConnectMiddleware<A, T> + Send + Sync + 'static,
@@ -348,7 +353,6 @@ macro_rules! impl_middleware_async {
348353
Err(_e) => {
349354
#[cfg(feature = "tracing")]
350355
tracing::error!("Error while extracting data: {}", _e);
351-
//TODO: handle error
352356
return Ok(());
353357
},
354358
};

socketioxide/src/ns.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ impl<A: Adapter> Namespace<A> {
3636
})
3737
}
3838

39-
/// Connects a socket to a namespace
40-
/// A handler is first called to check if the connection is allowed
41-
/// If the handler returns an error, a connect_error packet is sent to the client
42-
/// If the handler returns Ok, a connect packet is sent to the client
39+
/// Connects a socket to a namespace.
4340
///
44-
/// if an error occured the function returns false
41+
/// Middlewares are first called to check if the connection is allowed.
42+
/// * If the handler returns an error, a connect_error packet is sent to the client.
43+
/// * If the handler returns Ok, a connect packet is sent to the client
44+
/// and the handler is called.
4545
pub(crate) async fn connect(
4646
self: Arc<Self>,
4747
sid: Sid,

0 commit comments

Comments
 (0)