Intercept

Struct Intercept 

Source
pub struct Intercept<In, Out, S> { /* private fields */ }
Available on crate features intercept only.
Expand description

Middleware for observing and modifying service inputs and outputs.

Useful for logging, debugging, metrics, validation, and other cross-cutting concerns.

§Examples

Simple usage that observes inputs and outputs without modification:

let stack = (
    Intercept::layer()
        .on_input(|input| println!("request: {input}"))
        .on_output(|output| println!("response: {output}")),
    Execute::new(|input: String| async move { input }),
);

let service = stack.into_service();
let response = service.execute("input".to_string()).await;

Advanced usage of Intercept allows you to modify and observe inputs and outputs:

let stack = (
    Intercept::<String, String, _>::layer()
        .on_input(|input| println!("request: {input}")) // input observers are called first
        .on_input(|input| println!("another: {input}")) // multiple observers supported
        .modify_input(|input| input.to_uppercase()) // then inputs are modified
        .modify_input(|input| input.to_lowercase()) // multiple modifications supported
        .on_output(|output| println!("response: {output}")) // output observers called first
        .on_output(|output| println!("another response: {output}")) // multiple observers supported
        .modify_output(|output| output.trim().to_string()) // then outputs are modified
        .modify_output(|output| format!("result: {output}")), // multiple modifications supported
    Execute::new(|input: String| async move { input }),
);

let service = stack.into_service();
let response = service.execute("input".to_string()).await;

Implementations§

Source§

impl<In, Out> Intercept<In, Out, ()>

Source

pub fn layer() -> InterceptLayer<In, Out>

Creates a new InterceptLayer for building interception middleware.

§Examples
let stack = (
    Intercept::layer(), // Create a new interception layer, no observers yet
    Execute::new(|input: String| async move { input }),
);

let service = stack.into_service();
let response = service.execute("input".to_string()).await;

Trait Implementations§

Source§

impl<In, Out, S: Clone> Clone for Intercept<In, Out, S>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<In, Out, S: Debug> Debug for Intercept<In, Out, S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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

Source§

async fn execute(&self, input: In) -> Self::Out

Executes the wrapped service with interception and modification.

Execution order: input observers → input modifications → service execution → output observers → output modifications. Input modifications can short-circuit execution by returning ControlFlow::Break.

Source§

type Out = Out

The output type returned by this service.
Source§

impl<Req, Res, Err, S> Service<Req> for Intercept<Req, Result<Res, Err>, S>
where Err: Send + 'static, Req: Send + 'static, Res: Send + 'static, S: Service<Req, Response = Res, Error = Err> + Send + Sync + 'static, S::Future: Send + 'static,

Available on crate features tower-service only.
Source§

type Response = Res

Responses given by the service.
Source§

type Error = Err

Errors produced by the service.
Source§

type Future = InterceptFuture<Result<Res, Err>>

The future response value.
Source§

fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>

Returns Poll::Ready(Ok(())) when the service is able to process requests. Read more
Source§

fn call(&mut self, req: Req) -> Self::Future

Process the request and return the response asynchronously. Read more

Auto Trait Implementations§

§

impl<In, Out, S> Freeze for Intercept<In, Out, S>
where S: Freeze,

§

impl<In, Out, S> !RefUnwindSafe for Intercept<In, Out, S>

§

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

§

impl<In, Out, S> Sync for Intercept<In, Out, S>
where S: Sync,

§

impl<In, Out, S> Unpin for Intercept<In, Out, S>
where S: Unpin,

§

impl<In, Out, S> !UnwindSafe for Intercept<In, Out, S>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<In, Out, T> DynamicServiceExt<In, Out> for T
where In: Send + 'static, Out: Send + 'static, T: Service<In, Out = Out> + 'static,

Source§

fn into_dynamic(self) -> DynamicService<In, Out>

Available on crate features dynamic-service only.
Converts this service into a type-erased DynamicService.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.