tsuki_scheduler/runtime/
thread.rs

1use std::thread::JoinHandle;
2
3use crate::schedule::IntoSchedule;
4use crate::{Runtime, Task};
5
6/// thread based runtime
7/// # Errors
8/// if system hasn't enough resource to create new thread, a io::Error will be returned.
9#[derive(Debug, Clone, Default)]
10pub struct Thread;
11
12impl Thread {
13    pub fn new() -> Self {
14        Thread
15    }
16}
17
18impl Runtime for Thread {
19    type Handle = std::io::Result<JoinHandle<()>>;
20}
21
22impl Task<Thread> {
23    pub fn thread<S, F>(schedule: S, task: F) -> Self
24    where
25        S: IntoSchedule,
26        S::Output: Send + 'static,
27        F: Fn() + Send + 'static + Clone,
28    {
29        Task {
30            schedule: Box::new(schedule.into_schedule()),
31            run: Box::new(move |_: _, task_run: _| {
32                std::thread::Builder::new()
33                    .name(task_run.to_string())
34                    .spawn(task.clone())
35            }),
36        }
37    }
38}