Skip to content

Commit 6c101dd

Browse files
committed
runtime agnostic Instant
1 parent e74094c commit 6c101dd

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

src/report_aggregator/mod.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ use futures::{
44
channel::mpsc::{self, Sender},
55
StreamExt,
66
};
7-
#[cfg(feature = "tokio-comp")]
8-
use tokio::time::Instant;
97

108
use crate::{
119
packages::uname::Uname,
1210
proto::report::{Report, ReportHeader, Trace, TracesAndStats},
13-
runtime::{spawn, JoinHandle},
11+
runtime::{spawn, JoinHandle, abort, Instant},
1412
};
1513

1614
/// The [ReportAggregator] is the structure which control the background task spawned to aggregate
@@ -145,14 +143,7 @@ impl ReportAggregator {
145143

146144
impl Drop for ReportAggregator {
147145
fn drop(&mut self) {
148-
cfg_if::cfg_if! {
149-
if #[cfg(all(feature = "tokio-comp", not(feature = "async-std-comp")))] {
150-
self.handle.abort();
151-
} else if #[cfg(feature = "async-std-comp")] {
152-
} else {
153-
compile_error!("tokio-comp or async-std-comp features required");
154-
}
155-
}
146+
abort(&self.handle);
156147
// TODO: Wait for the proper aborted task
157148
}
158149
}

src/runtime.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,38 @@ cfg_if::cfg_if! {
77
tokio::spawn(f)
88
}
99

10+
pub fn abort(handle: &JoinHandle<()>) {
11+
handle.abort();
12+
}
13+
14+
pub struct Instant(tokio::time::Instant);
15+
impl Instant {
16+
pub fn now() -> Instant {
17+
Instant(tokio::time::Instant::now())
18+
}
19+
20+
pub fn elapsed(&self) -> std::time::Duration {
21+
self.0.elapsed()
22+
}
23+
}
1024
} else if #[cfg(feature = "async-std-comp")] {
1125
pub use async_std::task::JoinHandle;
1226
pub fn spawn(f: impl Future<Output = ()> + Send + 'static) -> JoinHandle<()> {
1327
async_std::task::spawn(f)
1428
}
29+
30+
pub fn abort(handle: &JoinHandle<()>) {}
31+
32+
pub struct Instant(std::time::Instant);
33+
impl Instant {
34+
pub fn now() -> Instant {
35+
Instant(std::time::Instant::now())
36+
}
37+
38+
pub fn elapsed(&self) -> std::time::Duration {
39+
self.0.elapsed()
40+
}
41+
}
1542
} else {
1643
compile_error!("tokio-comp or async-std-comp features required");
1744
}

0 commit comments

Comments
 (0)