EventBus

Struct EventBus 

Source
pub struct EventBus<T: Send + Sync + Clone + 'static> { /* private fields */ }
Expand description

高性能事件总线

使用以下优化策略:

  • ArcSwap 实现无锁读取事件处理器列表
  • DashMap 用于快速查找和管理事件处理器
  • 原子计数器生成唯一 ID
  • 批量事件处理优化

Implementations§

Source§

impl<T: Send + Sync + Clone + 'static> EventBus<T>

Source

pub fn add_event_handler( &self, event_handler: Arc<dyn EventHandler<T> + Send + Sync>, ) -> ForgeResult<HandlerId>

添加事件处理器,返回处理器 ID

Source

pub fn add_event_handlers( &self, event_handlers: Vec<Arc<dyn EventHandler<T> + Send + Sync>>, ) -> ForgeResult<Vec<HandlerId>>

批量添加事件处理器

Source

pub fn remove_event_handler(&self, handler_id: HandlerId) -> ForgeResult<bool>

移除事件处理器

Source

pub fn remove_event_handlers( &self, handler_ids: &[HandlerId], ) -> ForgeResult<usize>

批量移除事件处理器

Source

pub fn handler_count(&self) -> usize

获取当前活跃的处理器数量

Source

pub fn clear_handlers(&self) -> ForgeResult<()>

清空所有事件处理器

Source

pub async fn destroy(&self) -> ForgeResult<()>

异步销毁事件总线

Source

pub fn destroy_blocking(&self)

同步销毁事件总线(仅在非异步上下文中使用)

⚠️ 警告:此方法可能阻塞,应优先使用 destroy() 异步版本

Source

pub fn start_event_loop(&self)

启动事件循环

Source

pub fn new() -> Self

Source

pub fn with_config(config: EventConfig) -> Self

Source

pub fn subscribe(&self) -> Receiver<T>

Source

pub async fn broadcast(&self, event: T) -> ForgeResult<()>

Source

pub fn broadcast_blocking(&self, event: T) -> ForgeResult<()>

同步广播事件(仅在非异步上下文中使用)

⚠️ 警告:此方法可能阻塞当前线程,应优先使用 broadcast() 异步版本

§使用场景
  • 在 Drop 实现中
  • 在同步的测试代码中
  • 在非异步的回调函数中
§示例
// 在异步上下文中,优先使用:
// event_bus.broadcast(event).await?;

// 仅在必要时使用阻塞版本:
event_bus.broadcast_blocking(event)?;
Source

pub fn get_config(&self) -> &EventConfig

获取事件配置

Source

pub fn update_config(&mut self, config: EventConfig)

更新事件配置(注意:某些配置更改需要重启事件循环才能生效)

Source

pub fn get_stats(&self) -> EventBusStats

获取事件总线统计信息

Source

pub fn reset_stats(&self)

重置统计信息

Source

pub fn get_performance_report(&self) -> EventBusPerformanceReport

获取详细的性能报告

Trait Implementations§

Source§

impl<T: Send + Sync + Clone + 'static> Clone for EventBus<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Send + Sync + Clone + 'static> Default for EventBus<T>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<T> Freeze for EventBus<T>

§

impl<T> !RefUnwindSafe for EventBus<T>

§

impl<T> Send for EventBus<T>

§

impl<T> Sync for EventBus<T>

§

impl<T> !Unpin for EventBus<T>

§

impl<T> !UnwindSafe for EventBus<T>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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