iced_futures/backend/native/
async_std.rs

1//! An `async-std` backend.
2
3/// An `async-std` executor.
4#[derive(Debug)]
5pub struct Executor;
6
7impl crate::Executor for Executor {
8    fn new() -> Result<Self, futures::io::Error> {
9        Ok(Self)
10    }
11
12    #[allow(clippy::let_underscore_future)]
13    fn spawn(&self, future: impl Future<Output = ()> + Send + 'static) {
14        let _ = async_std::task::spawn(future);
15    }
16}
17
18pub mod time {
19    //! Listen and react to time.
20    use crate::subscription::{self, Hasher, Subscription};
21
22    /// Returns a [`Subscription`] that produces messages at a set interval.
23    ///
24    /// The first message is produced after a `duration`, and then continues to
25    /// produce more messages every `duration` after that.
26    pub fn every(
27        duration: std::time::Duration,
28    ) -> Subscription<std::time::Instant> {
29        subscription::from_recipe(Every(duration))
30    }
31
32    #[derive(Debug)]
33    struct Every(std::time::Duration);
34
35    impl subscription::Recipe for Every {
36        type Output = std::time::Instant;
37
38        fn hash(&self, state: &mut Hasher) {
39            use std::hash::Hash;
40
41            std::any::TypeId::of::<Self>().hash(state);
42            self.0.hash(state);
43        }
44
45        fn stream(
46            self: Box<Self>,
47            _input: subscription::EventStream,
48        ) -> futures::stream::BoxStream<'static, Self::Output> {
49            use futures::stream::StreamExt;
50
51            async_std::stream::interval(self.0)
52                .map(|_| std::time::Instant::now())
53                .boxed()
54        }
55    }
56}