Crates.io | wd_event |
lib.rs | wd_event |
version | 0.6.1 |
source | src |
created_at | 2020-12-06 12:17:42.546617 |
updated_at | 2023-08-24 02:51:50.768682 |
description | event system |
homepage | |
repository | |
max_upload_size | |
id | 320108 |
size | 17,929 |
This is a Rust async event Crate. Compared to the previous version, this is a brand new version. Not compatible with previous versions
wd_event is available on crates.io. It is recommended to look there for the newest released version, as well as links to the newest builds of the docs.
At the point of the last update of this README, the latest published version could be used like this:
Add the following dependency to your Cargo manifest...
[dependencies]
wd_event = "0.4"
struct Message<V>{
id : u64,
value: V,
}
impl<V> Message<V> {
fn new(id:u64,value:V)->Message<V>{
Self{id,value}
}
}
fn generate_event_entity()->Event<EventSchedule>{
EventSchedule::default()
.register(|ctx:Context, mut msg:Message<String>|{
println!("this is first Message<String> handler ID[{}]--> {}",msg.id,msg.value);
msg.value.push_str("->one");
ctx.next(msg) // no need async
})
.register(|ctx:Context, mut msg:Message<String>|async{
println!("this is second Message<String> handler ID[{}]--> {}",msg.id,msg.value);
msg.value.push_str("->two");
ctx.next(msg).await //all is async
})
.register(|ctx:Context, mut msg:Message<i64>|{
println!("this is third Message<i64> handle ID[{}]--> {}",msg.id,msg.value);
msg.value = 3;
async move{
ctx.next(msg).await //part is async
}
})
.build()
}
#[tokio::test]
async fn test_message(){
let event = generate_event_entity();
let res = event.launch(Context::default(),Message::new(1,"hello world".to_string())).await.expect("first test handle failed");
assert_eq!(res.value.as_str(),"hello world->one->two","Message<String> result assert failed");
let res = event.launch(Context::default(),Message::new(1,1i64)).await.expect("second test failed");
assert_eq!(res.value,3i64,"Message<i64> result assert failed")
}
attention:
ctx.next
representing the next handler to execute the eventContext
can deliver messages in contextLicensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.