[−][src]Crate wd_event
事件 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
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
use wd_event::{EventManage,EConfig,Context,Event,EType,AsyncEvent};
create a event manager
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
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 **
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.
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.
//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
//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.
Re-exports
pub use event::EType; |
pub use event::EventManage; |
pub use event::AsyncEvent; |
pub use event::Event; |
pub use event::EConfig; |
pub use task_pool::TaskPoolType; |
pub use task_pool::Task; |
pub use task_pool::TaskEntity; |
pub use obj_pool::ObjectManage; |
pub use context::Context; |
Modules
context | |
event | |
obj_pool | |
task_pool |
Attribute Macros
event_trait |