Service

Trait Service 

Source
pub trait Service<In>: Send + Sync {
    type Out;

    // Required method
    fn execute(&self, input: In) -> impl Future<Output = Self::Out> + Send;
}
Expand description

An async function In → Out that processes inputs.

This trait is the foundation for building composable services. Implement it directly for custom services, or use Execute to wrap closures.

See the crate documentation for usage examples and layer composition.

Required Associated Types§

Source

type Out

The output type returned by this service.

Required Methods§

Source

fn execute(&self, input: In) -> impl Future<Output = Self::Out> + Send

Processes the input and returns the output.

The returned future must be Send for compatibility with multi-threaded async runtimes.

§Examples
use layered::Service;

struct EchoService;

impl Service<String> for EchoService {
    type Out = String;

    async fn execute(&self, input: String) -> Self::Out {
        input
    }
}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<S, In> Service<In> for Box<S>
where S: Service<In>,

Source§

type Out = <S as Service<In>>::Out

Source§

fn execute(&self, input: In) -> impl Future<Output = Self::Out> + Send

Source§

impl<S, In> Service<In> for Arc<S>
where S: Service<In>,

Source§

type Out = <S as Service<In>>::Out

Source§

fn execute(&self, input: In) -> impl Future<Output = Self::Out> + Send

Implementors§

Source§

impl<E, F, In, Out> Service<In> for Execute<E>
where E: Fn(In) -> F + Send + Sync, F: Future<Output = Out> + Send,

Source§

type Out = Out

Source§

impl<In: Send, Out, S> Service<In> for Intercept<In, Out, S>
where S: Service<In, Out = Out>,

Available on crate features intercept only.
Source§

type Out = Out

Source§

impl<In: Send, Out: Send> Service<In> for DynamicService<In, Out>

Available on crate features dynamic-service only.
Source§

type Out = Out

Source§

impl<S, In: Send> Service<In> for Adapter<S>
where S: Service<In> + Send + Sync + Clone, S::Future: Send,

Available on crate features tower-service only.
Source§

type Out = Result<<S as Service<In>>::Response, <S as Service<In>>::Error>