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 #2941

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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,9 +8,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
# Unreleased

- **breaking:** The tuple and tuple_struct `Path` extractor deserializers now check that the number of parameters matches the tuple length exactly ([#2931])
- **breaking:** Make `serve` generic over the listener and IO types ([#2941])
- **change:** Update minimum rust version to 1.75 ([#2943])

[#2931]: https://github.com/tokio-rs/axum/pull/2931
[#2941]: https://github.com/tokio-rs/axum/pull/2941
[#2943]: https://github.com/tokio-rs/axum/pull/2943

# 0.7.7
Expand Down
1 change: 1 addition & 0 deletions axum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ features = [
[dev-dependencies]
anyhow = "1.0"
axum-macros = { path = "../axum-macros", version = "0.4.1", features = ["__private"] }
hyper = { version = "1.1.0", features = ["client"] }
quickcheck = "1.0"
quickcheck_macros = "1.0"
reqwest = { version = "0.12", 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 @@ -263,8 +264,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().with_state(())))
}
}
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 @@ -486,9 +486,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 @@ -497,7 +500,7 @@ const _: () = {
Poll::Ready(Ok(()))
}

fn call(&mut self, _req: IncomingStream<'_>) -> Self::Future {
fn call(&mut self, _req: serve::IncomingStream<'_, L>) -> Self::Future {
// call `Router::with_state` such that everything is turned into `Route` eagerly
// rather than doing that per request
std::future::ready(Ok(self.clone().with_state(())))
Expand Down
Loading
Loading