Skip to content

Commit a57935c

Browse files
Voultaphermladedav
andauthored
Generalize Connected implementation to all Listeners for SocketAddr (#3331)
Co-authored-by: David Mládek <[email protected]>
1 parent 76268ae commit a57935c

File tree

1 file changed

+88
-21
lines changed

1 file changed

+88
-21
lines changed

axum/src/extract/connect_info.rs

Lines changed: 88 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@ pub struct IntoMakeServiceWithConnectInfo<S, C> {
3030
_connect_info: PhantomData<fn() -> C>,
3131
}
3232

33-
#[test]
34-
fn traits() {
35-
use crate::test_helpers::*;
36-
assert_send::<IntoMakeServiceWithConnectInfo<(), NotSendSync>>();
37-
}
38-
3933
impl<S, C> IntoMakeServiceWithConnectInfo<S, C> {
4034
pub(crate) fn new(svc: S) -> Self {
4135
Self {
@@ -85,22 +79,13 @@ pub trait Connected<T>: Clone + Send + Sync + 'static {
8579
#[cfg(all(feature = "tokio", any(feature = "http1", feature = "http2")))]
8680
const _: () = {
8781
use crate::serve;
88-
use tokio::net::TcpListener;
8982

90-
impl Connected<serve::IncomingStream<'_, TcpListener>> for SocketAddr {
91-
fn connect_info(stream: serve::IncomingStream<'_, TcpListener>) -> Self {
92-
*stream.remote_addr()
93-
}
94-
}
95-
96-
impl<'a, L, F> Connected<serve::IncomingStream<'a, serve::TapIo<L, F>>> for L::Addr
83+
impl<L> Connected<serve::IncomingStream<'_, L>> for SocketAddr
9784
where
98-
L: serve::Listener,
99-
L::Addr: Clone + Sync + 'static,
100-
F: FnMut(&mut L::Io) + Send + 'static,
85+
L: serve::Listener<Addr = Self>,
10186
{
102-
fn connect_info(stream: serve::IncomingStream<'a, serve::TapIo<L, F>>) -> Self {
103-
stream.remote_addr().clone()
87+
fn connect_info(stream: serve::IncomingStream<'_, L>) -> Self {
88+
*stream.remote_addr()
10489
}
10590
}
10691
};
@@ -234,8 +219,90 @@ where
234219
#[cfg(test)]
235220
mod tests {
236221
use super::*;
237-
use crate::{routing::get, serve::IncomingStream, test_helpers::TestClient, Router};
238-
use tokio::net::TcpListener;
222+
use crate::{
223+
extract::connect_info::Connected, routing::get, serve::IncomingStream, serve::Listener,
224+
test_helpers::TestClient, Router,
225+
};
226+
use tokio::net::{TcpListener, TcpStream};
227+
228+
#[test]
229+
fn into_make_service_traits() {
230+
use crate::test_helpers::*;
231+
assert_send::<IntoMakeServiceWithConnectInfo<(), NotSendSync>>();
232+
}
233+
234+
#[allow(dead_code)]
235+
#[allow(clippy::todo)]
236+
fn connected_traits() {
237+
// Test that the `Connected` trait can be used with custom address and listener types.
238+
239+
fn create_router() -> Router {
240+
todo!()
241+
}
242+
243+
fn tcp_listener() -> TcpListener {
244+
todo!()
245+
}
246+
247+
#[derive(Clone)]
248+
struct CustomAddr(SocketAddr);
249+
250+
impl Connected<IncomingStream<'_, TcpListener>> for CustomAddr {
251+
fn connect_info(_stream: IncomingStream<'_, TcpListener>) -> Self {
252+
todo!()
253+
}
254+
}
255+
256+
impl Connected<IncomingStream<'_, CustomListener>> for CustomAddr {
257+
fn connect_info(_stream: IncomingStream<'_, CustomListener>) -> Self {
258+
todo!()
259+
}
260+
}
261+
262+
struct CustomListener {}
263+
264+
impl Listener for CustomListener {
265+
type Io = TcpStream;
266+
type Addr = SocketAddr;
267+
268+
async fn accept(&mut self) -> (Self::Io, Self::Addr) {
269+
todo!()
270+
}
271+
272+
fn local_addr(&self) -> tokio::io::Result<Self::Addr> {
273+
todo!()
274+
}
275+
}
276+
277+
fn custom_connected() {
278+
let router = create_router();
279+
let _ = crate::serve(
280+
tcp_listener(),
281+
router.into_make_service_with_connect_info::<CustomAddr>(),
282+
);
283+
}
284+
285+
fn custom_listener() {
286+
let router = create_router();
287+
let _ = crate::serve(CustomListener {}, router.into_make_service());
288+
}
289+
290+
fn custom_listener_with_connect() {
291+
let router = create_router();
292+
let _ = crate::serve(
293+
CustomListener {},
294+
router.into_make_service_with_connect_info::<SocketAddr>(),
295+
);
296+
}
297+
298+
fn custom_listener_with_custom_connect() {
299+
let router = create_router();
300+
let _ = crate::serve(
301+
CustomListener {},
302+
router.into_make_service_with_connect_info::<CustomAddr>(),
303+
);
304+
}
305+
}
239306

240307
#[crate::test]
241308
async fn socket_addr() {

0 commit comments

Comments
 (0)