iced_futures/backend/native/
smol.rs

1//! A `smol` backend.
2
3/// A `smol` 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    fn spawn(&self, future: impl Future<Output = ()> + Send + 'static) {
13        smol::spawn(future).detach();
14    }
15
16    fn block_on<T>(&self, future: impl Future<Output = T>) -> T {
17        smol::block_on(future)
18    }
19}
20
21pub mod time {
22    //! Listen and react to time.
23    use crate::subscription::{self, Hasher, Subscription};
24
25    /// Returns a [`Subscription`] that produces messages at a set interval.
26    ///
27    /// The first message is produced after a `duration`, and then continues to
28    /// produce more messages every `duration` after that.
29    pub fn every(
30        duration: std::time::Duration,
31    ) -> Subscription<std::time::Instant> {
32        subscription::from_recipe(Every(duration))
33    }
34
35    #[derive(Debug)]
36    struct Every(std::time::Duration);
37
38    impl subscription::Recipe for Every {
39        type Output = std::time::Instant;
40
41        fn hash(&self, state: &mut Hasher) {
42            use std::hash::Hash;
43
44            std::any::TypeId::of::<Self>().hash(state);
45            self.0.hash(state);
46        }
47
48        fn stream(
49            self: Box<Self>,
50            _input: subscription::EventStream,
51        ) -> futures::stream::BoxStream<'static, Self::Output> {
52            use futures::stream::StreamExt;
53
54            smol::Timer::interval(self.0).boxed()
55        }
56    }
57}