pub struct HandlerChain<T, R> { /* private fields */ }Expand description
Handler chain for managing a sequence of handlers.
This struct manages a collection of handlers and executes them in sequence. Each handler can process the request and decide whether to continue to the next handler in the chain.
§Type Parameters
T- The type of the request/inputR- The type of the response/output
§Examples
use rocketmq_common::common::chain::{Handler, HandlerChain};
let chain = HandlerChain::<Request, Response>::create()
.add_next(Box::new(ValidationHandler))
.add_next(Box::new(AuthenticationHandler))
.add_next(Box::new(ProcessingHandler));
let response = chain.handle(request);Implementations§
Source§impl<T, R> HandlerChain<T, R>
impl<T, R> HandlerChain<T, R>
Sourcepub fn add_next(self, handler: Box<dyn Handler<T, R>>) -> Self
pub fn add_next(self, handler: Box<dyn Handler<T, R>>) -> Self
Add a handler to the end of the chain.
This method allows for builder-style chaining by returning self.
§Arguments
handler- A boxed handler to add to the chain
§Returns
Self for method chaining
§Examples
let chain = HandlerChain::create()
.add_next(Box::new(FirstHandler))
.add_next(Box::new(SecondHandler));Sourcepub fn handle(&self, t: T) -> Option<R>
pub fn handle(&self, t: T) -> Option<R>
Process the request through the handler chain.
This method executes handlers in sequence. Each handler can process the request and optionally call this method again to continue to the next handler in the chain.
§Arguments
t- The request to process
§Returns
An Option<R> containing the response, or None if no handlers
are left or no response is produced
§Implementation Note
Uses Cell to store current_index, achieving interior mutability. This allows modifying the index while holding an immutable borrow of self, avoiding borrow conflicts and unsafe code.
§Examples
let mut chain = HandlerChain::create()
.add_next(Box::new(MyHandler));
if let Some(response) = chain.handle(request) {
println!("Got response: {:?}", response);
}