Closed ubnt-intrepid closed 4 years ago
The current definition of Handler and the abstraction is based on the older futures-0.1 ecosystem and we need to switch our mind to the new futures and async/.await world.
Handler
futures-0.1
futures
async
.await
A candidate of design (inspired from uvicorn):
#[async_trait] trait App { async fn call(&self, req: &mut Request<'_>) -> io::Result<()>; } struct App(...); #[async_trait] impl App for MyApp { async fn call(&self, req: &mut Request<'_>) -> io::Result<()> { req.reply(StatusCode::CREATED, "hello").await } } struct WebSocketApp(..); #[async_trait] impl App for MyApp { async fn call(&self, req: &mut Request<'_>) -> io::Result<()> { let websocket = req.reply_ws(...).await?; let (reader, writer) = websocket.split(); loop { match reader.read_msg().await? { Some(WsMsg::Ping(data)) => self.pong(data).await?, None => break, } } Ok(()) } }
struct Request<'a> { ... } impl Request<'_> { async fn reply(&mut self, status: StatusCode, reply: impl Reply) -> io::Result<()> { ... } async fn reply_ws(&mut self, status: StatusCode, hdrs: Headers) -> io::Result<WebSocket<'_>> { ... } }
The project was rebooted.
The current definition of
Handler
and the abstraction is based on the olderfutures-0.1
ecosystem and we need to switch our mind to the newfutures
andasync
/.await
world.A candidate of design (inspired from uvicorn):