1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
//! # 事件 event //! The event function is inspired by c#, and based on the event system, we can achieve program decoupling more easily. So rust has also implemented a similar, but richer, lightweight event development package. //! Project development address:[https://gitee.com/yutiandou/wd-event](https://gitee.com/yutiandou/wd-event) //! //! The event functions implemented with rust are as follows: //! - event add and execute //! - Context //! - delay event,delay cycle event //! - task pool //! - object pool //! //! //! ### extern wd-event crate //! ```rust //! use wd_event::{EventManage,EConfig,Context,Event,EType,AsyncEvent}; //! ``` //! //! ### create a event manager //! ```rust //! fn main() { //! let mut es = EventManage::new(EConfig::default()); //! //TODO ... //! } //! //! #[tokio::main] //! async fn main() { //! let mut es = EventManage::new_async(EConfig::default()).await; //! //TODO ... //! } //! ``` //! //! ### add a event //! If a structure implements the Event trait,it can be registered. //! - EType::Default: Execute the Event's handle method. //! - EType::Mutable: Execute the Event's handle_mut method. //! - EType::Async: Execute a async event. //! //! sync //! ```rust //! struct HandleOne; //! impl Event for HandleOne{ //! fn handle(&self,_ctx:Arc<Context>){ //! println!("execute handlone handle") //! } //! } //! //! let event_one = String::from("event_one"); //! es.add( event_one.clone(),HandleOne{},EType::Default).unwrap(); //! ``` //! //! async ** exec_sequence function execute async event is nonsegmented ** //! ```rust //! struct AsyncHanle; //! #[wd_event::event_trait] //! impl AsyncEvent for AsyncHanle{ //! async fn handle(&self,_ctx:Arc<Context>){ //! tokio::time::sleep(Duration::from_secs(1)).await; //! println!("休眠一秒后执行"); //! } //! } //! //! es.add_async(event_one.clone(),AsyncHanle{}).unwrap(); //! ``` //! //! ### execute event , non-blocking //! - exec_sequence: Execute in order, register first execute first. //! - exec_pool: Execute events through the task pool. //! - exec_immediately: Execute the event asynchronously immediately. //! ```rust //! es.exec_sequence(&event_one,None); //! es.exec_pool(&event_one,None); //! es.exec_immediately(&event_one,None).await;; //! ``` //! //! ### Context //! Context is used to pass state and share messages in multiple callbacks to an event. //! Changing the status of Context will stop the subsequent function calls, only the exec_sequence method and cycle event. //! Refer to the API documentation for more methods. //! //! ```rust //! //Create context and add a default message //! let a:i32 = 1; //! es.exec_sequence(&event_one,Some(Context::new_add_msg(a))); //! //! //Gets the default message for the context //! impl Event for HandleOne{ //! fn handle(&self,ctx:Arc<Context>){ //! let value = match ctx.get_msg::<i32>(){ //! Some(s)=>{ //! s.add(10) //! } //! None=>{0} //! }; //! println!("execute handlone handle,get value {}",value); //! } //! } //! // //! //! ``` //! //! ### delay event,delay cycle event //! //! ```rust //! //delay event and cycle event. only the handle_mut method is called //! fn handle_mut(&mut self,ctx:Arc<Context>){ //! let a = match ctx.get_arc_value::<String,i32>(&self.key){ //! Some(s)=>{ //! s.add(1)} //! None=>{0} //! }; //! println!("循环{}次",a); //! ctx.set_value(&self.key, a); //! } //! //Once a second //! es.delay_exec(Cycle{key:"default".to_string()},Context::new_delay(std::time::Duration::from_secs(1),true)); //! ``` //! //! ### task pool //! Task pool functionality, integrated into the event manager. Using the exec_pool function. //! Of course, the task pool function can also be used separately, as detailed in the API documentation. //! //! ### object pool //! Object pool, currently only a simple implementation. //! It is worth considering whether rust really needs object pools. #![allow(unused_variables)] pub mod event; pub mod task_pool; pub mod context; pub mod obj_pool; pub use async_trait::async_trait as event_trait; pub use event::{EType,EventManage,AsyncEvent,Event,EConfig}; pub use task_pool::{TaskPoolType,Task,TaskEntity}; pub use obj_pool::ObjectManage; pub use context::Context;