Skip to content
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

Make serve generic over the listener and IO types #2479

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions axum/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
# Unreleased

- **fixed:** Improve `debug_handler` on tuple response types ([#2201])
- **breaking:** Make `serve` generic over the listener and IO types ([#2479])

[#2201]: https://github.com/tokio-rs/axum/pull/2201
[#2479]: https://github.com/tokio-rs/axum/pull/2479

# 0.7.3 (29. December, 2023)

Expand Down
1 change: 1 addition & 0 deletions axum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ rustversion = "1.0.9"
[dev-dependencies]
anyhow = "1.0"
axum-macros = { path = "../axum-macros", version = "0.4.0", features = ["__private"] }
hyper = { version = "1.1.0", features = ["client"] }
quickcheck = "1.0"
quickcheck_macros = "1.0"
reqwest = { version = "0.11.14", default-features = false, features = ["json", "stream", "multipart"] }
Expand Down
5 changes: 3 additions & 2 deletions axum/src/docs/routing/into_make_service_with_connect_info.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use axum::{
serve::IncomingStream,
Router,
};
use tokio::net::TcpListener;

let app = Router::new().route("/", get(handler));

Expand All @@ -49,8 +50,8 @@ struct MyConnectInfo {
// ...
}

impl Connected<IncomingStream<'_>> for MyConnectInfo {
fn connect_info(target: IncomingStream<'_>) -> Self {
impl Connected<IncomingStream<'_, TcpListener>> for MyConnectInfo {
fn connect_info(target: IncomingStream<'_, TcpListener>) -> Self {
MyConnectInfo {
// ...
}
Expand Down
15 changes: 8 additions & 7 deletions axum/src/extract/connect_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,17 @@ where
/// [`Router::into_make_service_with_connect_info`]: crate::routing::Router::into_make_service_with_connect_info
pub trait Connected<T>: Clone + Send + Sync + 'static {
/// Create type holding information about the connection.
fn connect_info(target: T) -> Self;
fn connect_info(stream: T) -> Self;
}

#[cfg(all(feature = "tokio", any(feature = "http1", feature = "http2")))]
const _: () = {
use crate::serve::IncomingStream;
use crate::serve;
use tokio::net::TcpListener;

impl Connected<IncomingStream<'_>> for SocketAddr {
fn connect_info(target: IncomingStream<'_>) -> Self {
target.remote_addr()
impl Connected<serve::IncomingStream<'_, TcpListener>> for SocketAddr {
fn connect_info(stream: serve::IncomingStream<'_, TcpListener>) -> Self {
*stream.remote_addr()
}
}
};
Expand Down Expand Up @@ -264,8 +265,8 @@ mod tests {
value: &'static str,
}

impl Connected<IncomingStream<'_>> for MyConnectInfo {
fn connect_info(_target: IncomingStream<'_>) -> Self {
impl Connected<IncomingStream<'_, TcpListener>> for MyConnectInfo {
fn connect_info(_target: IncomingStream<'_, TcpListener>) -> Self {
Self {
value: "it worked!",
}
Expand Down
7 changes: 4 additions & 3 deletions axum/src/handler/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,13 @@ where
// for `axum::serve(listener, handler)`
#[cfg(all(feature = "tokio", any(feature = "http1", feature = "http2")))]
const _: () = {
use crate::serve::IncomingStream;
use crate::serve;

impl<H, T, S> Service<IncomingStream<'_>> for HandlerService<H, T, S>
impl<H, T, S, L> Service<serve::IncomingStream<'_, L>> for HandlerService<H, T, S>
where
H: Clone,
S: Clone,
L: serve::Listener,
{
type Response = Self;
type Error = Infallible;
Expand All @@ -195,7 +196,7 @@ const _: () = {
Poll::Ready(Ok(()))
}

fn call(&mut self, _req: IncomingStream<'_>) -> Self::Future {
fn call(&mut self, _req: serve::IncomingStream<'_, L>) -> Self::Future {
std::future::ready(Ok(self.clone()))
}
}
Expand Down
9 changes: 6 additions & 3 deletions axum/src/routing/method_routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1227,9 +1227,12 @@ where
// for `axum::serve(listener, router)`
#[cfg(all(feature = "tokio", any(feature = "http1", feature = "http2")))]
const _: () = {
use crate::serve::IncomingStream;
use crate::serve;

impl Service<IncomingStream<'_>> for MethodRouter<()> {
impl<L> Service<serve::IncomingStream<'_, L>> for MethodRouter<()>
where
L: serve::Listener,
{
type Response = Self;
type Error = Infallible;
type Future = std::future::Ready<Result<Self::Response, Self::Error>>;
Expand All @@ -1238,7 +1241,7 @@ const _: () = {
Poll::Ready(Ok(()))
}

fn call(&mut self, _req: IncomingStream<'_>) -> Self::Future {
fn call(&mut self, _req: serve::IncomingStream<'_, L>) -> Self::Future {
std::future::ready(Ok(self.clone()))
}
}
Expand Down
9 changes: 6 additions & 3 deletions axum/src/routing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,9 +430,12 @@ impl Router {
// for `axum::serve(listener, router)`
#[cfg(all(feature = "tokio", any(feature = "http1", feature = "http2")))]
const _: () = {
use crate::serve::IncomingStream;
use crate::serve;

impl Service<IncomingStream<'_>> for Router<()> {
impl<L> Service<serve::IncomingStream<'_, L>> for Router<()>
where
L: serve::Listener,
{
type Response = Self;
type Error = Infallible;
type Future = std::future::Ready<Result<Self::Response, Self::Error>>;
Expand All @@ -441,7 +444,7 @@ const _: () = {
Poll::Ready(Ok(()))
}

fn call(&mut self, _req: IncomingStream<'_>) -> Self::Future {
fn call(&mut self, _req: serve::IncomingStream<'_, L>) -> Self::Future {
std::future::ready(Ok(self.clone()))
}
}
Expand Down
Loading
Loading