-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Generalize Connected
implementation to all Listener
s for SocketAddr
#3331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…ddr` The previous implementation implemented the trait `Connected` for `TcpListener` and `TapIo`. This was both needlessly specific *and* blocked other implementers of the `Listener` trait from themselves implementing `Connected`.
25970da
to
33dda5d
Compare
@jplatte the CI passes, so please take another look. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not too familiar with the code, but the proposal sounds reasonable and if it compiles then I guess it's fine 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This maybe got forgotten a bit, I think we can merge this.
Should we write a changeling for this or not since this should allow strictly more things?
Well, this removes the ability to use Sorry about not responding for so long though! |
If people implement it the way this PR does for Specifically the following example works so I might be misunderstanding? Example
use std::future::{ready, Future};
use axum::{
extract::connect_info::Connected,
routing::get,
serve::{IncomingStream, Listener, ListenerExt},
Router,
};
use tokio::net::TcpStream;
#[tokio::main]
async fn main() {
let app = Router::new()
.route("/", get(()))
.into_make_service_with_connect_info::<Address>();
let listener = Listen.tap_io(|_| ());
axum::serve(listener, app).await.unwrap();
}
struct Listen;
#[derive(Debug, Clone)]
struct Address;
impl Listener for Listen {
type Io = TcpStream;
type Addr = Address;
fn accept(&mut self) -> impl Future<Output = (Self::Io, Self::Addr)> + Send {
ready(todo!())
}
fn local_addr(&self) -> tokio::io::Result<Self::Addr> {
todo!()
}
}
impl<L> Connected<IncomingStream<'_, L>> for Address
where
L: Listener<Addr = Address>,
{
fn connect_info(stream: IncomingStream<'_, L>) -> Self {
todo!()
}
} |
Can we have such an impl as a test / in an example (the one that previously stopped compiling, maybe)? If it works, I guess this is fine. |
In the details part of the PR description there are examples. I'm not aware of a scenario that previously compiled but no longer does. If the addr type implements |
I think adding what you have as the examples here (without the |
I've added the test, not sure if the CI failure is related to this PR. |
This is related to the PR:
The rest isn't and will be solved separately. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Last small things, after that the tests should pass.
The previous implementation implemented the trait
Connected
forTcpListener
andTapIo
. This was both needlessly specific and blocked other implementers of theListener
trait from themselves implementingConnected
.This is a revive of #3314 that avoids blocking existing implementations. AFAICT it's 2x2 matrix for (tcplistener, custom listener) x (SocketAddr, custom addr) as shown in this example:
The annoying aspect with types like
SocketAddr
in this situation is that it's not user controlled so they can't implement traits for it. In contrast user controlled custom address types don't have that limitation. However there is still the situation of third party address types that aren't aware ofConnected
those will have to be wrapped I fear. One option would be to provide a trait that third party address types can implement to opt into the default connected impl, but I don't think that's a good idea since it creates more traits to keep track of and third party address types are likely not aware ofConnected
so they won't be aware of such a trait either with high likelihood.