Crates.io | timewrap |
lib.rs | timewrap |
version | 0.3.4 |
source | src |
created_at | 2021-08-09 07:29:47.696051 |
updated_at | 2021-08-30 01:30:24.258057 |
description | An async sugar for virtual timeline. |
homepage | |
repository | https://github.com/AlseinX/timewrap |
max_upload_size | |
id | 433556 |
size | 15,571 |
This is a rust library that supports a virtual timeline with capability to Rust's async ecosystem. Within it, the users may execute async task that could await
for specified virtual time, and would run sequentially by the await
ed time. It does not actually wait for time in the real world, but just ensures the sequence of execution by the given virtual time.
It could be useful for writing game or simulation logic that has a time dimension decoupled with the real time, with concise async coroutine coding style, instead of event handling, manually written state machine, or callback hell.
The very simple demo could be found in the unit test.
// Initialize a Timewrap instance with an attached state.
let result = Mutex::new(String::new());
let timewrap = Timewrap::new_with_state(result);
// Spawn just like you do with `tokio`, but does not need to be 'static.
timewrap.spawn(async_fn!(|timewrap| {
use std::fmt::Write;
// Spawn another task within a running task.
timewrap.spawn(async {
for i in 0..3 {
timewrap.delay(10).await;
let mut result = timewrap.lock().await;
write!(result, "a{}", i).unwrap();
}
});
timewrap.delay(5).await;
for i in 0..3 {
timewrap.delay(10).await;
let mut result = timewrap.lock().await;
write!(result, "b{}", i).unwrap();
}
}));
// Drive the Timewrap to the estimated time synchronously.
timewrap.drive_shared_block(35);
// Consume the result.
let result = timewrap.into_state().into_inner();
assert_eq!(result, "a0b0a1b1a2b2");