use std::{future::poll_fn, marker::PhantomData}; use tower_service::Service as TowerService; use super::NotificationContext; use crate::service::{MaybeSendFuture, RequestContext, Service, ServiceRole}; pub struct TowerHandler { pub service: S, pub info: R::Info, role: PhantomData, } impl TowerHandler { pub fn new(service: S, info: R::Info) -> Self { Self { service, role: PhantomData, info, } } } impl Service for TowerHandler where S: TowerService + Sync + Send + Clone + 'static, S::Error: Into, S::Future: Send, { async fn handle_request( &self, request: R::PeerReq, _context: RequestContext, ) -> Result { let mut service = self.service.clone(); poll_fn(|cx| service.poll_ready(cx)) .await .map_err(Into::into)?; let resp = service.call(request).await.map_err(Into::into)?; Ok(resp) } fn handle_notification( &self, _notification: R::PeerNot, _context: NotificationContext, ) -> impl Future> + MaybeSendFuture + '_ { std::future::ready(Ok(())) } fn get_info(&self) -> R::Info { self.info.clone() } }