2025-08-23 00:20:58 +08:00
|
|
|
#[cfg(feature = "tokio-trace")]
|
|
|
|
|
use std::any::type_name;
|
2025-04-11 17:27:56 +08:00
|
|
|
use std::future::Future;
|
2025-08-23 00:20:58 +08:00
|
|
|
#[cfg(feature = "tokio-trace")]
|
|
|
|
|
use std::panic::Location;
|
2025-04-11 17:27:56 +08:00
|
|
|
use tauri::{async_runtime, async_runtime::JoinHandle};
|
|
|
|
|
|
|
|
|
|
pub struct AsyncHandler;
|
|
|
|
|
|
|
|
|
|
impl AsyncHandler {
|
2025-08-23 00:20:58 +08:00
|
|
|
#[track_caller]
|
2025-04-11 17:27:56 +08:00
|
|
|
pub fn spawn<F, Fut>(f: F) -> JoinHandle<()>
|
|
|
|
|
where
|
|
|
|
|
F: FnOnce() -> Fut + Send + 'static,
|
|
|
|
|
Fut: Future<Output = ()> + Send + 'static,
|
|
|
|
|
{
|
2025-08-23 00:20:58 +08:00
|
|
|
#[cfg(feature = "tokio-trace")]
|
|
|
|
|
Self::log_task_info(&f);
|
2025-04-11 17:27:56 +08:00
|
|
|
async_runtime::spawn(f())
|
|
|
|
|
}
|
2025-08-22 03:52:30 +08:00
|
|
|
|
2025-08-23 00:20:58 +08:00
|
|
|
#[track_caller]
|
2025-08-22 03:52:30 +08:00
|
|
|
pub fn spawn_blocking<T, F>(f: F) -> JoinHandle<T>
|
|
|
|
|
where
|
|
|
|
|
F: FnOnce() -> T + Send + 'static,
|
|
|
|
|
T: Send + 'static,
|
|
|
|
|
{
|
2025-08-23 00:20:58 +08:00
|
|
|
#[cfg(feature = "tokio-trace")]
|
|
|
|
|
Self::log_task_info(&f);
|
2025-08-22 03:52:30 +08:00
|
|
|
async_runtime::spawn_blocking(f)
|
|
|
|
|
}
|
2025-08-23 00:20:58 +08:00
|
|
|
|
|
|
|
|
#[cfg(feature = "tokio-trace")]
|
|
|
|
|
#[track_caller]
|
|
|
|
|
fn log_task_info<F>(f: &F)
|
|
|
|
|
where
|
|
|
|
|
F: ?Sized,
|
|
|
|
|
{
|
|
|
|
|
const TRACE_MINI_SIZE: usize = 0;
|
|
|
|
|
let size = std::mem::size_of_val(f);
|
|
|
|
|
if size <= TRACE_MINI_SIZE {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let location = Location::caller();
|
|
|
|
|
let type_str = type_name::<F>();
|
|
|
|
|
let size_str = format!("{} bytes", size);
|
|
|
|
|
let loc_str = format!(
|
|
|
|
|
"{}:{}:{}",
|
|
|
|
|
location.file(),
|
|
|
|
|
location.line(),
|
|
|
|
|
location.column()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
println!("┌────────────────────┬─────────────────────────────────────────────────────────────────────────────┐");
|
|
|
|
|
println!("│ {:<18} │ {:<80} │", "Field", "Value");
|
|
|
|
|
println!("├────────────────────┼─────────────────────────────────────────────────────────────────────────────┤");
|
|
|
|
|
println!("│ {:<18} │ {:<80} │", "Type of task", type_str);
|
|
|
|
|
println!("│ {:<18} │ {:<80} │", "Size of task", size_str);
|
|
|
|
|
println!("│ {:<18} │ {:<80} │", "Called from", loc_str);
|
|
|
|
|
println!("└────────────────────┴─────────────────────────────────────────────────────────────────────────────┘");
|
|
|
|
|
}
|
2025-04-11 17:27:56 +08:00
|
|
|
}
|