Compare commits
5 Commits
master
...
pipelined-
Author | SHA1 | Date |
---|---|---|
Avril | 1bfa6bbd4e | 3 years ago |
Avril | c658e8a702 | 3 years ago |
Avril | fbf96276ca | 3 years ago |
Avril | 444f3f38dc | 3 years ago |
Avril | 30522ec96f | 3 years ago |
@ -0,0 +1 @@
|
|||||||
|
<mxfile host="app.diagrams.net" modified="2021-08-19T19:47:34.219Z" agent="5.0 (X11)" etag="m3W9Xk7ZdncWV_dDKnck" version="14.9.8" type="device"><diagram id="yzWLK1SvWkPlnieqc2aE" name="Page-1">7V1bc5u6Fv41nt0+xAPC3B7T1NntpG06djptz8sZxZYNJxh8MMTJ/vVbEoibRCzfkJ10ptMYcZGQ1vrWt5aWRM+4Wjz9HcOl9zWaoqAHtOlTz/jYA0C3TBf/ISXPWQnQDCcrmcf+NL+qLBj7/6C8UMtLU3+KVrULkygKEn9ZL5xEYYgmSa0MxnG0rl82i4J6rUs4R1zBeAIDvvSnP028rNQBdln+Cflzj9WsW/kbLyC7OH+TlQen0bpSZAx7xlUcRUn2a/F0hQLSe6xfsvuuW84WDYtRmMjc8Pnr//2bzzex+xz88pK///vF/PGfC/aYRxik+RvnrU2eWRegKe6R/DCKEy+aRyEMhmXphzhKwyki9Wj4qLzmSxQtcaGOC/+HkuQ5H16YJhEu8pJFkJ/N6iQVtb5cXrSK0niCXngjJiQwnqPkhevcYgiw8KJogZL4Gd8XowAm/mO9HTAXonlxXdnP+Efe1Vt0O9/rP0ef74YjXHZ3Ob7pASvAbf9wH+Nf84R2UFYy9R9Z0Y8Vwqe1dewnWEGIWhD5hg+Ijl6M4KIHrsgFnj/xSIPIRR4MpwEeKfwLxajfXhF+rUpdgupv1yGt1SP1Dce3Vzd/rVhzaEXBrF7/GiNEeUcM1/j/KE2WaUKHdfKAki3a05DRUgKJOOEqEzReQioopN66tM38ILiKgiim9xpTiJzZBJfjXoseUOWMNXHQ/ayo7xHFCXp6WUJ5iSqwMMeBHAnN/HBdoorOoMKrIspAO5IQOkpU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bOUSzv3SqXcEQVFzxtezqPjbT6qchUgGPda9z/rHjLMurlTHitxXp0tdAaSwqrWXXAtFf0sjEht2AV612nImhng8xbKDdQOHKWSFoC9ufZ+mic1i80vimJJpfS+vvS0y3WVO9HksHwdxSxGlRyxLI0s519rnxbFZVYrjTvnLdhxyqZ7T7cRoDZlQfdorq5rKNX9nVIQ+vVo9AbdP6Q6y5IPtb6NI9jVu+bbkH3afVibxTlBp0bntg8SxIU61hYRRRFMKzOQIp1KF4cZl9LoeNGjq02H42jyQGbiPpHZabraNPEgQUn2FQ3Ijxj7Bk62o34lDbf2oQCyYdQ7rgpgwQUZUtqej+kyQE/j/LH5mazFlrbFHLnw7a4lG137IAprdK1lW7WlGfDseokuqBPuCyBw0ovNV/b9/gc+LL8rlEWrys8zGcN/AQ==</diagram></mxfile>
|
@ -0,0 +1,74 @@
|
|||||||
|
//! Syncronisation helpers
|
||||||
|
use super::*;
|
||||||
|
use futures::{
|
||||||
|
Future,
|
||||||
|
};
|
||||||
|
use tokio::time::Duration;
|
||||||
|
|
||||||
|
/// Force a future to take *at least* a specific amount of time to complete.
|
||||||
|
///
|
||||||
|
/// If the future takes longer than `time` to complete, no additional delay is added.
|
||||||
|
/// If `time` is zero, no delay is added.
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
/// The returned future will panic if
|
||||||
|
/// * The future itself panics
|
||||||
|
/// * We are unable to convert the future's duration from `chrono`'s duration span to std's duration span
|
||||||
|
pub fn timesync<'a, T, F>(future: F, time: Duration) -> impl Future<Output = T> + 'a
|
||||||
|
where F: Future<Output=T> + 'a
|
||||||
|
{
|
||||||
|
use chrono::prelude::*;
|
||||||
|
|
||||||
|
#[cold]
|
||||||
|
#[inline(never)]
|
||||||
|
fn _panic_failed_tmconv(orig: chrono::Duration, error: impl Into<eyre::Report>)
|
||||||
|
{
|
||||||
|
panic!("Failed to convert chrono duration span {:?} to std duration span: {}", orig, error.into());
|
||||||
|
}
|
||||||
|
|
||||||
|
async move {
|
||||||
|
let now = Utc::now();
|
||||||
|
let r = future.await;
|
||||||
|
if time.is_zero() {
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
let after = Utc::now();
|
||||||
|
let duration = after - now;
|
||||||
|
match duration.to_std() {
|
||||||
|
Ok(taken)
|
||||||
|
if taken < time => {
|
||||||
|
// Delay for the remaining time
|
||||||
|
tokio::time::delay_for(time - taken).await;
|
||||||
|
},
|
||||||
|
Err(error) => _panic_failed_tmconv(duration, error),
|
||||||
|
// At least `time` has passed
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
r
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait TimeSyncExt<'a>: Future + Sized + 'a
|
||||||
|
{
|
||||||
|
type OutputFuture: Future<Output = Self::Output> + 'a;
|
||||||
|
fn pad_time(self, time: Duration) -> Self::OutputFuture;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(nightly)]
|
||||||
|
impl<'a, F> TimeSyncExt<'a> for F
|
||||||
|
where F: Future + 'a
|
||||||
|
{
|
||||||
|
//#[cfg(nightly)]
|
||||||
|
type OutputFuture = impl Future<Output = Self::Output> + 'a;
|
||||||
|
//#[cfg(not(nightly))]
|
||||||
|
//type OutputFuture = futures::future::BoxFuture<'a, Self::Output>; //TODO: Requires `Send` bound for some reason.
|
||||||
|
fn pad_time(self, time: Duration) -> Self::OutputFuture {
|
||||||
|
/*#[cfg(not(nightly))] {
|
||||||
|
use futures::prelude::*;
|
||||||
|
return timesync(self, time).boxed();
|
||||||
|
}
|
||||||
|
#[cfg(nightly)] {
|
||||||
|
return*/ timesync(self, time)
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue