Skip to content

Commit 665929a

Browse files
paolobarbolinidjc
authored andcommitted
examples: simplify server acceptor
1 parent 59948c6 commit 665929a

File tree

1 file changed

+5
-25
lines changed

1 file changed

+5
-25
lines changed

examples/server.rs

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@
55
//! hyper will automatically use HTTP/2 if a client starts talking HTTP/2,
66
//! otherwise HTTP/1.1 will be used.
77
use async_stream::stream;
8-
use core::task::{Context, Poll};
9-
use futures_util::{future::TryFutureExt, stream::Stream};
8+
use futures_util::future::TryFutureExt;
9+
use hyper::server::accept;
1010
use hyper::service::{make_service_fn, service_fn};
1111
use hyper::{Body, Method, Request, Response, Server, StatusCode};
1212
use rustls::internal::pemfile;
13-
use std::pin::Pin;
1413
use std::vec::Vec;
1514
use std::{env, fs, io, sync};
16-
use tokio::net::{TcpListener, TcpStream};
17-
use tokio_rustls::server::TlsStream;
15+
use tokio::net::TcpListener;
1816
use tokio_rustls::TlsAcceptor;
1917

2018
fn main() {
@@ -70,34 +68,16 @@ async fn run_server() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
7068
yield stream.await;
7169
}
7270
};
71+
let acceptor = accept::from_stream(incoming_tls_stream);
7372
let service = make_service_fn(|_| async { Ok::<_, io::Error>(service_fn(echo)) });
74-
let server = Server::builder(HyperAcceptor {
75-
acceptor: Box::pin(incoming_tls_stream),
76-
})
77-
.serve(service);
73+
let server = Server::builder(acceptor).serve(service);
7874

7975
// Run the future, keep going until an error occurs.
8076
println!("Starting to serve on https://{}.", addr);
8177
server.await?;
8278
Ok(())
8379
}
8480

85-
struct HyperAcceptor<'a> {
86-
acceptor: Pin<Box<dyn Stream<Item = Result<TlsStream<TcpStream>, io::Error>> + 'a>>,
87-
}
88-
89-
impl hyper::server::accept::Accept for HyperAcceptor<'_> {
90-
type Conn = TlsStream<TcpStream>;
91-
type Error = io::Error;
92-
93-
fn poll_accept(
94-
mut self: Pin<&mut Self>,
95-
cx: &mut Context,
96-
) -> Poll<Option<Result<Self::Conn, Self::Error>>> {
97-
Pin::new(&mut self.acceptor).poll_next(cx)
98-
}
99-
}
100-
10181
// Custom echo service, handling two different routes and a
10282
// catch-all 404 responder.
10383
async fn echo(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {

0 commit comments

Comments
 (0)