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_model::common::chain::{Handler, HandlerChain};

struct Increment;
struct Double;

impl Handler<i32, i32> for Increment {
    fn handle(&self, request: i32, chain: &HandlerChain<i32, i32>) -> Option<i32> {
        chain.handle(request + 1)
    }
}

impl Handler<i32, i32> for Double {
    fn handle(&self, request: i32, _chain: &HandlerChain<i32, i32>) -> Option<i32> {
        Some(request * 2)
    }
}

let chain = HandlerChain::create()
    .add_next(Box::new(Increment))
    .add_next(Box::new(Double));
assert_eq!(chain.handle(3), Some(8));
assert_eq!(chain.handle(3), None);

The cursor advances before each handler is called. Handlers delegate explicitly through Self::handle; returning a response without delegation stops that call. Later calls continue at the current cursor. Independent requests require an explicit Self::reset when reusing the chain.

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
use rocketmq_model::common::chain::HandlerChain;

let chain = HandlerChain::<i32, i32>::create();
assert!(chain.is_empty());
assert_eq!(chain.handle(1), None);
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

See HandlerChain for a complete example with delegating and terminal handlers.

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

The cursor advances before invoking the selected handler. Returning a response without calling chain.handle leaves later handlers for a subsequent call. The cursor is not reset automatically, even if a handler returns None. See HandlerChain for delegation and Self::reset for reuse.

Source

pub fn reset(&mut self)

Reset the chain to start from the beginning.

This allows reusing the same chain for multiple requests.

§Examples
use rocketmq_model::common::chain::{Handler, HandlerChain};

struct Echo;
impl Handler<i32, i32> for Echo {
    fn handle(&self, request: i32, _chain: &HandlerChain<i32, i32>) -> Option<i32> {
        Some(request)
    }
}

let mut chain = HandlerChain::create().add_next(Box::new(Echo));
assert_eq!(chain.handle(7), Some(7));
assert_eq!(chain.handle(8), None);
chain.reset();
assert_eq!(chain.handle(8), Some(8));
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> 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> 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