# service-layer-rs A simple alternative to the tower service layer, implemented using async trait, making the code more concise and easier to use. ## Example ```rust use service_layer_rs::util::FnService; use service_layer_rs::{Layer, Service, ServiceBuilder}; use std::convert::Infallible; pub struct LogService { inner: S, name: String, } impl Service for LogService where S: Service, Request: Send + 'static, { type Response = S::Response; type Error = S::Error; async fn call( &self, request: Request, ) -> Result { println!("LogService<{}> start", self.name); let res = self.inner.call(request).await; println!("LogService<{}> end", self.name); res } } pub struct LogLayer(pub String); impl Layer for LogLayer where S: Send + Sync + 'static { type Service = LogService; fn layer(self, inner: S) -> Self::Service { LogService { inner, name: self.0 } } } #[tokio::main] async fn main() { let svc = FnService::new(|request: String| async move { println!("handle: {}", request); Ok::<_, Infallible>(request) }); let svc = ServiceBuilder::service(svc) .layer(LogLayer("Test".to_string())) .build(); let ss = svc.boxed(); let res: Result = ss.call("hello".to_owned()).await; println!("{:?}", res); } ``` ### Dynamic Dispatch ```rust let svc = FnService::new(|request: String| async move { println!("handle: {}", request); Ok::<_, Infallible>(request) }); // Box this service to allow for dynamic dispatch. let svc = ServiceBuilder::new(svc) .layer(LogLayer("Test".to_string())) .build() .boxed(); let res: Result = svc.call("hello".to_owned()).await; println!("{:?}", res); ```