[−][src]Trait kayrx::service::Service
An asynchronous function from Request
to a Response
.
Service
represents a service that represanting interation, taking requests and giving back
replies. You can think about service as a function with one argument and result as a return
type. In general form it looks like async fn(Req) -> Result<Res, Err>
. Service
trait just generalizing form of this function. Each parameter described as an assotiated type.
Services provides a symmetric and uniform API, same abstractions represents
clients and servers. Services describe only transforamtion
operation
which encorouge to simplify api surface and phrases value transformation
.
That leads to simplier design of each service. That also allows better testability
and better composition.
Services could be represented in several different forms. In general,
Service is a type that implements Service
trait.
struct MyService; impl Service for MyService { type Request = u8; type Response = u64; type Error = MyError; type Future = Pin<Box<Future<Output=Result<Self::Response, Self::Error>>>; fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { ... } fn call(&mut self) -> Self::Future { ... } }
Service can have mutable state that influence computation. This service could be rewritten as a simple function:
async fn my_service(req: u8) -> Result<u64, MyError>;
Associated Types
type Request
Requests handled by the service.
type Response
Responses given by the service.
type Error
Errors produced by the service.
type Future: Future<Output = Result<Self::Response, Self::Error>>
The future response value.
Required methods
fn poll_ready(&mut self, ctx: &mut Context) -> Poll<Result<(), Self::Error>>
Returns Ready
when the service is able to process requests.
If the service is at capacity, then NotReady
is returned and the task
is notified when the service becomes ready again. This function is
expected to be called while on a task.
This is a best effort implementation. False positives are permitted.
It is permitted for the service to return Ready
from a poll_ready
call and the next invocation of call
results in an error.
There are several notes to consider:
-
.poll_ready()
might be called on different task from actual service call. -
In case of chained services,
.poll_ready()
get called for all services at once.
fn call(&mut self, req: Self::Request) -> Self::Future
Process the request and return the response asynchronously.
This function is expected to be callable off task. As such,
implementations should take care to not call poll_ready
. If the
service is at capacity and the request is unable to be handled, the
returned Future
should resolve to an error.
Calling call
without calling poll_ready
is permitted. The
implementation must be resilient to this fact.
Provided methods
fn map<F, R>(self, f: F) -> Map<Self, F, R> where
Self: Sized,
F: FnMut(Self::Response) -> R,
Self: Sized,
F: FnMut(Self::Response) -> R,
Map this service's output to a different type, returning a new service of the resulting type.
This function is similar to the Option::map
or Iterator::map
where
it will change the type of the underlying service.
Note that this function consumes the receiving service and returns a
wrapped version of it, similar to the existing map
methods in the
standard library.
fn map_err<F, E>(self, f: F) -> MapErr<Self, F, E> where
Self: Sized,
F: Fn(Self::Error) -> E,
Self: Sized,
F: Fn(Self::Error) -> E,
Map this service's error to a different error, returning a new service.
This function is similar to the Result::map_err
where it will change
the error type of the underlying service. This is useful for example to
ensure that services have the same error type.
Note that this function consumes the receiving service and returns a wrapped version of it.
Implementations on Foreign Types
impl<'a, S> Service for &'a mut S where
S: Service + 'a,
[src]
S: Service + 'a,
type Request = S::Request
type Response = S::Response
type Error = S::Error
type Future = S::Future
fn poll_ready(&mut self, ctx: &mut Context) -> Poll<Result<(), Self::Error>>
[src]
fn call(&mut self, request: Self::Request) -> S::Future
[src]
impl<S: ?Sized> Service for Box<S> where
S: Service,
[src]
S: Service,
type Request = S::Request
type Response = S::Response
type Error = S::Error
type Future = S::Future
fn poll_ready(&mut self, ctx: &mut Context) -> Poll<Result<(), S::Error>>
[src]
fn call(&mut self, request: Self::Request) -> S::Future
[src]
impl<S> Service for RefCell<S> where
S: Service,
[src]
S: Service,
type Request = S::Request
type Response = S::Response
type Error = S::Error
type Future = S::Future
fn poll_ready(&mut self, ctx: &mut Context) -> Poll<Result<(), Self::Error>>
[src]
fn call(&mut self, request: Self::Request) -> S::Future
[src]
impl<S> Service for Rc<RefCell<S>> where
S: Service,
[src]
S: Service,
type Request = S::Request
type Response = S::Response
type Error = S::Error
type Future = S::Future
fn poll_ready(&mut self, ctx: &mut Context) -> Poll<Result<(), Self::Error>>
[src]
fn call(&mut self, request: Self::Request) -> S::Future
[src]
Implementors
impl<A, B> Service for AndThenService<A, B> where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
[src]
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
type Request = A::Request
type Response = B::Response
type Error = A::Error
type Future = AndThenServiceResponse<A, B>
fn poll_ready(&mut self, cx: &mut Context) -> Poll<Result<(), Self::Error>>
[src]
fn call(&mut self, req: A::Request) -> Self::Future
[src]
impl<A, B> Service for ThenService<A, B> where
A: Service,
B: Service<Request = Result<A::Response, A::Error>, Error = A::Error>,
[src]
A: Service,
B: Service<Request = Result<A::Response, A::Error>, Error = A::Error>,
type Request = A::Request
type Response = B::Response
type Error = B::Error
type Future = ThenServiceResponse<A, B>
fn poll_ready(&mut self, ctx: &mut Context) -> Poll<Result<(), Self::Error>>
[src]
fn call(&mut self, req: A::Request) -> Self::Future
[src]
impl<A, B, F, Fut, Res, Err> Service for AndThenApplyFn<A, B, F, Fut, Res, Err> where
A: Service,
B: Service,
F: FnMut(A::Response, &mut B) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
Err: From<A::Error> + From<B::Error>,
[src]
A: Service,
B: Service,
F: FnMut(A::Response, &mut B) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
Err: From<A::Error> + From<B::Error>,
type Request = A::Request
type Response = Res
type Error = Err
type Future = AndThenApplyFnFuture<A, B, F, Fut, Res, Err>
fn poll_ready(&mut self, cx: &mut Context) -> Poll<Result<(), Self::Error>>
[src]
fn call(&mut self, req: A::Request) -> Self::Future
[src]
impl<A, F, E> Service for MapErr<A, F, E> where
A: Service,
F: Fn(A::Error) -> E + Clone,
[src]
A: Service,
F: Fn(A::Error) -> E + Clone,
type Request = A::Request
type Response = A::Response
type Error = E
type Future = MapErrFuture<A, F, E>
fn poll_ready(&mut self, ctx: &mut Context) -> Poll<Result<(), Self::Error>>
[src]
fn call(&mut self, req: A::Request) -> Self::Future
[src]
impl<A, F, Response> Service for Map<A, F, Response> where
A: Service,
F: FnMut(A::Response) -> Response + Clone,
[src]
A: Service,
F: FnMut(A::Response) -> Response + Clone,
type Request = A::Request
type Response = Response
type Error = A::Error
type Future = MapFuture<A, F, Response>
fn poll_ready(&mut self, ctx: &mut Context) -> Poll<Result<(), Self::Error>>
[src]
fn call(&mut self, req: A::Request) -> Self::Future
[src]
impl<F, Fut, Req, Res, Err> Service for FnService<F, Fut, Req, Res, Err> where
F: FnMut(Req) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
[src]
F: FnMut(Req) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
type Request = Req
type Response = Res
type Error = Err
type Future = Fut
fn poll_ready(&mut self, _: &mut Context) -> Poll<Result<(), Self::Error>>
[src]
fn call(&mut self, req: Req) -> Self::Future
[src]
impl<F, Fut, Req, Res, Err> Service for FnServiceFactory<F, Fut, Req, Res, Err, ()> where
F: FnMut(Req) -> Fut + Clone,
Fut: Future<Output = Result<Res, Err>>,
[src]
F: FnMut(Req) -> Fut + Clone,
Fut: Future<Output = Result<Res, Err>>,
type Request = Req
type Response = Res
type Error = Err
type Future = Fut
fn poll_ready(&mut self, _: &mut Context) -> Poll<Result<(), Self::Error>>
[src]
fn call(&mut self, req: Self::Request) -> Self::Future
[src]
impl<T, F, R, In, Out, Err> Service for Apply<T, F, R, In, Out, Err> where
T: Service<Error = Err>,
F: FnMut(In, &mut T) -> R,
R: Future<Output = Result<Out, Err>>,
[src]
T: Service<Error = Err>,
F: FnMut(In, &mut T) -> R,
R: Future<Output = Result<Out, Err>>,