Trait Service
trait Service<Request>
An asynchronous function from a Request to a Response.
The Service trait is a simplified interface making it easy to write
network applications in a modular and reusable way, decoupled from the
underlying protocol.
Functional
A Service is a function of a Request. It immediately returns a
Future representing the eventual completion of processing the
request. The actual request processing may happen at any time in the
future, on any thread or executor. The processing may depend on calling
other services. At some point in the future, the processing will complete,
and the Future will resolve to a response or error.
At a high level, the Service::call function represents an RPC request. The
Service value can be a server or a client.
Associated Types
type ResponseResponses given by the service.
type ErrorErrors produced by the service.
Note: Returning an
Errorto a hyper server, the behavior depends on the protocol. In most cases, hyper will cause the connection to be abruptly aborted. It will abort the request however the protocol allows, either with some sort of RST_STREAM, or killing the connection if that doesn't exist.type Future: TraitBound { trait_: Path { path: "Future", id: Id(268), args: Some(AngleBracketed { args: [], constraints: [AssocItemConstraint { name: "Output", args: None, binding: Equality(Type(ResolvedPath(Path { path: "Result", id: Id(34), args: Some(AngleBracketed { args: [Type(QualifiedPath { name: "Response", args: None, self_type: Generic("Self"), trait_: Some(Path { path: "", id: Id(402), args: None }) }), Type(QualifiedPath { name: "Error", args: None, self_type: Generic("Self"), trait_: Some(Path { path: "", id: Id(402), args: None }) })], constraints: [] }) }))) }] }) }, generic_params: [], modifier: None }The future response value.
Required Methods
fn call(self: &Self, req: Request) -> <Self as >::FutureProcess the request and return the response asynchronously.
calltakes&selfinstead ofmut &selfbecause:- It prepares the way for async fn,
since then the future only borrows
&self, and thus a Service can concurrently handle multiple outstanding requests at once. - It's clearer that Services can likely be cloned
- To share state across clones, you generally need
Arc<Mutex<_>>That means you're not really using the&mut selfand could do with a&self. The discussion on this is here: https://github.com/hyperium/hyper/issues/3040
- It prepares the way for async fn,
since then the future only borrows
Implementors
impl<Request, S: Service<Request> + ?Sized> Service for &mut Simpl<Request, S: Service<Request> + ?Sized> Service for Box<S>impl<Request, S: Service<Request> + ?Sized> Service for Rc<S>impl<Request, S: Service<Request> + ?Sized> Service for Arc<S>impl<Request, S: Service<Request> + ?Sized> Service for &S