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(duration: std::time::Duration) -> Subscription<std::time::Instant> {
30        subscription::from_recipe(Every(duration))
31    }
32
33    #[derive(Debug)]
34    struct Every(std::time::Duration);
35
36    impl subscription::Recipe for Every {
37        type Output = std::time::Instant;
38
39        fn hash(&self, state: &mut Hasher) {
40            use std::hash::Hash;
41
42            std::any::TypeId::of::<Self>().hash(state);
43            self.0.hash(state);
44        }
45
46        fn stream(
47            self: Box<Self>,
48            _input: subscription::EventStream,
49        ) -> futures::stream::BoxStream<'static, Self::Output> {
50            use futures::stream::StreamExt;
51
52            smol::Timer::interval(self.0).boxed()
53        }
54    }
55}