tsuki_scheduler/runtime/
tokio.rs

1use std::future::Future;
2
3use crate::schedule::IntoSchedule;
4use crate::{Runtime, Task};
5
6/// Tokio runtime.
7///
8/// The task is spawned using [`tokio::task::spawn`].
9///
10/// # Create a new task
11/// see [`Task::tokio`]
12#[derive(Debug, Default)]
13pub struct Tokio;
14
15impl Runtime for Tokio {
16    type Handle = tokio::task::JoinHandle<()>;
17}
18
19impl Tokio {
20    pub fn new() -> Self {
21        Self
22    }
23}
24
25impl Task<Tokio> {
26    /// Create a new task that will be executed with tokio.
27    ///
28    /// # Example
29    /// ```
30    /// # use tsuki_scheduler::prelude::*;
31    /// let task = Task::tokio(now(), || async {
32    ///   println!("Hello, world!");
33    /// });
34    /// ```
35    pub fn tokio<S, F, Fut>(schedule: S, task: F) -> Self
36    where
37        S: IntoSchedule,
38        S::Output: Send + 'static,
39        F: Fn() -> Fut + Send + 'static,
40        Fut: Future<Output = ()> + Send + 'static,
41    {
42        Task {
43            schedule: Box::new(schedule.into_schedule()),
44            run: Box::new(move |_: _, _: _| tokio::task::spawn(task())),
45        }
46    }
47}