service_layer_rs

Trait Service

source
pub trait Service<Request, Response> {
    // Required method
    async fn call(&self, request: Request) -> Response;
}
Expand description

§Example

use service_layer_rs::{FnService, Layer, Service, ServiceBuilder};

struct LogMiddle<S> {
    svc: S,
    name: String,
}

impl<S, Request, Response> Service<Request, Response> for LogMiddle<S>
where
    S: Service<Request, Response>,
{
    async fn call(&self, req: Request) -> Response {
        println!("start {} --->", self.name);
        let resp = self.svc.call(req).await;
        println!("end   {} <---", self.name);
        resp
    }
}

struct LogMiddleLayer {
    log: String,
}

impl<S> Layer<S, LogMiddle<S>> for LogMiddleLayer {
    fn layer(self, svc: S) -> LogMiddle<S> {
        LogMiddle { svc, name: self.log }
    }
}

 let svc = FnService::new(|req: String| async move {
    format!("hello {}", req);
 });
 let svc = ServiceBuilder::new(svc)
    .layer(LogMiddleLayer { log: "test".into() })
    .build();
 svc.call("1".to_string()).await;

Required Methods§

source

async fn call(&self, request: Request) -> Response

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<Request, Response, F, Fut> Service<Request, Response> for FnService<F>
where F: Fn(Request) -> Fut, Fut: Future<Output = Response>,