flams_math_archives/utils/mod.rs
1pub mod errors;
2pub mod ignore_source;
3pub mod lazy_file;
4pub mod path_ext;
5
6pub trait AsyncEngine: 'static {
7 fn background(f: impl FnOnce() + Send + 'static);
8 fn block_on<R: Send + Sync + 'static>(
9 f: impl FnOnce() -> R + Send + Sync + 'static,
10 ) -> impl std::future::Future<Output = R> + Send + Sync;
11 fn exec_after(delay: std::time::Duration, f: impl FnOnce() + Send + 'static);
12}
13
14pub struct SyncEngine;
15impl AsyncEngine for SyncEngine {
16 #[inline]
17 fn background(f: impl FnOnce() + Send + 'static) {
18 let _ = std::thread::spawn(f);
19 }
20 fn block_on<R: Send + Sync>(
21 f: impl FnOnce() -> R + Send + Sync,
22 ) -> impl std::future::Future<Output = R> + Send + Sync {
23 std::future::ready(f())
24 }
25 fn exec_after(delay: std::time::Duration, f: impl FnOnce() + Send + 'static) {
26 std::thread::spawn(move || {
27 std::thread::sleep(delay);
28 f();
29 });
30 }
31}
32
33pub struct AllSyncEngine;
34impl AsyncEngine for AllSyncEngine {
35 #[inline]
36 fn background(f: impl FnOnce() + Send + 'static) {
37 f();
38 }
39 fn block_on<R: Send + Sync>(
40 f: impl FnOnce() -> R + Send + Sync,
41 ) -> impl std::future::Future<Output = R> + Send + Sync {
42 std::future::ready(f())
43 }
44 fn exec_after(delay: std::time::Duration, f: impl FnOnce() + Send + 'static) {
45 std::thread::spawn(move || {
46 std::thread::sleep(delay);
47 f();
48 });
49 }
50}