Skip to main content

HandlerChain

Struct HandlerChain 

Source
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/input
  • R - 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>

Source

pub fn create() -> Self

Create a new empty handler chain.

§Returns

A new HandlerChain instance with no handlers

§Examples
ⓘ
let chain = HandlerChain::<Request, Response>::create();
Source

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));
Source

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);
}
Source

pub fn reset(&mut self)

Reset the chain to start from the beginning.

This allows reusing the same chain for multiple requests.

§Examples
ⓘ
let mut chain = HandlerChain::create()
    .add_next(Box::new(MyHandler));

chain.handle(request1);
chain.reset();
chain.handle(request2); // Start from first handler again
Source

pub fn len(&self) -> usize

Get the number of handlers in the chain.

§Returns

The number of handlers

Source

pub fn is_empty(&self) -> bool

Check if the chain is empty.

§Returns

true if there are no handlers, false otherwise

Trait Implementations§

Source§

impl<T, R> Default for HandlerChain<T, R>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T, R> !Freeze for HandlerChain<T, R>

§

impl<T, R> !RefUnwindSafe for HandlerChain<T, R>

§

impl<T, R> !Send for HandlerChain<T, R>

§

impl<T, R> !Sync for HandlerChain<T, R>

§

impl<T, R> !UnwindSafe for HandlerChain<T, R>

§

impl<T, R> Unpin for HandlerChain<T, R>
where Vec<Box<dyn Handler<T, R>>>: Unpin,

§

impl<T, R> UnsafeUnpin for HandlerChain<T, R>
where Vec<Box<dyn Handler<T, R>>>: UnsafeUnpin,

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self> ⓘ

Attaches the current Context to this type, returning a WithContext wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more