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