This commit is contained in:
2026-05-08 00:17:03 +02:00
parent 0de2e334ae
commit 5f05108132
6 changed files with 1102 additions and 107 deletions
+26 -26
View File
@@ -1,18 +1,18 @@
//! # Event Bus
//!
//! Un bus d'événements asynchrone permettant de faire transiter des messages typés
//! entre plusieurs modules, sans couplage direct.
//! An asynchronous event bus for routing typed messages between modules
//! without direct coupling.
//!
//! ## Caractéristiques
//! ## Features
//!
//! - **Association clé → événement** : chaque topic (`&str`) est indépendant
//! - **Sans restriction de type** : n'importe quel `T: Any + Send + Sync + Clone`
//! - **Wake-up ciblé** : seuls les abonnés du bon topic sont réveillés
//! - **API callback** : style JavaScript — `bus.on("topic", |payload| { ... })`
//! - **Pattern glob** : `on_pattern("user-*", |topic, payload| { ... })`
//! - **Handlers async** : `on_async` et `on_pattern_async`
//! - **Key → event mapping**: each topic (`&str`) is independent
//! - **Type-unrestricted**: any `T: Any + Send + Sync + Clone`
//! - **Targeted wake-up**: only subscribers of the matching topic are woken up
//! - **Callback API**: JavaScript-style — `bus.on("topic", |payload| { ... })`
//! - **Glob pattern**: `on_pattern("user-*", |topic, payload| { ... })`
//! - **Async handlers**: `on_async` and `on_pattern_async`
//!
//! ## Exemple — callback sync
//! ## Example — sync callback
//!
//! ```rust,no_run
//! use std::sync::Arc;
@@ -25,7 +25,7 @@
//! let bus = Arc::new(EventBus::new());
//!
//! bus.on::<User>("user-connected", |user| {
//! println!("Connecté : {:?}", user);
//! println!("Connected: {:?}", user);
//! });
//!
//! bus.emit("user-connected", User { name: "Alice".into() });
@@ -33,7 +33,7 @@
//! # });
//! ```
//!
//! ## Exemple — callback async
//! ## Example — async callback
//!
//! ```rust,no_run
//! use std::sync::Arc;
@@ -46,7 +46,7 @@
//! let bus = Arc::new(EventBus::new());
//!
//! bus.on_async::<User, _, _>("user-connected", |user| async move {
//! println!("(async) Connecté : {:?}", user);
//! println!("(async) Connected: {:?}", user);
//! });
//!
//! bus.emit("user-connected", User { name: "Bob".into() });
@@ -54,7 +54,7 @@
//! # });
//! ```
//!
//! ## Exemple — pattern glob (topic inclus dans le callback)
//! ## Example — glob pattern (topic included in the callback)
//!
//! ```rust,no_run
//! use std::sync::Arc;
@@ -68,8 +68,8 @@
//!
//! bus.on_pattern::<User, _>("user-*", |topic, user| {
//! match topic.as_str() {
//! "user-created" => println!("Créé : {:?}", user),
//! "user-deleted" => println!("Supprimé : {:?}", user),
//! "user-created" => println!("Created : {:?}", user),
//! "user-deleted" => println!("Deleted : {:?}", user),
//! other => println!("{}: {:?}", other, user),
//! }
//! });
@@ -80,7 +80,7 @@
//! # });
//! ```
//!
//! ## Exemple — multi-types avec `match_event!` (cas avancé)
//! ## Example — multi-type with `match_event!` (advanced)
//!
//! ```rust,no_run
//! use std::sync::Arc;
@@ -107,20 +107,20 @@
mod bus;
// Réexports publics
// Public re-exports
pub use bus::{AnyEvent, EventBus};
/// Downcaste un [`AnyEvent`] vers un ou plusieurs types concrets et exécute
/// la closure correspondante si le type correspond.
/// Downcasts an [`AnyEvent`] to one or more concrete types and executes
/// the matching closure if the type matches.
///
/// Les branches non correspondantes sont ignorées silencieusement.
/// Non-matching branches are silently ignored.
///
/// # Syntaxe
/// # Syntax
/// ```text
/// match_event!(evt, Type1 => |val| { ... }, Type2 => |val| { ... })
/// ```
///
/// # Exemple
/// # Example
/// ```rust,no_run
/// # use std::sync::Arc;
/// # use oxspeak_server_lib::event_bus::EventBus;
@@ -134,8 +134,8 @@ pub use bus::{AnyEvent, EventBus};
///
/// if let Ok(evt) = rx.recv().await {
/// match_event!(evt,
/// User => |u| println!("Utilisateur : {:?}", u),
/// UdpMetric => |m| println!("Metric : {:?}", m),
/// User => |u| println!("User: {:?}", u),
/// UdpMetric => |m| println!("Metric: {:?}", m),
/// );
/// }
/// # });
@@ -149,7 +149,7 @@ macro_rules! match_event {
} else
)+
{
// Aucun type ne correspond → on ignore silencieusement
// No matching type → silently ignored
}
};
}