Skip to main content

MiddlewareChain

Struct MiddlewareChain 

Source
pub struct MiddlewareChain<Evt, ErrState = ErrorsDisabled> { /* private fields */ }
Expand description

中间件链——按注册顺序逐层执行。

链中的每一层可以是 inspector 或 mutator,通过构建方法指定。 参数 ErrState 是 typestate 标记,默认 ErrorsDisabled

§执行模型

  1. Inspector 层:同一层内的 inspector 通过 tokio::spawn 并行执行, 不等待其完成。错误通过 error channel 异步报告。 无 tokio 运行时:降级为同步执行。
  2. Mutator 层:同一层内的 mutator 按注册顺序依次执行。 遇到 MutatorAction::Block 立即终止整条链。

§Typestate 错误通道

// 默认状态:ErrorsDisabled
let chain = MiddlewareChain::<String>::new()
    .with_inspector(Insp);

// 激活错误通道后变为 ErrorsEnabled
let (chain, rx) = chain.activate_error_channel();
// chain:  MiddlewareChain<String, ErrorsEnabled>

Implementations§

Source§

impl<Evt: Clone + Send + 'static> MiddlewareChain<Evt, ErrorsDisabled>

Source

pub fn new() -> Self

创建一个新的空中间件链(ErrorsDisabled 状态)。

链初始为空,需要通过 with_inspectorwith_mutators 等方法填充。

Source

pub fn activate_error_channel( self, ) -> (MiddlewareChain<Evt, ErrorsEnabled>, UnboundedReceiver<(String, InspectorError)>)

启用错误通道并获取 receiver。

调用后链的状态变为 ErrorsEnabled,inspector 执行中的错误将通过 返回的 receiver 异步送达。此方法仅可调用一次(编译期由 typestate 保证)。

如果不需要跟踪 inspector 错误,可以忽略返回值中的 receiver。

§示例
let chain = MiddlewareChain::<String>::new()
    .with_inspector(Insp);
let (_chain, mut error_rx) = chain.activate_error_channel();
Source§

impl<Evt: Clone + Send + 'static, S> MiddlewareChain<Evt, S>

Source

pub fn with_inspector(self, i: impl InspectorMiddleware<Evt> + 'static) -> Self

添加单个 inspector 作为独立的一层。

每个 with_inspector 调用创建一个新层,与其他层按注册顺序执行。 同一层内的 inspector 通过 tokio::spawn 后台并行。

Source

pub fn with_inspectors(self, i: impl IntoInspectors<Evt>) -> Self

添加一组 inspector 作为同一层(并行)。

接受 Bevy 风格的 tuple (A, B, C),所有 inspector 将在同一层内并行执行。

§示例
let chain = MiddlewareChain::<String>::new()
    .with_inspectors((A, B));  // A 和 B 并行执行
Source

pub fn with_inspectors_from_iter( self, iter: impl IntoIterator<Item = Arc<dyn InspectorMiddleware<Evt>>>, ) -> Self

从迭代器批量添加已装箱的 inspector。

Source

pub fn with_mutator(self, m: impl MutatorMiddleware<Evt> + 'static) -> Self

添加单个 mutator 作为独立的一层。

每个 with_mutator 调用创建一个新层。同一层内的 mutator 按顺序执行。

Source

pub fn with_mutators(self, m: impl IntoMutators<Evt>) -> Self

添加一组 mutator 作为同一层(顺序执行)。

接受 Bevy 风格的 tuple (A, B, C),所有 mutator 按注册顺序依次执行。

§示例
let chain = MiddlewareChain::<String>::new()
    .with_mutators((Censor, Blocker));  // Censor 先,Blocker 后
Source

pub fn with_mutators_from_iter( self, iter: impl IntoIterator<Item = Arc<dyn MutatorMiddleware<Evt>>>, ) -> Self

从迭代器批量添加已装箱的 mutator。

Source

pub fn is_empty(&self) -> bool

链中是否没有任何 middleware 层。

Source

pub fn len(&self) -> usize

返回 middleware 层的数量。

Source

pub fn process(&self, event: Evt) -> Result<Evt, MiddlewareBlocked>

按注册顺序逐层执行。

§执行流程
  • Inspector 层tokio::spawn 后台并发执行,不等待,错误进入 error channel。 如果当前没有 tokio 运行时,降级为同步执行。
  • Mutator 层:同步顺序执行。遇到 Block 立即短路,返回 Err(MiddlewareBlocked)
§返回值
  • Ok(event) — 经过所有 layer 处理后的最终事件
  • Err(MiddlewareBlocked) — 被 mutator 阻止
Source§

impl<Evt: Clone + Send + 'static> MiddlewareChain<Evt, ErrorsEnabled>

Source

pub fn error_sender(&self) -> Option<UnboundedSender<(String, InspectorError)>>

返回底层 error channel 的 sender,可用于外部发送错误。

返回的 sender 是 Option——如果 chain 构造时未激活错误通道则为 None

Trait Implementations§

Source§

impl<Evt: Clone + Send + 'static> Default for MiddlewareChain<Evt, ErrorsDisabled>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<Evt, ErrState = ErrorsDisabled> !RefUnwindSafe for MiddlewareChain<Evt, ErrState>

§

impl<Evt, ErrState = ErrorsDisabled> !UnwindSafe for MiddlewareChain<Evt, ErrState>

§

impl<Evt, ErrState> Freeze for MiddlewareChain<Evt, ErrState>

§

impl<Evt, ErrState> Send for MiddlewareChain<Evt, ErrState>
where ErrState: Send,

§

impl<Evt, ErrState> Sync for MiddlewareChain<Evt, ErrState>
where ErrState: Sync,

§

impl<Evt, ErrState> Unpin for MiddlewareChain<Evt, ErrState>
where ErrState: Unpin,

§

impl<Evt, ErrState> UnsafeUnpin for MiddlewareChain<Evt, ErrState>

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> MaybeSend for T
where T: Send,

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, 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.
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