Implement time::every in iced_futures

This commit is contained in:
Héctor Ramón Jiménez 2020-04-30 05:37:44 +02:00
parent bb9ccc4f62
commit e2076612cb
14 changed files with 110 additions and 169 deletions

View file

@ -6,7 +6,5 @@ edition = "2018"
publish = false
[dependencies]
iced = { path = "../..", features = ["async-std", "canvas", "debug"] }
iced_native = { path = "../../native" }
async-std = { version = "1.0", features = ["unstable"] }
iced = { path = "../..", features = ["canvas", "tokio"] }
itertools = "0.9"

View file

@ -1,14 +1,13 @@
//! This example showcases an interactive version of the Game of Life, invented
//! by John Conway. It leverages a `Canvas` together with other widgets.
mod style;
mod time;
use grid::Grid;
use iced::{
button::{self, Button},
executor,
slider::{self, Slider},
Align, Application, Column, Command, Container, Element, Length, Row,
time, Align, Application, Column, Command, Container, Element, Length, Row,
Settings, Subscription, Text,
};
use std::time::{Duration, Instant};

View file

@ -1,34 +0,0 @@
use iced::futures;
pub fn every(
duration: std::time::Duration,
) -> iced::Subscription<std::time::Instant> {
iced::Subscription::from_recipe(Every(duration))
}
struct Every(std::time::Duration);
impl<H, I> iced_native::subscription::Recipe<H, I> for Every
where
H: std::hash::Hasher,
{
type Output = std::time::Instant;
fn hash(&self, state: &mut H) {
use std::hash::Hash;
std::any::TypeId::of::<Self>().hash(state);
self.0.hash(state);
}
fn stream(
self: Box<Self>,
_input: futures::stream::BoxStream<'static, I>,
) -> futures::stream::BoxStream<'static, Self::Output> {
use futures::stream::StreamExt;
async_std::stream::interval(self.0)
.map(|_| std::time::Instant::now())
.boxed()
}
}