tsuki_scheduler/runtime/
async_std.rs

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