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, ()>
impl<In, Out> Intercept<In, Out, ()>
Sourcepub fn layer() -> InterceptLayer<In, Out>
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: Send, Out, S> Service<In> for Intercept<In, Out, S>where
S: Service<In, Out = Out>,
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
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.
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<In, Out, T> DynamicServiceExt<In, Out> for T
impl<In, Out, T> DynamicServiceExt<In, Out> for T
Source§fn into_dynamic(self) -> DynamicService<In, Out>
fn into_dynamic(self) -> DynamicService<In, Out>
Available on crate features
dynamic-service only.Converts this service into a type-erased
DynamicService.