Crates.io | tokio-postponable-delay |
lib.rs | tokio-postponable-delay |
version | 0.1.0 |
source | src |
created_at | 2020-05-23 12:57:55.254037 |
updated_at | 2020-05-23 12:57:55.254037 |
description | A tokio Delay that can be pushed back without instantiating new delays |
homepage | |
repository | https://github.com/p-avital/tokio-postponable-delay |
max_upload_size | |
id | 244871 |
size | 6,730 |
Like a normal tokio::time::Delay
, except it will check whether its target time of resolution has moved before resolving, and reschedule itself if needed.
For now, this works by dropping the previous Delay
and instantiating a new one to reschedule itself.
async fn typical_usage() {
let target = std::time::Instant::now() + std::time::Duration::from_millis(10);
let delay = ResettableDelay::new(target);
let handle = delay.get_handle();
std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_millis(5));
let target = std::time::Instant::now() + std::time::Duration::from_millis(10);
handle.postpone(target)
});
delay.await;
}