tower_hyper_http_body_compat/lib.rs
1//! Adapters between hyper 0.14-1.0, http-body 0.4-1.0, and tower-service 0.3.
2//!
3//! The required release candidates are:
4//!
5//! - hyper 1.0.0-rc.4
6//! - http-body 1.0.0-rc.2
7//!
8//! # Example
9//!
10//! Running an axum `Router` with hyper 1.0:
11//!
12//! ```no_run
13//! # use hyper_1 as hyper;
14//! use axum::{Router, routing::get};
15//! use hyper::server::conn::http1;
16//! use std::net::SocketAddr;
17//! use tokio::net::TcpListener;
18//! use tower_http::trace::TraceLayer;
19//! use tower_hyper_http_body_compat::TowerService03HttpServiceAsHyper1HttpService;
20//!
21//! #[tokio::main]
22//! async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
23//! let app = Router::new()
24//! .route("/", get(|| async { "Hello, World!" }))
25//! // we can still add regular tower middleware
26//! .layer(TraceLayer::new_for_http());
27//!
28//! // `Router` implements tower-service 0.3's `Service` trait. Convert that to something
29//! // that implements hyper 1.0's `Service` trait.
30//! let service = TowerService03HttpServiceAsHyper1HttpService::new(app);
31//!
32//! let addr: SocketAddr = ([127, 0, 0, 1], 8080).into();
33//!
34//! let mut tcp_listener = TcpListener::bind(addr).await?;
35//! loop {
36//! let (tcp_stream, _) = tcp_listener.accept().await?;
37//!
38//! // hyper-util isn't on crates.io yet. Instead depend on it via git
39//! // `hyper-util = { git = "https://github.com/hyperium/hyper-util" }`
40//! let tcp_stream = hyper_util::rt::TokioIo::new(tcp_stream);
41//!
42//! let service = service.clone();
43//! tokio::task::spawn(async move {
44//! if let Err(http_err) = http1::Builder::new()
45//! .keep_alive(true)
46//! .serve_connection(tcp_stream, service)
47//! .await
48//! {
49//! eprintln!("Error while serving HTTP connection: {}", http_err);
50//! }
51//! });
52//! }
53//! }
54//! ```
55//!
56//! Note that this library doesn't require axum. Its supports any [`tower::Service`].
57//!
58//! # Feature flags
59//!
60//! To enable the `Service` adapters you must enable either `http1` or `http2` and `server` or
61//! `client` (i.e. `(http1 || http2) && (server || client)`).
62//!
63//! The `Body` adapters are always enabled.
64
65#![warn(
66 clippy::all,
67 clippy::dbg_macro,
68 clippy::todo,
69 clippy::empty_enum,
70 clippy::enum_glob_use,
71 clippy::mem_forget,
72 clippy::unused_self,
73 clippy::filter_map_next,
74 clippy::needless_continue,
75 clippy::needless_borrow,
76 clippy::match_wildcard_for_single_variants,
77 clippy::if_let_mutex,
78 clippy::mismatched_target_os,
79 clippy::await_holding_lock,
80 clippy::match_on_vec_items,
81 clippy::imprecise_flops,
82 clippy::suboptimal_flops,
83 clippy::lossy_float_literal,
84 clippy::rest_pat_in_fully_bound_structs,
85 clippy::fn_params_excessive_bools,
86 clippy::exit,
87 clippy::inefficient_to_string,
88 clippy::linkedlist,
89 clippy::macro_use_imports,
90 clippy::option_option,
91 clippy::verbose_file_reads,
92 clippy::unnested_or_patterns,
93 clippy::str_to_string,
94 rust_2018_idioms,
95 future_incompatible,
96 nonstandard_style,
97 missing_debug_implementations,
98 missing_docs
99)]
100#![deny(unreachable_pub, private_in_public)]
101#![allow(elided_lifetimes_in_paths, clippy::type_complexity)]
102#![forbid(unsafe_code)]
103#![cfg_attr(docsrs, feature(doc_auto_cfg, doc_cfg))]
104#![cfg_attr(test, allow(clippy::float_cmp))]
105
106macro_rules! cfg_service {
107 ($($item:item)*) => {
108 $(
109 #[cfg(all(
110 any(feature = "http1", feature = "http2"),
111 any(feature = "server", feature = "client")
112 ))]
113 $item
114 )*
115 };
116}
117
118macro_rules! ready {
119 ($e:expr) => {
120 match $e {
121 std::task::Poll::Ready(t) => t,
122 std::task::Poll::Pending => {
123 return std::task::Poll::Pending;
124 }
125 }
126 };
127}
128
129cfg_service! {
130 mod service;
131 mod http_service;
132
133 pub use service::{Hyper1ServiceAsTowerService03Service, TowerService03ServiceAsHyper1Service};
134 pub use http_service::{
135 Hyper1HttpServiceAsTowerService03HttpService, TowerService03HttpServiceAsHyper1HttpService,
136 };
137}
138
139mod body;
140
141pub use body::{HttpBody04ToHttpBody1, HttpBody1ToHttpBody04};
142
143#[cfg(test)]
144mod tests;
145
146pub mod future {
147 //! Future types.
148
149 cfg_service! {
150 pub use crate::http_service::{
151 Hyper1HttpServiceAsTowerService03HttpServiceFuture,
152 TowerService03HttpServiceAsHyper1HttpServiceFuture,
153 };
154 pub use crate::service::TowerService03ServiceAsHyper1ServiceFuture;
155 }
156}