From deced08513e9971eeb4165a5c27139e4d9a8daf3 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Thu, 2 Jun 2022 17:28:26 +0200 Subject: Replace with async --- src/server.rs | 42 +++++++++++++----------------------------- 1 file changed, 13 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/server.rs b/src/server.rs index 81ffcd5..b06299c 100644 --- a/src/server.rs +++ b/src/server.rs @@ -23,14 +23,8 @@ pub struct Server { pub login_provider: Box, } -async fn handle_req(req: Request) -> Result { - tracing::debug!("Got request: {:#?}", req); - Ok(Response::ok("Done")?) -} - - -struct Echo; -impl Service for Echo { +struct Connection; +impl Service for Connection { type Response = Response; type Error = anyhow::Error; type Future = Pin> + Send>>; @@ -40,15 +34,16 @@ impl Service for Echo { } fn call(&mut self, req: Request) -> Self::Future { - println!("Got request: {:#?}", req); - let fut = futures::future::ok(Response::ok("Done").unwrap()); - Box::pin(fut) + Box::pin(async move { + println!("Got request: {:#?}", req); + Ok(Response::ok("Done")?) + }) } } -struct Charlie; -impl<'a> Service<&'a AddrStream> for Charlie { - type Response = Echo; +struct Instance; +impl<'a> Service<&'a AddrStream> for Instance { + type Response = Connection; type Error = anyhow::Error; type Future = Pin> + Send>>; @@ -58,8 +53,9 @@ impl<'a> Service<&'a AddrStream> for Charlie { fn call(&mut self, addr: &'a AddrStream) -> Self::Future { println!("{}, {}", addr.remote_addr, addr.local_addr); - let fut = futures::future::ok(Echo); - Box::pin(fut) + Box::pin(async { + Ok(Connection) + }) } } @@ -88,19 +84,7 @@ impl Server { let incoming = AddrIncoming::new("127.0.0.1:4567").await?; - - /*let make_service = tower::service_fn(|addr: &AddrStream| { - tracing::debug!(remote_addr = %addr.remote_addr, local_addr = %addr.local_addr, "accept"); - //let service = tower::ServiceBuilder::new().service_fn(handle_req); - //let service = tower::service_fn(handle_req); - let service = Echo; - futures::future::ok::<_, std::convert::Infallible>(service) - //service - });*/ - - - //println!("{:?}", make_service); - let server = ImapServer::new(incoming).serve(Charlie); + let server = ImapServer::new(incoming).serve(Instance); let _ = server.await?; /*let creds = self.login_provider.login("quentin", "poupou").await?; -- cgit v1.2.3