Skip to content

Commit a527fae

Browse files
committed
add AsyncTask(Boxed)
1 parent 3b53c32 commit a527fae

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

burn-train/src/metric/store/client.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use super::EventStore;
22
use super::{Aggregate, Direction, Event, Split};
3-
use crate::util;
3+
use crate::util::{self, AsyncTaskBoxed};
4+
use log::info;
45
use std::sync::mpsc;
56

67
/// Type that allows to communicate with an [event store](EventStore).
78
pub struct EventStoreClient {
89
sender: mpsc::Sender<Message>,
9-
handler: Option<Box<dyn FnOnce() -> Result<(), ()>>>,
10+
handler: Option<AsyncTaskBoxed>,
1011
}
1112

1213
impl EventStoreClient {
@@ -154,7 +155,7 @@ impl Drop for EventStoreClient {
154155
let handler = self.handler.take();
155156

156157
if let Some(handler) = handler {
157-
handler().expect("The event store thread should stop.");
158+
handler.join().expect("The event store thread should stop.");
158159
}
159160
}
160161
}

burn-train/src/util.rs

+26-4
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,45 @@
11
#![allow(missing_docs)]
22

3+
pub trait AsyncTask {
4+
fn join(self: Box<Self>) -> Result<(), ()>;
5+
}
6+
7+
pub type AsyncTaskBoxed = Box<dyn AsyncTask>;
8+
9+
struct Thread {
10+
join: Box<dyn FnOnce() -> Result<(), ()>>,
11+
}
12+
13+
impl AsyncTask for Thread {
14+
fn join(self: Box<Self>) -> Result<(), ()> {
15+
(self.join)()
16+
}
17+
}
18+
19+
impl Thread {
20+
fn new(join: Box<dyn FnOnce() -> Result<(), ()>>) -> Self {
21+
Thread { join }
22+
}
23+
}
24+
325
#[cfg(not(feature = "browser"))]
4-
pub fn spawn<F>(f: F) -> Box<dyn FnOnce() -> Result<(), ()>>
26+
pub fn spawn<F>(f: F) -> AsyncTaskBoxed
527
where
628
F: FnOnce(),
729
F: Send + 'static,
830
{
931
let handle = std::thread::spawn(f);
10-
Box::new(move || handle.join().map_err(|_| ()))
32+
Box::new(Thread::new(Box::new(move || handle.join().map_err(|_| ()))))
1133
}
1234

1335
#[cfg(feature = "browser")]
14-
pub fn spawn<F>(f: F) -> Box<dyn FnOnce() -> Result<(), ()>>
36+
pub fn spawn<F>(f: F) -> AsyncTaskBoxed
1537
where
1638
F: FnOnce(),
1739
F: Send + 'static,
1840
{
1941
rayon::spawn(f);
20-
Box::new(|| Ok(()))
42+
Box::new(Thread::new(Box::new(|| Ok(()))))
2143
}
2244

2345
#[cfg(feature = "browser")]

0 commit comments

Comments
 (0)